外观
3.3 自动续期配置
约 475 字
HTTPS续期
本页对应「三、操作步骤」的 3.3,完成自动续期配置。返回上级:HTTPS 证书申请与续期操作指南。
前置条件
- 3.2 已完成,站点使用新证书且 HTTPS 访问正常。
- systemd timer 或 cron 可用,服务器可出网访问 ACME 服务。
- 已有可写入的日志目录
/var/log/axis。
操作步骤
- 确认系统自带的续期定时器。
systemctl list-timers | grep certbot
systemctl status certbot.timer --no-pager- 编写续期后自动重载 Nginx 的钩子,路径为
/etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh:
#!/usr/bin/env bash
set -euo pipefail
nginx -t && systemctl reload nginx
logger -t certbot-hook "nginx reloaded after certificate renewal"- 赋可执行权限并单独测试钩子能否成功重载。
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
sudo /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
sudo journalctl -t certbot-hook -n 5 --no-pager- 执行一次模拟续期,验证验证路径与钩子全链路可用。
sudo mkdir -p /var/log/axis
sudo certbot renew --dry-run --deploy-hook "systemctl reload nginx"- 让续期结果落盘并纳入监控,避免失败静默。
sudo tee /etc/cron.d/certbot-renew-check >/dev/null <<'EOF'
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 6 * * * root certbot renew --quiet --deploy-hook "systemctl reload nginx" >> /var/log/axis/cert-renew.log 2>&1
EOF
sudo chmod 644 /etc/cron.d/certbot-renew-check结果确认
certbot.timer处于 active (waiting),NEXT 时间为未来 24 小时内。- dry-run 输出
Congratulations, all simulated renewals succeeded。 - 钩子执行后
journalctl -t certbot-hook有对应记录,Nginx 仍为 active。 /var/log/axis/cert-renew.log可写,手动追加一行测试无权限错误。
注意事项
- systemd timer 与 cron 同时配置会造成重复续期,保留一种触发方式。
- 续期失败不会自动回滚,必须通过日志与“证书剩余天数”告警发现问题。
- deploy-hook 中的
nginx -t失败会阻止重载,这是预期保护行为,应先修复配置。 - 删除 cron 文件时同步清理日志,避免日志无限增长。