文章目录
在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会影响的软件。
参考: