let’s encrypt提供免费的 https 证书,使用非常方便。可以在线自动更新,目前一个证书的有效期只有90天,所以90天内,至少要更新一次证书。所以可以通过定时任务 crontab 和 let’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
可以看到第一行的 renew_before_expiry = 30 days 意思是,距离证书到期时间为30天以内的时候,执行更新证书的命令 certbot-2 renew 才会更新证书。
本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统