nginx的certbot证书续签

certbot证书可以手动续签和自动续签,自动续签方式有两种:通过crontab周期任务和通过系统systemd。

手动续签

sudo certbot certificates	//证书有效期查询
sudo systemctl stop nginx	//关闭nginx,解除占用端口
sudo certbot renew				//续签证书
sudo systemctl restart nginx	//重启nginx
sudo certbot certificates

自动续签

一、crontab建立周期任务

certbot默认配置:

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
#
# Important Note!  This cronjob will NOT be executed if you are
# running systemd as your init system.  If you are running systemd,
# the cronjob.timer function takes precedence over this cronjob.  For
# more details, see the systemd.timer manpage, or use systemctl show
# certbot.timer.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

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

如果系统运行了systemd,这里crontab不会执行!

我选择自己添加crontab任务:

crontab -e
0 0  1 */3 * sudo systemctl stop nginx && certbot -q renew --renew-hook "systemctl restart nginx" //每隔3个月的当月第一天0分0秒执行一次

前5位表示分(0-60)、时(0-24)、天(1-30)、月(1-12)、周(0-6)。
- q表示不输出执行结果日志。

补充:

crontab -l //查看当前用户周期任务
crontab -l -u root //查看root用户周期任务
cat /etc/passwd | cut -f 1 -d : | xargs -I {} crontab -l -u {} //以root用户执行,查看所有周期任务
  • /var/spool/cron/crontabs/ 路径下有所有用户的任务。
  • /etc/crontab 负责调度各种管理和维护任务。
  • /etc/cron.d/ 存放着要执行的crontab任务。

二、systemd

前面提到certbot默认不会在运行了systemd的系统上通过crontab自动续签,通过systemctl管理。

// 开启服务
sudo systemctl start certbot.service
sudo systemctl start certbot.timer
sudo systemctl status certbot.service // 查看certbot服务
sudo systemctl status certbot.timer // 查看certbot的周期运行服务

这两个服务的配置文件路径:/lib/systemd/system/certbot.service/lib/systemd/system/certbot.timer

如果启用了服务却没有自动续签,查看系统日志:

sudo journalctl -u certbot

报错显示我是因为开启nginx占用了端口导致的,修改cerbot.service配置文件为:

[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=certbot renew --pre-hook "systemctl stop nginx" --post-hook "systemctl restart nginx"
PrivateTmp=true

然后运行systemctl daemon-reload

或者使用python3-certbot-nginx插件,见 https://www.f5.com/company/blog/nginx/using-free-ssltls-certificates-from-lets-encrypt-with-nginx

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值