HTTPS 全新免費(fèi)攻略(本文只針對Centos+Nginx)
1.最簡單的阿里云免費(fèi)證書
截止當(dāng)前2018.11.19,阿里云DNS解析配置處可以直接申請免費(fèi)SSL證書,但限制1年有效期,且每個域名下只能申請3個單域名,美其名曰測試專用。申請完成后,下載證書直接nginx配置即可。阿里云在云盾證書過期前會有通知提醒。
阿里云證書購買那邊怎么顯示出免費(fèi)的證書就不表了。
2.Let's Encrypt + Certbot(手動驗證)
不想付費(fèi),又想隨意申請免費(fèi)證書,看這里官方文檔。
- 安裝Certbot:
yum install -y epel-release && yum install -y certbot(certbot 出現(xiàn)python相關(guān)的安裝問題,參考附錄【4】) - Nginx 配置 Certbot 手動驗證的路徑
在原 Nginx的 server 下添加以下配置
server {
...
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/share/nginx/certbot;
}
location = /.well-known/acme-challenge/ {
return 404;
}
...
}
Certbot 會在/usr/share/nginx/certbot生成相應(yīng)的驗證文件并確認(rèn)域名。
- Certbot 驗證域名,成功后生成證書
sudo certbot certonly --webroot -w /usr/share/nginx/certbot -d www.host.com
請?zhí)鎿Q [www.host.com] 成相應(yīng)的域名。
- Nginx 配置證書
server {
listen 443 ssl;
server_name www.host.com;
charset utf-8;
access_log /var/log/nginx/log/https_api.access.log main;
ssl_certificate /etc/letsencrypt/live/www.host.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.host.com/privkey.pem;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
...
}
(Docker Nginx下的坑,因為Certbot 生成的證書是軟連接文件,在配置證書路徑的時候要包含 /etc/letsencrypt/ 整個路徑,不然nginx啟動時候找不到對應(yīng)的證書,會報錯SSL: error:02001002:system library:fopen:No such file or directory:...)
- Certbot 自動更新, Let's Encrypt 的證書有效期只有3個月,不想服務(wù)器突然掛了,就配置好自動更新吧(注意nginx 要重啟,不能重載nginx -s reload 只會坑死你,別問我怎么知道的。。。宕機(jī)半小時多嚇?biāo)廊肆?。
# 更新證書測試
certbot renew --dry-run
# 驗證證書過期時間
openssl x509 -noout -dates -in /etc/letsencrypt/live/[www.host.com]/cert.pem
# 定時更新證書,并重啟nginx服務(wù)器(systemctl)
crontab -e
# add
0 2 1 * * /usr/bin/certbot renew --quiet && /bin/systemctl restart nginx
# or docker
# add
0 2 1 * * /usr/bin/certbot renew --quiet && docker ps | grep 'nginx:latest' | awk '{print "docker restart " $1}' | sh