------------恢復內容開始------------
環境: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
------------恢復內容結束------------