Let’s Encrypt 90天续期全流程:certbot 续期窗口、定时任务与报错排查
Let's Encrypt 证书有效期固定 90 天,官方建议在剩余 30 天(即证书生命周期第 60 天)发起 letsencrypt 90天续期,certbot 通过 renewal 配置文件里的 renew_before_expiry = 30 days 实现该窗口,配合 certbot.timer 即可全自动完成。 certbot.timer 默认 OnCalendar=*-*-* 00,12:00:00 且 RandomizedDelaySec=43200(最多 12 小时随机抖动),每天最多执行两次 certbot renew,避免整点集中请求 Let's Encrypt 接口。 验证 letsencrypt 90天续期是否生效,执行 certbot certificates 看 VALID 天数是否从小于 30 天回到约 90 天,或用 openssl s_client -connect example.com:443 -servername example.com 查看 notAfter 是否前移约 60 天。 Let's Encrypt 的失败验证限制为每账号每主机名每小时 5 次,调试阶段一律先跑 certbot renew --dry-run(走 staging 环境),确认无误再正式续期。 续期成功后必须执行 systemctl reload nginx(或 apachectl graceful),否则进程仍加载旧证书,用 --deploy-hook 把它固化进 renewal 配置。
为什么 Let's Encrypt 证书只有 90 天?letsencrypt 90天续期的时间窗口怎么算
先纠正一个流传很广的误解:90 天不是行业硬性上限。据 CA/Browser Forum 的 Baseline Requirements,公共 TLS 证书最长有效期是 398 天;90 天纯粹是 Let’s Encrypt 自己的 CA 策略。官方文章《Why ninety-day lifetimes for certificates?》明确给出三条理由:一是把续期逼向自动化(人工续期在 90 天下会成为沉重的运维负担);二是缩短私钥泄露后的“有效作恶窗口”;三是降低 CRL 与吊销信息传播滞后的风险。2021-09-30,旧交叉链 DST Root CA X3 到期引发的安卓兼容问题,进一步证明了客户端对自动续期的依赖程度。
letsencrypt 90天续期的时间窗口是怎么算出来的?答案落在 certbot 的续期配置文件里,而不是任何全局常量。默认阈值是到期前 30 天,即证书生效后的第 60 天:
# 查看当前证书的到期时间与剩余天数(certbot 1.0+ 支持,snap/apt 包均可用)
sudo certbot certificates
# 输出示例:
# Certificate Name: example.com
# Domains: example.com www.example.com
# Expiry Date: 2025-06-01 08:00:00+00:00 (VALID: 62 days)
# 续期阈值保存在每个证书独立的 renewal 配置中
sudo grep -n "renew_before_expiry" /etc/letsencrypt/renewal/example.com.conf
# renew_before_expiry = 30 days <-- 默认值,certbot renew 每次运行都会解析它
# 只有当剩余有效期 < 30 天时才会真正签发;否则输出:
# Cert not yet due for renewal
# 演练:完整走一遍 ACME 流程但不替换生产证书,用来验证 80 端口与 DNS
sudo certbot renew --dry-run
# 确实需要立刻换发(例如私钥疑似泄露)时强制续期,注意会消耗速率配额
sudo certbot renew --cert-name example.com --force-renewal
为什么不拖到最后几天?据 Let’s Encrypt 官方 Rate Limits 文档,失败的验证限制是每账号每主机名每小时 5 次;注册域名维度约每周 50 张证书(含重复签发)。提前 30 天续期,等于给自己留出约 4 周的重试余量,DNS 解析异常、安全组误关 80 端口、ACME 客户端升级失败都能从容处理。反过来,如果只在到期前 3 天才续期,一旦碰上速率限制,就只能等窗口重置。
调度层如何避免整点洪峰?Let’s Encrypt 建议续期发生在生命周期剩余约 1/3 时并加入随机抖动。certbot 的 systemd timer 用 RandomizedDelaySec=43200 把每天两次检查打散到 12 小时内;acme.sh 则依赖自带 cron,每天 00:00 检查一次。
| 方案 | 默认触发频率 | 随机抖动 | 查看方式 |
|---|---|---|---|
| certbot systemd timer(1.0+ 官方包) | 每天 2 次 | RandomizedDelaySec=43200 |
systemctl list-timers | grep certbot |
| certbot + cron | 官方示例为每天 2 次 | 无,需自行加 sleep $((RANDOM%3600)) |
crontab -l |
| acme.sh 内置 cron | 每天 1 次(0 0 * * *) |
无 | crontab -l | grep acme.sh |
90 天不是终点。CA/Browser Forum 已通过 SC-081 提案,计划把公共 TLS 证书最长有效期分阶段压缩到 200 天、100 天、47 天(以 CA/B 官方 ballot 页面为准)。现在就把 letsencrypt 90天续期做成纯自动化流水线,未来缩短上限时只需改 renew_before_expiry 阈值,无需重做架构。
顺带一个链路细节:Let’s Encrypt 已宣布逐步关闭 OCSP responder、转向 CRL 分发,因此 letsencrypt 90天续期后不必再为每张新证书单独维护 OCSP stapling 的响应文件;但 fullchain.pem 必须完整包含中间证书(含 ISRG Root X1 交叉链),否则部分安卓客户端会报 NET::ERR_CERT_AUTHORITY_INVALID 或“证书链不完整”。用 openssl s_client -connect example.com:443 -showcerts 可以逐张核对下发链路。
letsencrypt 90天续期完整实操步骤(certbot 版,7 步)
先回答最常被问的一句:Let’s Encrypt 证书真的只有 90 天吗?是。据 Let’s Encrypt 官方文档《Why ninety-day lifetimes for certificates?》,90 天是刻意选择——短周期能压缩密钥泄露的暴露窗口,也倒逼自动化。这意味着 letsencrypt 90天续期不是”一年做一次”的运维动作,而是一条必须常驻的流水线。下面 7 步从”看现状”走到”加告警”,每一步都能直接复制执行。
第 1 步 摸清现状:先看版本,再看每张证书还剩几天
# 确认 certbot 版本:2.11.0 与 3.x 的参数基本一致,1.x 部分钩子行为不同
certbot --version
# 输出示例:certbot 2.11.0
# 列出本机所有受管证书,重点看 Expiry Date 后的 VALID: N days
certbot certificates
Found the following certs:
Certificate Name: example.com
Domains: example.com www.example.com
Expiry Date: 2025-06-30 08:12:33+00:00 (VALID: 42 days)
Certificate Name: api.example.com
Domains: api.example.com
Expiry Date: 2025-06-09 11:40:07+00:00 (VALID: 21 days)
把 VALID 小于 30 天的域名单独抄出来——按 certbot 默认 30 天续期窗口,这些就是本轮 letsencrypt 90天续期必须处理的对象。
| certbot certificates 中的 VALID | 含义 | 建议动作 |
|---|---|---|
| > 60 days | 刚签发 | 无需操作,确认 timer 已启用即可 |
| 30–60 days | 正常,尚未进窗 | 检查定时器下次触发是否在 12 小时内 |
| 21–30 days | 已进窗但未成功 | 立刻 dry-run,翻 /var/log/letsencrypt/letsencrypt.log |
| < 21 days | 告警阈值已破 | 人工介入,准备手工 renew |
| < 7 days | 高危 | 先手工续期保住线上,再补查根因 |
第 2 步 干跑验证:不消耗正式速率限制
# --dry-run 走 ACME staging 环境,不占用正式环境的速率配额
certbot renew --dry-run
# 成功时结尾必为:
# Congratulations, all simulated renewals succeeded:
# /etc/letsencrypt/live/example.com/fullchain.pem (success)
这是排查 letsencrypt 90天续期问题成本最低的手段。失败时典型输出如下,先分清是”验证不通”还是”限流”:
Certbot failed to authenticate some domains (authenticator: nginx).
Domain: example.com
Type: unauthorized
Detail: 2606:4700::6810:84e5: Invalid response from
http://example.com/.well-known/acme-challenge/xxxx: 404
# 若看到下面这条,是验证失败次数超限,需等 1 小时冷却
Error creating new order :: too many failed authorizations recently:
see https://letsencrypt.org/docs/failed-validation-limit/
第 3 步 固定续期窗口:默认 30 天,敏感业务可压到 21 天
# 编辑 /etc/letsencrypt/renewal/example.com.conf
[renewalparams]
authenticator = nginx
# 到期前多少天开始尝试续期;缺失时 certbot 按 30 days 处理
renew_before_expiry = 30 days
# 续期成功后自动执行的钩子(见第 5 步)
renew_hook = systemctl reload nginx
业务对证书轮换敏感(如存在证书 pinning、双向 TLS 对端校验)时,把 30 改成 21 days,可缩短”新旧证书并存、新旧 CA 链混用”的时间窗。注意该值必须小于 90 天有效期,否则每次 renew 都会立刻触发。
第 4 步 启用定时任务:确认 12 小时内会跑一次
systemctl enable --now certbot.timer
systemctl list-timers certbot.timer
# NEXT LEFT LAST PASSED UNIT
# Tue 2025-06-17 03:14:22 UTC 9h left Mon 2025-06-16 15:02:11 UTC 3h 12min certbot.timer
certbot 自带的 systemd 单元为 OnCalendar=*-*-* 00/12:00:00 并叠加 RandomizedDelaySec=43200,即每天两次、最多再随机推迟 12 小时——CA 侧因此不会在同一秒被全世界的服务器打爆。LEFT 若大于 12 小时,说明 timer 没真正生效。容器或精简系统无 systemd 时改 cron:
# /etc/cron.d/certbot —— 每 12 小时一次,-q 抑制无变化时的输出
0 */12 * * * root certbot renew -q --deploy-hook "systemctl reload nginx"
第 5 步 固化重载动作:解决”证书已更新、线上仍是旧证书”
# 方式 A:写进 renewal 配置(推荐,随证书走)
renew_hook = systemctl reload nginx
# 方式 B:命令行追加,适合一次性验证
certbot renew --deploy-hook "systemctl reload nginx"
| 钩子参数 | 触发时机 | 典型用途 |
|---|---|---|
--pre-hook |
每次续期尝试前 | 临时放行 80 端口、暂停 WAF |
--deploy-hook |
仅证书真正更新后 | reload nginx/postfix、推送 CDN |
--renew-hook |
每次 renew 成功后(即使未换证) | certbot 2.x 起与 deploy-hook 行为趋同,官方推荐统一用 deploy-hook |
--post-hook |
无论成败都执行 | 恢复防火墙规则 |
第 6 步 首次手工跑一遍,把整条链路验证通
# --force-renewal 会立即换发新证书并消耗 duplicate certificate 配额(每注册域名每周 5 张),只做一次
certbot renew --cert-name example.com --force-renewal
--deploy-hook "systemctl reload nginx"
# 观察日志中是否出现续期成功且 Hook 无报错
grep -E "Renewing an existing certificate for example.com|Hook command"
/var/log/letsencrypt/letsencrypt.log
若出现 Hook command "systemctl reload nginx" returned error code 1,说明证书已换但服务没重载,属最危险状态:线上仍在用旧证书。用 openssl s_client -connect example.com:443 -servername example.com | openssl x509 -noout -dates 对比 notAfter 与本地 fullchain.pem 即可确认。
第 7 步 加到期告警:比续期脚本更重要的兜底
#!/usr/bin/env bash
# /usr/local/bin/check-cert-expiry.sh —— 每日 cron 巡检
CERT=/etc/letsencrypt/live/example.com/cert.pem
# -checkend 单位秒;返回 0 表示仍在有效期内,非 0 表示即将过期
if ! openssl x509 -checkend $((21*86400)) -noout -in "$CERT"; then
echo "WARN: example.com 证书剩余不足 21 天" |
mail -s "[证书告警] example.com" ops@example.com
fi
# /etc/cron.d/cert-check —— 每天 06:17 巡检
17 6 * * * root /usr/local/bin/check-cert-expiry.sh
openssl x509 -checkend 的行为在 OpenSSL 官方手册中有明确定义:证书在指定秒数内过期则退出码为 1。返回非 0 就意味着 letsencrypt 90天续期大概率已连续失败多轮,此时不要再等定时器,直接按第 2 步 dry-run + 第 6 步手工续期介入。
letsencrypt 90天续期配置与命令示例(certbot 3.x / systemd timer / acme.sh)
certbot 3.x 的续期不是靠”到期前跑一次”,而是靠一个每天触发两次的 systemd timer,再由 certbot 自己判断是否进入续期窗口。据 Let’s Encrypt 官方文档,证书有效期固定 90 天,官方推荐在剩余约 30 天时续期,这也是 certbot renew 的默认判定阈值(renewal 配置中的 renew_before_expiry 默认值为 30 days)。下面给出可直接落地的单元文件、cron 等价写法与 renewal 配置。
1)systemd timer:覆盖发行版自带单元
Debian/Ubuntu 的 certbot 包自带 /usr/lib/systemd/system/certbot.timer,要改行为请把同名文件放到 /etc/systemd/system/ 下覆盖,而不是直接改包内文件(升级会被覆盖)。
# /etc/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily
[Timer]
# 每天 00:00 与 12:00 各触发一次(certbot 自身再判断是否进入 30 天窗口)
OnCalendar=*-*-* 00,12:00:00
# 0~43200 秒随机抖动,避免整点全球 CA 请求尖峰
RandomizedDelaySec=43200
# 关机错过的任务开机后补跑一次
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/certbot.service
[Unit]
Description=Certbot renewal
[Service]
Type=oneshot
# -q 只输出 warning/error,完整日志仍写入 /var/log/letsencrypt/letsencrypt.log
ExecStart=/usr/bin/certbot -q renew
sudo systemctl daemon-reload
sudo systemctl enable --now certbot.timer
systemctl list-timers certbot.timer # 查看 NEXT/LAST 触发时间
systemd-analyze calendar "*-*-* 00,12:00:00" # 校验 OnCalendar 表达式
sudo systemctl start certbot.service # 立即手动跑一次,验证无报错
适用版本:systemd 229+(RandomizedDelaySec 自 229 起可用,systemd-analyze calendar 自 231 起可用)。
2)非 systemd 环境的 cron 等价写法
certbot 官方文档给出的 cron 形式如下,注意用 [ ! -d /run/systemd/system ] 做互斥,避免和 timer 重复执行。RANDOM 取值 0–32767 秒,抖动幅度略小于 12 小时,效果与 RandomizedDelaySec 接近:
# /etc/cron.d/certbot
0 0,12 * * * root [ -x /usr/bin/certbot ] && [ ! -d /run/systemd/system ] && sleep $((RANDOM % 43200)) && /usr/bin/certbot -q renew
3)renewal 配置片段
# /etc/letsencrypt/renewal/example.com.conf
[renewalparams]
authenticator = webroot
webroot_path = /var/www/html
key_type = ecdsa
elliptic_curve = secp256r1
renew_before_expiry = 30 days
renew_hook = nginx -t && systemctl reload nginx
关键坑:key_type 只在”签发新证书”时生效。Certbot 1.x 默认 RSA 2048,2.0 起新证书默认 ECDSA secp256r1;但已有证书的密钥类型已固化,只改配置不会换钥,必须 certbot renew --force-renewal 或重新签发一次才会切换。
4)acme.sh 3.0.x 方案
# 签发:剩余 60 天即续期(--days 60),比 certbot 的 30 天更保守
acme.sh --issue -d example.com -w /var/www/html --days 60
# 安装证书:把 fullchain/key 落到 nginx 路径,--reloadcmd 会随每次续期自动执行
acme.sh --install-cert -d example.com
--key-file /etc/nginx/ssl/example.com.key
--fullchain-file /etc/nginx/ssl/example.com.fullchain.pem
--reloadcmd "nginx -t && systemctl reload nginx"
5)重载命令必须幂等
systemctl restart nginx 会造成数百毫秒到数秒的连接中断;letsencrypt 90天续期每 60 天左右才真正生效一次,完全没必要用重启换风险。统一写成先语法检查再 reload,配置写错时 nginx -t 返回非零,reload 不会执行,旧配置继续服务。
| 维度 | certbot 3.x + systemd timer | acme.sh 3.0.x |
|---|---|---|
| 续期阈值 | 剩余 30 天(renew_before_expiry) |
--days 60,默认 60 天 |
| 调度载体 | certbot.timer,每天 2 次 + 随机抖动 | 安装时写入 crontab,每天 1 次 |
| 默认密钥 | 2.0 起 ECDSA secp256r1 | ec-256 |
| 运行依赖 | Python + systemd | 纯 shell,无 Python |
| 部署钩子 | renew_hook / --deploy-hook |
--reloadcmd |
6)日志与调试
journalctl -u certbot.service --since "3 days ago" # 先看调度层有没有真的跑起来
sudo tail -n 200 /var/log/letsencrypt/letsencrypt.log # 再看 ACME 交互细节
sudo certbot renew -v --dry-run # 演练,不消耗速率限制
sudo certbot renew -vv # 打印完整 ACME JSON 响应与 challenge token 路径
letsencrypt 90天续期失败时最常见的两类输出是 Failed authorization procedure(webroot 路径与 nginx root 不一致,challenge 文件 404)与 too many failed authorizations recently(触发 Let’s Encrypt 的失败验证速率限制,需等待 1 小时)。调试阶段务必加 --dry-run 走 staging 环境,避免撞上生产速率限制;ACME 协议本身的行为可对照 RFC 8555 第 7 章。
letsencrypt 90天续期方案对比:certbot、acme.sh、Caddy、Traefik 怎么选
先给结论:选型只由三个变量决定——你现有的 Web 服务器是什么、能不能开放 80 端口、证书要不要覆盖通配符。Let’s Encrypt 的证书有效期固定为 90 天,据 Let’s Encrypt 官方文档《Challenge Types》与《Renewing certificates》,官方建议在剩余 30 天左右续期,避免因吊销或时钟偏差导致中断。下面这张表把 2025 年仍在维护的四个主流方案放在一起对比。
| 方案 | 2025 常见版本 | 续期触发窗口 | 调度方式 | 最适合 | 主要代价 |
|---|---|---|---|---|---|
| certbot | 2.11 / 3.x | 剩余 30 天 | systemd timer(每日 00:00、12:00 + 最多 12h 随机延迟) | 已有 nginx/apache,想自动改写配置 | 依赖 Python 运行时,Debian 装包后约 100 MB 级 |
| acme.sh | 3.0.x | --days 60(默认;即 90 天证书签发后约 30 天就续,比 certbot 更早) |
自带 cron,每日 00:00 检查 | DNS-01、通配符、内网无入站端口 | DNS 凭证明文存于 ~/.acme.sh/account.conf |
| Caddy | 2.8 | 生命周期 2/3 处(90 天证书≈剩余 30 天) | 进程内建,无 cron | 容器、反向代理、零额外组件 | 排障要翻容器日志 |
| Traefik | 3.x | 剩余约 30 天 | 内置 ACME 定时检查 | K8s、动态后端、多路由 | CRD/YAML 配置心智负担较高 |
想用 --nginx 插件自动改配置,certbot 值得那 100 MB 吗?
值得,前提是你确实跑 nginx 或 apache。certbot 的独特价值是插件与服务器配置深度绑定:首次签发时它直接写入 443 server 块与 80→443 跳转,letsencrypt 90天续期时复用同一套插件逻辑,续期成功即触发 reload,不需要你维护 --deploy-hook。代价是 Python 依赖,精简镜像里体积偏大。
# 适用 certbot 2.0+(默认密钥类型已是 ECDSA P-256);3.x 参数兼容
sudo certbot --nginx -d example.com -d www.example.com
-m ops@example.com --agree-tos --no-eff-email
# 自动写入 443 server 块 + 80 跳转;续期由 certbot.timer 驱动
# 查看 timer 真实触发时间(含随机延迟)
systemctl list-timers certbot.timer
# NEXT LEFT UNIT ACTIVATES
# Mon 2025-06-02 00:37:12 CST 3h 12min certbot.timer certbot.service
通配符和内网机器为什么更推荐 acme.sh?
它是纯 Shell 实现,支持 100 多个 DNS 服务商 API,DNS-01 不要求任何入站端口。*.example.com 只能用 DNS-01 签发,这是 ACME 协议(RFC 8555)层面的限制,不是工具选择问题。
# 通配符 + 阿里云 DNS-01,无需 80 端口对公网可达
export Ali_Key="LTAI5t..." ; export Ali_Secret="..."
acme.sh --issue --dns dns_ali -d example.com -d '*.example.com' --days 60
# --days 默认 60:剩余 60 天即触发续期
acme.sh --install-cert -d example.com
--key-file /etc/nginx/ssl/example.com.key
--fullchain-file /etc/nginx/ssl/example.com.crt
--reloadcmd "systemctl reload nginx"
chmod 600 ~/.acme.sh/account.conf # 凭证明文存放,必须收紧权限
Caddy 和 Traefik 没有 cron,续期是怎么发生的?
ACME 被内建进进程,letsencrypt 90天续期由进程自身在后台完成,容器化部署零额外组件。Caddy 2.8 在证书生命周期 2/3 处发起续期,日志关键字为 certificate obtained successfully 与 certificate renewed successfully;Traefik 3.x 默认在剩余约 30 天时续期,排障用 docker logs traefik 2>&1 | grep -i acme。
验证方式选错,续期就会失败——真实报错长什么样?
| 验证方式 | 端口要求 | 典型适用 |
|---|---|---|
| HTTP-01 | 80 入站可达 | 常规公网站点 |
| DNS-01 | 无需入站端口 | 内网机器、通配符证书 |
| TLS-ALPN-01 | 443 入站可达 | 80 被占用或强制全站 HTTPS |
Certbot failed to authenticate some domains (authenticator: nginx).
Domain: example.com
Type: unauthorized
Detail: 2606:4700::6810:84e5: Invalid response from
http://example.com/.well-known/acme-challenge/xxxx: 404
# 另见:Timeout during connect (likely firewall problem) —— 多为 80 被安全组拦截
升级前先跑什么?
# certbot 2.x → 3.x:默认密钥类型与部分插件参数有调整,先空跑
sudo certbot renew --dry-run
# acme.sh 升级后重新安装一次证书任务,确保 reloadcmd 仍在 cron 条目里
acme.sh --upgrade && acme.sh --install-cert -d example.com
--key-file /etc/nginx/ssl/example.com.key
--fullchain-file /etc/nginx/ssl/example.com.crt
--reloadcmd "systemctl reload nginx"
证书本身的密钥类型也值得留意:certbot 自 2.0 起默认签发 ECDSA P-256,若你的老客户端仍只支持 RSA,需显式加 --key-type rsa --rsa-key-size 2048,具体曲线与套件建议可对照 Mozilla Server Side TLS 指南。
letsencrypt 90天续期常见报错与排查(附真实报错文本)
certbot 续期失败可以按三层拆解定位:ACME 校验层(HTTP-01、TLS-ALPN-01 能否取到 token)、本机服务层(80 和 443 端口占用、hook 退出码)、网络出口层(能否连通 ACME 目录)。按层定位比反复重跑快得多——据 Let’s Encrypt 文档,同一账户对同一主机名每小时校验失败 5 次即触发限制,盲目重试只会把封禁窗口不断推后。
报错 Detail: Invalid response from http://example.com/.well-known/acme-challenge/xxxx: 404,怎么定位?
这说明 ACME 的 HTTP-01 请求确实到达了站点,只是没命中 .well-known/acme-challenge/ 下的 token 文件。高频原因有两个:--webroot 指向的目录与 nginx 该 server 块的 root 不一致;或者 location ~ /.well-known 被伪静态、rewrite 规则提前拦截。先手工复现再改配置:
# 1. 造一个测试 token,模拟 ACME 取文件路径
sudo mkdir -p /var/www/html/.well-known/acme-challenge
echo ok | sudo tee /var/www/html/.well-known/acme-challenge/test-token
# 2. 走 HTTP 取一次,期望 200 且返回 ok;返回 404 即 webroot 与 root 不一致
curl -I http://example.com/.well-known/acme-challenge/test-token
# 3. 打印 nginx 实际生效配置(nginx 1.9.1+ 支持 -T),核对 root 与 location 顺序
sudo nginx -T | grep -nE "server_name|root|well-known"
关于 301:据 Let’s Encrypt 文档,校验器会跟随 http→https 跳转,但跳转目标必须同样能返回该 token;若 HTTPS 站点用的是另一份 root,结果仍是 404。定位后用同一 webroot 预演:
sudo certbot renew --webroot -w /var/www/html --dry-run
报错 Problem binding to port 80: Could not bind to IPv4 or IPv6 怎么办?
standalone 插件需要独占 80 端口,而 nginx 正在监听。三种方案的取舍如下:
| 校验插件 | 是否独占 80 端口 | 是否需要停 Web 服务 | 典型失败信息 |
|---|---|---|---|
--standalone |
是 | 是 | Problem binding to port 80 |
--webroot |
否 | 否 | Invalid response … 404 |
--nginx |
否 | 否(仅 reload) | Hook command … returned error code 1 |
确实只能用 standalone 时,可在 /etc/letsencrypt/renewal/example.com.conf 的 [renewalparams] 段加 hook,但每次续期都会短暂停机,且 hook 失败会让整次续期被标记为失败:
# /etc/letsencrypt/renewal/example.com.conf
[renewalparams]
authenticator = standalone
pre_hook = systemctl stop nginx
post_hook = systemctl start nginx
报错 Too many failed authorizations recently 之后还能做什么?
已触发每小时 5 次的失败限制,立即停止重试,等满 1 小时后先用 --dry-run(走 staging 环境,限额显著宽松)把问题定位清楚,再切正式环境:
sudo certbot renew --dry-run # 全部证书预演
sudo certbot renew --cert-name example.com --dry-run # 只预演单个证书
报错 Hook command “systemctl reload nginx” returned error code 1
证书其实已签发成功,失败的是 reload hook。此时 letsencrypt 90天续期在磁盘上已完成,但线上仍是旧证书,属于”静默不一致”:
sudo nginx -t # 先过语法校验
sudo stat -c '%y %n' /etc/letsencrypt/live/example.com/fullchain.pem # 确认 mtime 已更新
sudo systemctl reload nginx # nginx -t 通过后再 reload
常见根因是重复的 listen 443 ssl、ssl_certificate 路径拼错,或指向了不存在的域名目录。
报错 requests.exceptions.ConnectionError: HTTPSConnectionPool(host=’acme-v02.api.letsencrypt.org’, port=443): Max retries exceeded
服务器出网 443 被防火墙或代理拦截。先验证连通性,返回 200 才算通:
curl -I https://acme-v02.api.letsencrypt.org/directory # 期望 HTTP/2 200
需要走代理时,环境变量必须设在 certbot 自身的运行环境里(systemd timer 拉起的进程不读 ~/.bashrc):
sudo systemctl edit certbot.service
[Service]
Environment="HTTPS_PROXY=http://127.0.0.1:3128"
输出 Cert not yet due for renewal 是报错吗?
不是。据 certbot 文档,续期窗口默认为到期前 30 天(renewal 配置项 renew_before_expiry = 30 days),窗口之外 renew 只打印这句就退出,这正是 letsencrypt 90天续期留出 30 天缓冲的设计。此时若加 --force-renewal 会真的签发新证书,消耗”同一组域名每周 5 张重复证书”的限额——调试时连跑几次,就可能把自己锁在限额之外。
如何验证 letsencrypt 90天续期真的生效?4 种确认方法
“我明明配了定时任务,怎么还是过期了?”——这类事故几乎都不是续期失败,而是没人验证续期结果。下面四个观测面互相独立,任意一个不通过都说明 letsencrypt 90天续期链路某处断了。据 Let’s Encrypt 官方文档,当前证书有效期为 90 天,certbot renew 默认在剩余不足 30 天时才真正签发新证书(--days 默认 30,可调但官方不建议大于 30)。
方法一:直接问服务端要证书(最可信)
# </dev/null 防止 s_client 等待 stdin 挂起;-servername 必带,否则可能拿到同 IP 默认站点的证书
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null
| openssl x509 -noout -dates -issuer -subject
# 典型输出(续期成功后)
# notBefore=Mar 1 00:12:33 2025 GMT
# notAfter=May 30 00:12:32 2025 GMT
# issuer=C = US, O = Let's Encrypt, CN = R11
判定标准有三条:notBefore 应前移到最近一次续期的时间点;notAfter 与 notBefore 相差约 90 天;issuer 必须是 CN = R10/R11(RSA 中间证书)或 E5/E6(ECDSA 中间证书)。如果看到 CN = ISRG Root X1,说明服务器把根证书也塞进了链里,功能上不报错但多传了一个证书,可用 fullchain.pem 纠正。这套命令适用于 OpenSSL 1.1.1 与 3.x,Windows 自带 OpenSSL 缺失时建议用 WSL 或 Git Bash。
方法二:查 certbot 自己的账本
certbot certificates --cert-name example.com
# 关注两行:Expiry Date 是否顺延、VALID 天数是否从 <30 天回到约 90 天
# 再确认 live/ 下的软链指向了新的归档文件
ls -l /etc/letsencrypt/live/example.com/
# fullchain.pem -> ../../archive/example.com/fullchain3.pem ← 编号应递增
stat -c '%y %n' /etc/letsencrypt/live/example.com/fullchain.pem
# mtime 应等于最近一次续期时间;若仍是几个月前,说明 renew 没写入磁盘
注意 live/ 目录里的文件永远是符号链接,不要把它们复制到别处再引用,否则 certbot renew 更新后你的副本仍是旧的——这是 Nginx 配置里最常见的隐性过期源。
方法三:确认调度器真的跑了
systemctl list-timers certbot.timer --all
# LAST 列应在最近 12–24 小时内;NEXT 列为下一次触发时间
journalctl -u certbot.service --since "7 days ago"
| grep -E "Running certbot renew|Congratulations|Cert not yet due"
# 看到 "Congratulations, all renewals succeeded" 才算成功;
# 只有 "Cert not yet due for renewal" 属正常跳过,不算失败
# 用 cron 部署的场景,日志在 /var/log/letsencrypt/letsencrypt.log
grep -i "Renewing an existing certificate" /var/log/letsencrypt/letsencrypt.log | tail -3
certbot 官方安装包(≥1.21)自带 certbot.timer,默认每天触发两次并带 RandomizedDelaySec 随机延迟,用于给 Let’s Encrypt 的 ACME 端点削峰(据 Let’s Encrypt 官网的 rate limits 说明,同一注册域名每周有签发上限)。若你改用 cron,务必写绝对路径 /usr/bin/certbot 并在 crontab 里显式设置 PATH,否则会出现 certbot: command not found 且只在日志里留一行报错。
方法四:兜底巡检脚本 + 第三方评级
# 剩余不足 21 天即告警:返回 0 表示安全,返回 1 表示即将过期
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null
| openssl x509 -noout -checkend $((21*86400))
# "Certificate will not expire" → 安全
# "Certificate will expire" → 退出码 1,触发告警
把它写进每日 cron 就是 letsencrypt 90天续期的最后一道保险:0 6 * * * /usr/local/bin/tls-check.sh || mail -s "TLS 即将过期" ops@example.com。另外,curl -sI https://example.com 只能证明 TCP/TLS 握手成功,看不到任何证书细节,别拿它当验证手段;要看链完整性,用 Qualys SSL Labs 的 SSL Server Test,若证书链缺中间证书,评级会从 A 掉到 B(据 Qualys SSL Labs 评分文档,链不完整会扣分)。
| 验证方法 | 观测面 | 通过判据 | 局限 |
|---|---|---|---|
openssl s_client |
服务端实际下发的证书 | notBefore 前移、issuer 为 R10/R11/E5/E6 | 前面有 CDN 时看到的是边缘证书 |
certbot certificates |
certbot 账本与文件 | VALID 回到 ~90 天、archive 编号递增 | 账本新 ≠ 服务已加载 |
systemctl list-timers / 日志 |
调度器 | LAST 在 24h 内、日志有 Congratulations | 只证明跑过,不证明生效 |
openssl x509 -checkend |
剩余天数 | 退出码 0 | 需自建告警通道 |
别忘了 CDN 和四层负载均衡。如果站点前面挂着 CDN 或 L4 LB,源站 letsencrypt 90天续期成功后,边缘节点可能仍持有旧证书——此时方法一会显示旧日期,而方法二却显示新证书,两者矛盾就是信号。解决方式二选一:在 CDN 控制台手动触发证书同步/回源更新,或改用 DNS-01 挑战在边缘侧统一签发,让证书根本不经过源站。DNS-01 还能顺带支持通配符证书(*.example.com),这也是 HTTP-01 做不到的。
把 letsencrypt 90天续期做成可观测:监控、告警与回滚
证书续期成功不代表链路可观测——真正的问题是:续期连续失败时,你多久能知道?据 Let’s Encrypt 文档,失败验证(Failed Validation)限制为每账号每主机名每小时 5 次,触发后必须等窗口过去,因此告警阈值必须留足人工修复时间。
Prometheus:一行 PromQL 直击到期时间戳
blackbox_exporter(v0.25.0 起稳定提供该指标)在 HTTPS 探测时会输出 probe_ssl_earliest_cert_expiry,值是证书链中最早到期的那张证书的 Unix 时间戳,比手工解析 notAfter 可靠得多。
# blackbox.yml —— 关键模块配置
modules:
https_cert:
prober: http
timeout: 5s # 探测超时,默认 120s,公网探测建议 5-10s
http:
preferred_ip_protocol: ip4
valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
tls_config:
insecure_skip_verify: false # 必须校验,否则自签证书也会“通过”
# prometheus.yml 抓取任务
scrape_configs:
- job_name: blackbox_tls
metrics_path: /probe
params:
module: [https_cert]
static_configs:
- targets: ["example.com:443", "api.example.com:443"]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 127.0.0.1:9115 # blackbox_exporter 实际地址
# tls_expiry.rules.yml
groups:
- name: tls-expiry
rules:
- alert: SSLCertExpiringSoon
# 剩余不足 21 天即触发;86400 = 一天秒数
expr: (probe_ssl_earliest_cert_expiry - time()) < 21 * 86400
for: 1h # 持续 1 小时,滤掉单次探测抖动
labels:
severity: warning
annotations:
summary: "{{ $labels.instance }} 证书剩余 {{ $value | humanizeDuration }}"
为什么用 for: 1h 而不是默认 0?blackbox 每 30s 抓一次,单次 DNS 抖动就会误报;1 小时意味着连续 120 次失败才告警,既排除噪声,又远早于 21 天窗口。这样即使 letsencrypt 90天续期连续失败,你仍有约三周排障时间。
Zabbix / Nagios:轻量替代方案
Zabbix 6.0+ 内置 web.certificate.get 监控项,返回 JSON,用依赖项 JSONPath $.cert.expiry 提取到期时间戳,触发器写 last(/host/cert.expiry) - now() < 21*86400。Nagios 系用 monitoring-plugins 2.3+ 的 check_http:
# -C 21:证书剩余少于 21 天返回 CRITICAL;-S 走 HTTPS;--sni 必须加,否则多域名主机返回默认证书
/usr/lib/nagios/plugins/check_http -H example.com -p 443 -S --sni -C 21
# 输出示例:OK - Certificate 'example.com' will expire on 2025-09-14 08:12 +0000.
| 方案 | 检查方式 | 阈值 | 告警粒度 | 适用场景 |
|---|---|---|---|---|
| Prometheus + blackbox_exporter | probe_ssl_earliest_cert_expiry |
21 天 + for: 1h |
秒级指标,可算剩余时长趋势 | 已有 Prometheus 栈、主机数量多 |
| Zabbix 6.0+ | web.certificate.get + JSONPath |
21 天 | 默认 1 分钟 | 传统运维体系 |
| Nagios / Icinga | check_http -C 21 |
21 天 | 检查周期(通常 5 分钟) | 存量 Nagios 环境 |
| certbot 自带 | /etc/cron.d/certbot + 邮件 |
无(仅续期失败时) | 仅在执行时刻 | 不可作为唯一手段 |
阈值统一取 21 天而非 7 天,理由是可修复性:Let’s Encrypt 的失败验证限制与每周 5 张的重复证书限制,都可能让“再试一次”变成“等一小时”。三周足够切换验证方式(HTTP-01 换 DNS-01)、排查 80 端口被防火墙拦截或 CDN 回源等问题。
备份与回滚:保住 ACME 账号,别重新注册
# 备份整个目录:accounts(账号密钥)、archive(历史证书)、live(软链)、renewal(续期配置)
sudo tar czf /root/letsencrypt-$(date +%F).tar.gz -C / etc/letsencrypt
sudo chmod 600 /root/letsencrypt-$(date +%F).tar.gz # 内含私钥,必须 600
# 恢复(误删或迁移机器)
sudo systemctl stop nginx
sudo mv /etc/letsencrypt /etc/letsencrypt.broken
sudo tar xzf /root/letsencrypt-2025-06-01.tar.gz -C /
sudo certbot renew --dry-run # 验证账号与续期配置完好
sudo systemctl start nginx
恢复后 certbot renew 可直接继续,因为账号私钥与 renewal/*.conf 都在,无需重新注册 ACME 账号、也不会消耗新账号的速率限制。
# 回滚到上一版证书:archive 下保留 cert1.pem、cert2.pem…
ls -l /etc/letsencrypt/live/example.com/ # 查看当前软链指向
N=2 # 目标版本号,改这里即可
cd /etc/letsencrypt/live/example.com
for f in cert chain fullchain privkey; do
sudo ln -sfn ../../archive/example.com/${f}${N}.pem ${f}.pem # 软链必须是相对路径
done
sudo nginx -t && sudo systemctl reload nginx
回滚仅适用于“新证书链不被某老旧客户端信任”这类极端情况。据 Let’s Encrypt 文档,证书一旦吊销不可“复活”,回退软链只是让服务继续使用旧证书,直到它自然到期。
季度演练与阈值随政策漂移
# 每季度手工跑一次,覆盖系统升级、nginx 配置变更后的回归
sudo certbot renew --dry-run --cert-name example.com # --dry-run 走 staging,不消耗生产速率限制
sudo nginx -t && sudo systemctl reload nginx
阈值不是常数。据 CA/Browser Forum Ballot SC-081 的通过方案,公信证书最长有效期将分阶段从 398 天缩至 200 天、100 天,最终 47 天。若证书变为 47 天,把告警阈值下调到 10–14 天,同时把 /etc/letsencrypt/renewal/example.com.conf 中的 renew_before_expiry(默认 30 days)改为约 15 days,保持“提前三分之一生命周期续期”的比例不变,letsencrypt 90天续期的既有自动化链路才不会被政策变更打个措手不及。
不需要。启用 certbot.timer(或写入 cron)后,certbot renew 会每天检查一次,剩余有效期低于 renew_before_expiry(默认 30 days)时自动签发新证书。你只需要保证 80 或 443 端口可达、renew_hook 能成功重载 Web 服务即可。
前者只在剩余有效期小于阈值时才真正续期,未到期会输出 Cert not yet due for renewal;后者无视窗口强制签发。Let's Encrypt 对“重复证书”约限制 5 张/周,反复 –force-renewal 很容易撞上限额,调试一律用 –dry-run(走 staging 环境)。
三者都通过 ACME 协议签发,自动化程度接近。Let's Encrypt 生态最成熟、客户端支持最广;ZeroSSL 提供 90 天免费 DV 且带 Web 控制台;Buypass Go SSL 也是 90 天。差异主要在速率限制、OCSP/CRL 策略与中间证书链,letsencrypt 90天续期方案在工具链兼容性上通常最省事。
这是触发了每小时 5 次的失败验证限制。立刻停止所有正式环境重试,等待 1 小时;期间用 certbot renew –dry-run 在 staging 环境定位问题(多为 webroot 路径错误、80 端口不通或 DNS 解析异常),确认 dry-run 通过后再回正式环境执行一次。
不能。90 天是 Let's Encrypt 的固定策略,没有延长选项。如果确实需要更长有效期,只能改用商业 DV 证书(常见 1 年,价格区间约每年几十到几百元人民币),但 letsencrypt 90天续期全自动之后,人工成本其实接近于零。
最常见原因是 Web 服务没有 reload,进程仍持有旧证书文件句柄;其次是 CDN 或负载均衡边缘缓存了旧证书。先执行 systemctl reload nginx 并用 openssl s_client 确认源站证书已更新,再到 CDN 控制台刷新或重新同步证书。
通配符只能用 DNS-01 验证,需要把 DNS 服务商的 API 凭证配置给客户端(certbot 的 dns 插件或 acme.sh 的 dns_xxx)。凭证要 chmod 600 存放并定期轮换;同时确认 DNS 记录传播时间(TTL 建议 60-300 秒),否则续期时 TXT 记录可能还没生效。
本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。