Certbot对免费Let's Encrypt证书的续期

Let’s Encrypt 证书的有效期为90天,到期之前我们得给证书续期,不然依赖此证书的网站等服务会出现问题。

准备

首先,检查你的 Let’s Encrypt 证书是否管理程序是 Certbot(你是否安装了 Certbot)。

直接敲击certbot命令,若能找到命令就是了。

或者执行命令:

find / -name "certbot"

如果找到类似于 /bin/certbot  的结果,那么说明已安装了 Certbot。

如果没有找到,先下载 Certbot 安装 Let’s Encrypt 证书。

续期

certbot renew --renew-hook "service nginx reload"
--renew-hook "service nginx reload" 表示续期完成后,重新加载nginx服务。

或者

certbot renew
service nginx restart/reload

若报错:

Attempting to renew cert (proxy.piaoling.win) from /etc/letsencrypt/renewal/proxy.piaoling.win.conf produced an unexpected error: Problem binding to port 80: Could not bind to IPv4 or IPv6.. Skipping.

此时需要停止掉占用80端口的进程,待续期完成后再重新开启该进程,比如:Nginx、Apache等。

 此时表示成功续期90天!

但是现在 Certbot 也会自己判断了,没有快到期之前,它也觉得没必要频繁续期。会出现如下结果:

Certbot 到底在证书到期前多久可以去续期呢?

在 /etc/letsencrypt/renewal 目录下,有你的网站的 renewal 的配置文件:xxxxxx.conf

从这个文件中我们可以看到,第一行虽然用 # 注释掉不生效了,但是我们可以知道 Certbot 会在证书到期前30天才可以开始重新续期。

自动续期

每次都手动续期的话,可能比较麻烦,也容易忘,我们可以写个的脚本添加到Crontab来实现自动续期。

其实安装certbot时,certbot已经为我们提供了两种自动续期的方式。

1、方式一:crontab

我们可以在系统中找到/etc/cron.d/certbot文件。

文件内容如下:

0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

建议做如下修改,证书续期的同时让nginx(其他工具同理)重新加载证书文件。

0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew --renew-hook "systemctl reload nginx"

------ ! -d /run/systemd/system:判断系统中是否运行于systemd服务,若有则不进行任何操作。为什么会这样呢,因为certbot在运行有systemd服务的系统中提供了systemd.timer的证书续期方式,也就是第二种方式。

2、方式二:systemd timer

对于运行有systemd服务的系统,我们可以在 /lib/systemd/system中找到certbot.servcie,certbot.timer。

同样我们需要修改certbot.service文件,将文件内容中的/usr/bin/certbot -q renew修改为/usr/bin/certbot -q renew --renew-hook "systemctl reload nginx"。

作者書劍飄零
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/oboth-zl/p/14309353.html