使用certbot命令和定时任务(crontab)自动更新 let’s encrypt证书

作者

let’s encrypt 提供免费的https证书,使用非常方便。可以在线自动更新,目前一个证书的有效期只有90天,所以90天内,至少要更新一次证书。所以可以通过定时任务crontablet’s encrypt 提供的证书管理工certbot,可以实现对证书的自动管理,可以对证书进行管理(增加,删除)等操作。

使用crontab 对证书进行自动管理

crontab -e 增加两条定时任务

# 每月的 1,7,21,28号, 4点30 更新证书
30 4 1,7,21,28 * * /usr/bin/certbot-2 renew
# 每月的 1,7,21,28号, 5点30 重新启动nginx 服务器
30 5 1,7,21,28 * * /usr/sbin/nginx -t && killall  nginx  && /usr/sbin/nginx

查看当前的证书信息

certbot certificates 查看当前的所有的证书信息

certbot certificates
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: liuhaolin.com
    Domains: liuhaolin.com www.liuhaolin.com
    Expiry Date: 2019-04-03 23:25:43+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/liuhaolin.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/liuhaolin.com/privkey.pem

默认的情况,证书到期前30天,执行certbot renew 证书才会更新,每个证书有个配置文件。默认在文件夹 /etc/letsencrypt/renewal 下,有每个证书的信息。

cat /etc/letsencrypt/renewal/www.liuhaolin.com.conf

let's encrypt 证书的配置文件

可以看到一行的 renew_before_expiry = 30 days 意思是,距离证书到期时间为30天以内的时候,执行更新证书的命令certbot-2 renew才会更新证书。

回复

您的电子邮箱地址不会被公开。