在CentOS7上的nginx中部署Let’s Encrypt免费证书

本文介绍如何在CentOS7上的nginx中部署Let‘s Encrypt免费证书。

之前一直使用的是沃通免费证书,最近看到网上的一些消息,担心Firefox和Chrome会取消它的根证书,所以准备逐步替换为Let’s Encrypt免费证书。目前已在https://demo.lyz810.com中使用,未来可能会逐步将其他域替换为Let’s Encrypt免费证书。
下面将操作步骤记录一下,以便后续替换时查阅。

一、安装certbot
文档:https://certbot.eff.org/#centosrhel7-nginx
$ sudo yum install epel-release
$ sudo yum install certbot

二、为域名申请一个证书
-w后面是站点根目录
-d后面是站点域名,如果多个域名,可以使用多个-d参数,每个-d参数跟一个域名,-d之间用空格分开
certbot certonly --webroot -w /website/lyz810-main/demo/ -d demo.lyz810.com

1.提示输入邮箱,用于紧急通知以及密钥恢复
2.阅读文档,选Agree即可

如果成功证书和私钥会保存在/etc/letsencrypt/live/demo.lyz810.com/中

三、nginx配置证书
ssl_certificate /etc/letsencrypt/live/demo.lyz810.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/demo.lyz810.com/privkey.pem;
重启nginx服务器

四、证书自动续期
证书有效期为90天,所以需要写一个定时任务

#minute   hour    day  month  week    command
0         0,12    *    *      *       certbot renew > /var/log/certbot.log & echo certbot last renew at `date` >> /var/log/certbot.log

在每天0点和12点会更新一次证书,并将结果保存到/var/log/certbot.log日志中。
注意:
如果设置cron或systemd任务,建议一天执行两次(如果没有到需要更新的时间段内,运行此命令不会更新证书)