跳到主要内容
Apache

certbot 自动申请与续期 Let’s Encrypt 证书:从 90 天证书到全自动续期的完整实践

一、问题背景:90 天证书与正在收紧的有效期上限

Let’s Encrypt 签发的域名验证(DV)证书有效期固定为 90 天,不提供“一次买断多年”的选项。这意味着任何上线超过三个月的站点,都必须至少经历一次续期。原文中 apt install certbot python3-certbot-nginx 加 certbot --nginx -d example.com 的流程本身没错,问题在于它把“能签下来”当成了终点,而真正的运维风险全部集中在第 91 天。

与此同时,公共 TLS 证书的有效期上限正在被 CA/Browser Forum 持续压缩。根据 SC-081 提案(已被 Chrome、Apple、Mozilla 等根计划采纳执行),时间表如下:

生效日期 单张公共 TLS 证书最长有效期
2026-03-15 200 天
2027-03-15 100 天
2029-03-15 47 天

历史上出现过的 398 天(Apple 曾推动的上限)与 825 天(Chrome 曾提出的上限)方案均已作废,任何仍写着“申请一年期证书”的教程都应视为过时内容。另外两点必须澄清:

  • Let’s Encrypt 免费证书仍是 90 天,低于上述上限,因此签发流程不受影响,但续期失败后的重试窗口会随行业上限收紧而越来越窄;
  • EV 证书不再承诺在浏览器地址栏展示企业名称。Chrome 自 77 起移除 EV 企业名展示,Firefox、Safari 后续跟进,地址栏只保留锁标识,DV/OV/EV 在 UI 层已无可见差异,EV 的价值退回到组织审核与合规采购要求层面。

结论很明确:自动化续期不是“可选优化项”,而是 90 天证书模型下的强制组件。

二、根因分析:续期为什么会在半夜悄悄失败

绝大多数“证书过期”事故并非 ACME 协议本身出问题,而是下面四条链路中任意一条断了,而这条链路平时没有任何告警。

1)HTTP-01 验证链路死锁。 certbot 默认用 HTTP-01:Let’s Encrypt 从公网访问 http://example.com/.well-known/acme-challenge/<token>。如果站点把 80 端口的全部路径 301 到 https,而 https 证书恰好已过期,验证请求就会撞上无效证书,形成“续期需要有效证书、有效证书需要续期成功”的死锁。正确做法是给 /.well-known/acme-challenge/ 单独放行明文,不参与跳转。

2)调度器缺失。 Debian/Ubuntu 的 certbot 包会安装 certbot.timer,其内容大致为 OnCalendar=*-*-* 00/12:00:00(每 12 小时触发一次)、RandomizedDelaySec=43200(随机延迟最多 12 小时,避免全网同时请求)、Persistent=true。在容器、精简镜像或自编译环境中,systemd 不存在,timer 根本不会安装,证书到期后也就不会自动续期。

3)插件不重载。 用 --nginx 签发时,certbot 续期成功后会自动 reload Nginx;用 webroot 或 standalone 则不会。此时磁盘上的证书是新的,Nginx 进程里加载的仍是旧证书,表现为“明明续期成功了,浏览器还是报过期”。

4)限流与 DNS 环境。 Let’s Encrypt 对每个注册域名限制每周 50 张证书、每周 5 张重复证书,同一账号对同一主机名每小时失败 5 次即触发封禁。国内解析商(阿里云、腾讯云、DNSPod)没有官方 certbot DNS 插件,DNS-01 往往只能手工加 TXT 记录,无法全自动。

三、完整实操步骤(命令可直接执行)

Step 0 环境确认。

cat /etc/os-release | head -n 2
nginx -v
ss -lntp | grep -E ':80|:443'

Step 1 安装 certbot。 发行版仓库版本通常滞后,追求新特性(如 --preferred-profile、ECDSA 默认化)建议用 snap:

# 方式 A:发行版仓库
apt update && apt install -y certbot python3-certbot-nginx

# 方式 B:snap(推荐,版本更新)
snap install core && snap refresh core
snap install --classic certbot
ln -sf /snap/bin/certbot /usr/bin/certbot

certbot --version

Step 2 先用 staging 演练,避免消耗正式配额。

certbot --nginx -d example.com -d www.example.com 
  --staging --non-interactive --agree-tos -m ops@example.com

# 演练完删掉 staging 证书,防止与正式证书同名冲突
certbot delete --cert-name example.com

Step 3 正式签发并强制跳转。

certbot --nginx -d example.com -d www.example.com 
  --non-interactive --agree-tos -m ops@example.com --redirect

注意 --non-interactive 必须同时给出 --agree-tos 与 -m,否则会直接退出。-m 用于证书到期提醒与账号找回,不要填无效邮箱。

Step 4 确认定时器真实存在。

systemctl list-timers certbot.timer --all
systemctl cat certbot.timer | grep -E 'OnCalendar|RandomizedDelaySec|Persistent'
systemctl enable --now certbot.timer

Step 5 演练续期。 --dry-run 走 staging 环境,不消耗正式配额:

certbot renew --dry-run

Step 6 查看状态。

certbot certificates

若输出 Certificate not yet due for renewal; no action taken.,说明证书还有 30 天以上有效期,属正常,不是错误。

四、配置示例:cli.ini、Nginx 与部署钩子

4.1 全局默认参数(/etc/letsencrypt/cli.ini),避免每次敲一长串参数:

# /etc/letsencrypt/cli.ini
email = ops@example.com
agree-tos = true
server = https://acme-v02.api.letsencrypt.org/directory
key-type = ecdsa
elliptic-curve = secp384r1
max-log-backups = 10
# webroot 模式才需要下面两行
# authenticator = webroot
# webroot-path = /var/www/certbot

key-type = ecdsa 与 rsa-key-size 互斥,不要同时设置;P-384 对 Windows 7、Android 4.x 等老客户端不友好,面向存量终端可改回 rsa-key-size = 3072。

4.2 Nginx 站点配置(webroot 模式,适用于不想让 certbot 改配置的场景):

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    # ACME HTTP-01 路径必须走明文,严禁 301
    location ^~ /.well-known/acme-challenge/ {
        root /var/www/certbot;
        default_type text/plain;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    http2 on;   # nginx >= 1.25.1;旧版写 listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_session_cache   shared:SSL:10m;
    ssl_stapling        off;   # Let's Encrypt 已停用 OCSP,开启无意义
}

必须使用 fullchain.pem 而非 cert.pem,否则缺少中间证书,Android 与 Java 客户端会报“证书链不完整”。

4.3 部署钩子(webroot/standalone 模式必需):

install -d /etc/letsencrypt/renewal-hooks/deploy
cat > /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh <<'EOF'
#!/bin/sh
nginx -t && systemctl reload nginx
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh

4.4 无 systemd 环境的 cron 兜底:

17 3 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx" >> /var/log/certbot-renew.log 2>&1

五、方案对比:选 certbot、acme.sh 还是商业证书

certbot 不是唯一选择。它依赖 Python 与 systemd,在国内解析商场景下明显吃亏;acme.sh 是纯 shell 实现,内置了大量国内 DNS API,是 80 端口被封时的首选。

维度 certbot acme.sh 商业 DV/OV 证书
费用 免费 免费 付费
默认有效期 90 天 90 天(Let’s Encrypt) 受 SC-081 约束,2026-03-15 起最长 200 天
运行时依赖 Python + systemd 纯 shell,无依赖 无
DNS API 覆盖 官方仅 Cloudflare/Route53/DO 等 阿里云、腾讯云、DNSPod 等内置 不涉及
自动续期机制 systemd timer 自带 cron 通常需手工导入
典型场景 常规 Linux + Nginx/Apache 国内解析商、容器、嵌入式 需 OV 组织校验或赔付条款

再看验证方式的选择,它决定了你能不能用通配符、要不要动防火墙:

挑战类型 依赖端口 支持通配符 典型场景
HTTP-01 80 否 公网可访问的常规站点
DNS-01 无 是 80 被封、通配符、内网、CDN 回源
TLS-ALPN-01 443 否 仅 443 可用(certbot 不内置)

实践建议:常规站点用 certbot + --nginx 最省事;解析商在国内、或需要 *.example.com 通配符时,直接换 acme.sh + DNS API,不要硬啃 certbot 的手工 DNS 模式,因为它无法全自动续期。EV 证书只在有明确合规审计或采购要求时才值得考虑,不要指望它带来地址栏收益。

六、常见错误排查(含真实报错)

1. Timeout during connect (likely firewall problem)
80 端口不通。依次检查:ss -lntp | grep :80 确认监听、ufw status 或 firewall-cmd --list-ports 确认本机防火墙、云厂商安全组确认入方向放行 80。

2. Invalid response from http://example.com/.well-known/acme-challenge/xxx: 404
验证路径被 rewrite、被 location 优先级更高的规则拦截,或 root 目录不对。手工验证:

mkdir -p /var/www/certbot/.well-known/acme-challenge
echo ok > /var/www/certbot/.well-known/acme-challenge/test
curl -v http://example.com/.well-known/acme-challenge/test

3. urn:ietf:params:acme:error:unauthorized :: The key authorization file from the server did not match this challenge
多台后端做了负载均衡,验证请求落到了没有该文件的另一台机器。解决办法是把 webroot 目录放到共享存储,或在每台机器上都执行签发。

4. too many failed authorizations recently
同一账号对同一主机名每小时失败 5 次的限流。不要继续重试,等满 1 小时,并改用 --staging 调试,确认通过后再切正式环境。

5. Problem binding to port 80: Could not bind to IPv4 or IPv6
standalone 模式下 Nginx 占着 80。改用 webroot,或临时 systemctl stop nginx 后签发再启动。

6. Client with the currently selected authenticator does not support any combination of challenges that will satisfy the CA
申请了通配符 -d '*.example.com',而通配符只能走 DNS-01。加 --preferred-challenges dns 并配置对应 DNS 插件。

7. nginx: [emerg] duplicate listen options for 0.0.0.0:443
certbot 的 nginx 插件与已有配置冲突,通常是两个 server 块重复写了 listen 443 ssl。清理重复项后重新执行,或改用 webroot 模式避免插件改配置。

所有失败的完整堆栈都在 /var/log/letsencrypt/letsencrypt.log,加 -v 或 -vvv 可提升日志级别。

七、效果验证:从本地到公网四层确认

第一层,本地签发状态:

certbot certificates
# 输出示例:Expiry Date: 2026-06-01 12:00:00+00:00 (VALID: 89 days)

第二层,线上实际下发的证书:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null 
  | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

重点核对 notAfter 是否与 certbot certificates 一致。若不一致,说明 Nginx 没有 reload,回到第四节配置 deploy-hook。

第三层,证书链完整性:

openssl s_client -connect example.com:443 -servername example.com -verify_return_error </dev/null >/dev/null 2>&1
echo $?   # 必须输出 0

第四层,剩余天数监控脚本,接入 Zabbix/Prometheus 的 textfile collector 或直接 cron 告警:

#!/bin/bash
end=$(openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null 
  | openssl x509 -noout -enddate | cut -d= -f2)
days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
echo "example.com 剩余 ${days} 天"
[ "$days" -lt 21 ] && exit 1

再确认定时器与历史执行记录:

systemctl list-timers certbot.timer
journalctl -u certbot.service -n 50 --no-pager

注意:openssl ocsp 检查现在会失败,因为 Let’s Encrypt 已于 2025 年停止提供 OCSP 响应,新证书不再包含 OCSP URL,改用 CRL 分发点(-ext crlDistributionPoints 可查看)。这不是证书故障,不要误判。

最后用外部视角复核一次:testssl.sh example.com 或 SSL Labs 在线检测,确认协议版本、加密套件、证书链均无告警。

常见问题

不会。–dry-run 强制使用 staging 环境,签发的是不受信任的测试证书,不计入正式环境的每域名每周 50 张限流。但 staging 自身也有限流,频繁演练仍可能被拒。演练后可执行 certbot delete –cert-name example.com 清理 staging 证书,避免与正式证书同名冲突。

能,但必须改用 DNS-01。certbot 官方 DNS 插件只覆盖 Cloudflare、Route53、DigitalOcean 等少数解析商,阿里云、腾讯云、DNSPod 需第三方插件,或直接改用内置 DNS API 的 acme.sh。纯手工模式 certbot certonly –manual –preferred-challenges dns 无法自动续期,不建议生产使用。

不需要。Let’s Encrypt 的 90 天始终低于 200 天与 100 天两档上限,签发与续期流程完全不变,只有 2029-03-15 进入 47 天阶段时才需同步缩短。certbot 默认在到期前 30 天续期,届时对 47 天证书意味着剩余 64% 就续,属正常,但必须保证每月至少成功续期一次。

取决于插件。用 –nginx 签发时,续期成功后 certbot 会自动 reload Nginx;用 webroot 或 standalone 模式则不会,必须配置 –deploy-hook,或在 /etc/letsencrypt/renewal-hooks/deploy/ 下放可执行脚本执行 nginx -t && systemctl reload nginx。否则证书已更新,进程仍在用旧证书。

不能。Chrome 自 77 起移除地址栏 EV 企业名展示,Firefox、Safari 后续跟进,现在地址栏只有锁标识,DV、OV、EV 在浏览器 UI 上无任何可见差异。EV 的剩余价值在于组织身份审核、合规要求与部分行业采购条款,不要再以“地址栏显示企业名”作为采购理由。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。