文章目錄
在CentOS7上使用Letsencrypt爲Nginx站點配置HTTPS
前言
計劃對自己的站點xdevops.cn 配置HTTPS,但是用自簽名的SSL證書會被瀏覽器認爲不安全,所以採用Letsencrypt簽發的免費SSL證書。
環境準備:
- CentOS7
- nginx (已配置好xdevops.cn和www.xdevops.cn的HTTP站點)
配置過程
安裝Certbot Let’sencrypt客戶端
sudo yum install certbot python2-certbot-nginx
查看certbot版本:
sudo certbot --version
筆者使用的certbot 1.4.0版本生成的 /etc/letsencrypt/ssl-dhparams.pem 已經是2048長度,不需要再另外生成ssl-dhparams。
爲Nginx站點配置HTTPS
sudo certbot --nginx -d xdevops.cn -d www.xdevops.cn
根據提示,輸入郵箱地址、同意協議(A)和不接收廣告郵件(N)。
Certbot Let’encrypt客戶端會:
- 生成證書;
- 部署證書到Nginx站點;
- 修改Nginx站點配置,將HTTP請求重定向爲HTTPS請求;
- HTTPS相關配置;
查看修改後的Nginx配置文件可以看到這些改動。
重新加載Nginx配置:
sudo systemctl reload nginx
驗證站點HTTPS證書
在瀏覽器中打開:
- https://www.ssllabs.com/ssltest/analyze.html?d=xdevops.cn
- https://www.ssllabs.com/ssltest/analyze.html?d=www.xdevops.cn
確保站點的整體評分爲A級。
在瀏覽器中打開https://xdevops.cn來驗證證書信息是否正確。
定時更新Let‘sencrypt SSL證書
Let’s encrypt簽發的SSL證書有效期爲90天,因此需要配置一個crontab定時任務來定時更新SSL證書。
先驗證是否可以更新SSL證書成功:
sudo certbot renew --dry-run
再設置crontab定時任務:
sudo crontab -e
設置爲每天3:15am更新證書:
15 3 * * * /usr/bin/certbot renew --quiet
查看crontab定時任務配置:
sudo crontab -l
參考文檔
-
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-centos-7
-
https://linuxize.com/post/secure-nginx-with-let-s-encrypt-on-centos-7/
Troubleshooting
certbot UnrewindableBodyError
問題:在運行certbot命令時,報錯UnrewindableBodyError
原因:requests和urllib3包有問題
解決方法:
-
重裝requests和urllib3包:
sudo pip uninstall requests sudo pip uninstall urllib3 sudo yum remove python-urllib3 sudo yum remove python-requests sudo yum install python-urllib3 sudo yum install python-requests
-
重裝certbot:
sudo yum install certbot python2-certbot-nginx
雖然這個方法解決了我的問題,但是使用yum remove來刪除軟件在生產上會帶來不可預測的風險!!請謹慎覈對yum remove會影響的軟件。
參考: