centos8 申请 Let’s Encrypt免费通配符ssl 证书


------------恢复内容开始------------

环境:centos8 ,  php7.2 ,  nginx 1.4

因为公司要求,兼容子域名。所以要申请个通配符ssl 证书.

目前,免费的 就是 Let’s Encrypt 了。

用certbot 来申请的。

一: 下载certbot.

    wget https://dl.eff.org/certbot-auto     // 直接下面certbot包

   chmod a+x certbot-auto               // 给权限

二: 申请证书: 

   在安装certbot 的目录下面执行这个,也就是 你执行上面两条命令的目录下。

  因为  ./certbot-auto  是执行当前目录下的这个文件。

   ./certbot-auto certonly  -d *.example.com -d example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory

注意:这里指定了2个域名:*.example.com, example.com,前者通配域名并不包含后者,不包含后者访问example.com会产生无效证书错误。

  • certonly - 表示安装模式,certbot可以有安装模式和验证模式
  • -d - 指定域名
  • --manual 手动安装
  • --preferred-challenges dns 使用dns方式证明域名所有权
  • -server - Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定

  这里我申请的是通配符证书, 匹配 *.example.com,   即是 通配到二级域名。  这里的example 是你的域名啊,不要照抄。

   输入这行命令后: 

        

       这是要权限,y 就行。  回车         

会要求你输入一个邮箱。由于接收什么到期提醒等等的。

  最后出现如下图,此处先不动,很重要。

   

        

  这里标注的就是  DNS 的 TEX 记录。  我们要复制它。然后到我们的域名服务商那里去,,找到我们的域名,

  添加一条 DNS记录。   记录类型就选: TXT。

                                       主机记录:就是 上图红框上面的   _acme-challenge.newyingyong.cn      就是这个,记得抄你的不要写错了

                                      解析线路: 默认的就好,不改。

                                      记录值:  就是上图红框里的值了。

                                      然后确认就可以了

 

如图所示:我的服务器和域名都是阿里云的。

 

  好了,现在就是时间问题了。注意先不要回车,,得等你配置的这个DNS 生效, 可以等几分钟再回车。 我配的时候 很快。 一分钟。

     回车看到 

                        

  

   这个页面,说明,证书生成成功了。  你可以记录下证书的位置。

    你还可以通过  ./certbot-auto certificates  这个命令看到你的证书详细信息。

   

 

 

然后,就是 配置  nginx.conf 文件了 , 配置 nginx.

完整配置文件如下:

server { charset utf-8; client_max_body_size 200M; listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default ipv6only=on; ## listen for ipv6 # 把xxx替换成你的域名 # Make site accessible from server_name server_name xxx.com www.xxx.com; root /site/xxx; index index.html index.htm index.php; access_log /var/log/nginx/xxx/access.log; error_log /var/log/nginx/xxx/error.log; return 301 https://$server_name$request_uri; #redirect http to https location / { # First attempt to serve request as file, then try_files $uri $uri/ /index.php$is_args$args; } # deny accessing php files for the /assets directory location ~ ^/assets/.*\.php$ { deny all; } location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php:9000; #fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~* /\. { deny all; } } # https server server { charset utf-8; client_max_body_size 200M; listen 443 ssl; #listen [::]:80 default ipv6only=on; ## listen for ipv6 ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; ssl_session_timeout 5m; # 把xxx替换成你的域名 # Make site accessible from server_name server_name xxx.com www.xxx.com; root /site/xxx; index index.html index.htm index.php; access_log /var/log/nginx/xxx/access.log; error_log /var/log/nginx/xxx/error.log; location / { # First attempt to serve request as file, then try_files $uri $uri/ /index.php$is_args$args; } # deny accessing php files for the /assets directory location ~ ^/assets/.*\.php$ { deny all; } location ~ \.php$ { try_files $uri =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php:9000; #fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param HTTPS on; } location ~* /\. { deny all; } } 

 

 

配置证书:自动续期。

./certbot-auto renew    //这个命令就是更新证书有效期的。   一般Let’s Encrypt 证书有效期为3个月

 

 当然了,我们也可以设置定时任务。然他自动更新。

crontab -e

0 0 * * * /root/tar/certbot-auto renew --renew-hook "systemctl reload nginx"     //每天执行一次

 

 

 

 

 

参考: https://blog.csdn.net/guyan0319/article/details/80859830

 

------------恢复内容结束------------


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM