1.安装客户端Certbot
Let’s Encrypt提供了很好用的客户端软件Certbot,有了它,证书的创建、验证、签名、安装和续订变得非常容易,只需要执行简单的命令即可。
yum -y install epel-release
yum -y install certbot python3-certbot-nginx
2.给域名创建SSL证书
Certbot将自动为指定的网站域名获取并安装有效的SSL证书。如果要给多个域或子域创建单个证书,只需要将它们作为参数传递给命令即可。参数列表的第一个域名将被Let’s Encrypt用于创建证书的基本域,因此我们将顶级域名设置在最前面,随后是其他子域。
certbot --nginx -d example.com -d www.example.com
参数说明:
--nginx
参数告诉Certbot使用Nginx插件-d
指定网站域名
2.1、错误提示
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: NoInstallationError(“Could not find a usable ‘nginx’ binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.”,)
解决办法:上面提示信息显示没有找到nginx,可以考虑将nginx执行命令放到环境变量中,或者通过设置nginx软链接解决
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
然后再次使用命令获取证书
certbot --nginx -d example.com -d www.example.com
2.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
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module
make
cp ./objs/nignx /usr/local/nginx/sbin/
3.设置定时自动续订
测试自动更新证书
certbot renew --dry-run
输出如下信息则说明可以使用自动更新功能:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for example.com and www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/example.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
编辑crontab进行定时自动更新证书调用
crontab -e
输入如下内容并保存:
30 5 * * 1 /usr/bin/certbot renew >>/var/log/https_renew.log 2>&1
或者
0 12 * * * /usr/bin/certbot renew --quiet
4.设置HTTP强制跳转HTTPS
server {
listen 80;
server_name example.com www.example.com; ##这里修改为网站域名
rewrite ^(.*)$ https://${server_name}$1 permanent; ##这一句是关键
或者
##rewrite ^(.*)$ https://$host$1 permanent;
}
5.重启Nginx
nginx -s reload