为什么使用HTTPS?
浏览器对普通HTTP网站的访问会显示”不安全”警告、HTTPS比HTTP的网站,更容易取得搜索引擎的信任,更利于SEO、HTTPS在网络上传输的数据是加密的,更安全;HTTPS已经成为网站安全的标准配置,本文将介绍如何使用Certbot工具为Nginx服务器自动获取和部署免费的Let's Encrypt SSL证书。
Certbot是什么?
Certbot是Let's Encrypt官方推荐的客户端工具,能够自动化完成证书的获取、安装和续期过程。
Let's Encrypt是什么?
Let's Encrypt是一个免费、自动化、开放的证书颁发机构(CA),它完全免费、证书过期后会自动续期、安装自动化、被所有主流浏览器信任。
准备工作
本文以Centos7举例,其他操作系统请理性参考
- nginx服务器
- 服务器防火墙开放80和443端口
- 域名已解析到服务器IP
安装Certbot
//1、安装epel-release(一个可以为centos提供额外软件包的项目)
sudo yum install epel-release
//2、安装python3-certbot-nginx
sudo yum install certbot python3-certbot-nginx
安装完成后,接着安装证书
//如果只有一个域名,只需要一个-d
sudo certbot --nginx -d 域名1 -d 域名2
注意:如果这一步没有报错,直接按照安装提示继续就好,会要求输入邮箱等,如果报错了按下面的步骤尝试解决。
错误1:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The requested nginx plugin does not appear to be installed
解决方案1:配置nginx环境变量
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
解决方案2:安装certbot-nginx插件
sudo yum install certbot-nginx
错误2:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The nginx plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('Nginx build is missing SSL module (–with-http_ssl_module).',)
解决方案:这个错误是由于nginx缺少SSL模块,要重新配置下,在nginx源码包目录执行下面的命令
./configure --prefix=/usr/local/nginx --with-http_ssl_module
注意:如果报openssl相关的错误,执行一下这个命令:
yum -y install openssl openssl-devel
./configure执行完没问题后运行make命令
make
最后替换重新编译后的nginx包
1、先停止nginx
systemctl stop nginx
2、把原来的nginx备份一下
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
3、在nginx源码包目录的nginx解压目录中执行
cp ./objs/nginx /usr/local/nginx/sbin/
4、启动nginx
systemctl start nginx
其他错误:
如果是报域名相关的错误,需要检查域名解析配置,排查域名是否能正常访问
安装完成后,可以执行下sudo certbot renew –dry-run模拟SSL 证书的自动续签过程(但不会真正更新证书),如果有问题,及时解决,保证后期续签顺利
sudo certbot renew --dry-run
最后:
如果证书成功安装,会有类似下面的提示信息,这时候就可以使用https访问网站了!
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/yourdomain.com/privkey.pem
This certificate expires on 2023-12-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Deploying certificate
Successfully deployed certificate for yourdomain.com to /etc/nginx/nginx.conf
Successfully deployed certificate for www.yourdomain.com to /etc/nginx/nginx.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled HTTPS on https://yourdomain.com and https://www.yourdomain.com



用宝塔,三个月手动更新一次也不麻烦
收藏了,感谢分享