跳到主要内容
证书选购指南

certbot 申请免费证书全流程:6 步签发、3 种验证方式对比与自动续期

certbot 申请免费证书 — certbot 申请免费证书全流程:6 步签发、3 种验证方式对比与自动续期
核心结论

certbot 申请免费证书最短只需一条命令:sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.example.com --email ops@example.com --agree-tos,Let's Encrypt 签发的 DV 证书有效期 90 天、费用 0 元,必须在到期前 30 天由 timer 自动续期。 验证方式决定成败:80 端口可对外用 webroot(http-01);服务器在内网或要通配符 *.example.com 只能用 dns-01;certbot 临时占用 80 端口时用 standalone,需先 systemctl stop nginx。 certbot 2.0.0 起默认 --key-type ecdsa(曲线 secp256r1),要兼容 Android 6 以下等老设备时加 --key-type rsa --rsa-key-size 2048;调试一律加 --staging,避免触发每注册域名 50 张/周与失败验证 5 次/小时的上限。 续期靠 certbot.timer:systemctl list-timers | grep certbot 可看到每天两次、带最长 12 小时随机延迟的任务,改完配置用 certbot renew --dry-run 验证,--deploy-hook 'systemctl reload nginx' 只在真正续期成功时触发。 验证是否生效:echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -issuer -dates,输出 issuer 为 Let's Encrypt R11/E5 且日期为新证书日期即成功。

一、为什么 certbot 申请免费证书成了默认选项?背景与根本原因

因为 ACME 协议把”验证—签发—部署”三段全部变成了可脚本化的 HTTP 交互。2015 年 12 月 Let’s Encrypt 进入公开测试,2016 年 4 月正式 GA,用 RFC 8555 定义的 ACME 协议签发 DV(域名验证型)证书:单张有效期 90 天、费用 0 元。免费本身不是关键,可自动化才是——如果没有 ACME,90 天一轮的手工换证会直接把运维成本推高数倍。所以 certbot 申请免费证书能成为默认选项,是”协议标准化 + 客户端成熟 + 短周期倒逼自动化”三者叠加的结果,而不是单纯的价格因素。

certbot 是 EFF 维护的参考级 ACME 客户端,2025 年主流版本为 3.x(如 3.1.0,要求 Python 3.9+),通过 apt、dnf、snap、pip 四种渠道分发。先确认你手上的版本,因为 2.0.0 是行为分水岭:低于该版本默认仍签发 RSA 2048,2.0.0 起默认改为 ECDSA P-256。

# 1) 确认版本:3.x 为当前主流分支
certbot --version
# 典型输出:certbot 3.1.0

# 2) 查看密钥类型默认值(2.0.0+ 默认为 ecdsa;需要兼容老客户端时显式指定)
certbot certonly --help all | grep -E -- '--key-type|--rsa-key-size'
# --key-type {ecdsa,rsa}   (default: ecdsa)
# --rsa-key-size N         (default: 2048)

90 天不是”免费版限制”,而是安全取舍。短周期把私钥泄露、错误签发、吊销(CRL/OCSP)的暴露窗口压到季度级;代价是必须自动化。据 Let’s Encrypt 文档,官方建议在到期前 30 天续期,certbot 的 renew 逻辑默认就是这个阈值——只要 cron 或 systemd timer 每天跑一次 certbot renew,就天然满足。certbot ≥ 2.3.0 可用 --renew-before-expiration 调整这个天数(单位:天,默认 30):

# 演练续期并临时把阈值改为 20 天(仅本次生效,不写配置)
certbot renew --dry-run --renew-before-expiration 20
# --dry-run 使用 staging 环境,不消耗正式环境速率限制

成本对比:DV 层级上,免费与付费的加密强度没有差别。Let’s Encrypt 与商业 DV 同为 P-256 ECDSA 或 RSA 2048,差异集中在赔付条款、SLA 和组织信息校验上。据 CA/Browser Forum 已通过的 SC-081 分阶段方案,证书最长有效期将从 398 天逐步压缩到 2029 年的 47 天,手动换证的边际成本只会继续上升。

维度 Let’s Encrypt DV 商业 DV 商业 OV
年费 0 元 约 50–500 元 约 800–3000 元
单张有效期 90 天 通常 1 年(受 CA/B 新规压缩) 通常 1 年(受 CA/B 新规压缩)
密钥强度 ECDSA P-256 / RSA 2048 同级 同级
组织信息校验 无 无 有(营业执照等)
保修赔付 无 通常 1 万–50 万美元 更高额度
技术支持 SLA 社区 / 文档 工单 工单 + 电话
自动化程度 ACME 全自动 部分支持 ACME 多为手动

换句话说,只有当合规、赔付或品牌信任需要”组织已验证”时,付费证书才产生真实溢价;纯加密与身份绑定到域名的场景,certbot 申请免费证书加 systemd timer 的组合在中长期仍是性价比最高的路径。

签发的第一步不是敲 certbot,而是确认”Let’s Encrypt 能不能找到你”。原因很直接:据 Let’s Encrypt 官方速率限制文档,同一账号对同一主机名的失败验证限制为每小时 5 次,超限后该主机名会被锁定 1 小时,期间即使配置改对了也签不出来。所以先花两分钟做完下面 5 项自检,比反复重试划算得多。

1)域名解析是否双向正确?校验器会优先尝试 AAAA 记录。如果域名只配了 AAAA 而服务器根本没监听 IPv6,http-01 会直接失败——这是新手最常踩的坑,临时方案是先删掉 AAAA 记录,等 A 记录生效后再签发。

# 两条都必须返回本机公网 IP,缺一不可
dig +short A    example.com
dig +short AAAA example.com
# 对照本机真实出口 IP(IPv4),确认 A 记录没写错
curl -4 -s https://ifconfig.me; echo

2)80/443 是否真的在监听、真的放行?本机监听、云安全组、主机防火墙是三层独立的门,任何一层没开都白搭。另外,Let’s Encrypt 的 http-01 校验器会跟随最多 10 次重定向,所以 301 跳到 443 是允许的,但跳转后的站点必须仍能返回 /.well-known/acme-challenge/ 下的文件,否则同样报 Invalid response from ...: 404。

ss -lntp | grep -E ':80|:443'          # 期望看到 nginx/apache 监听 0.0.0.0:80 与 :443
sudo ufw status verbose                # Ubuntu:需含 80/tcp ALLOW、443/tcp ALLOW
sudo firewall-cmd --list-ports         # RHEL/Rocky:需含 80/tcp 443/tcp
# 云厂商(阿里云/腾讯云/AWS)安全组入方向同样要放行 80、443

3)系统时间准不准?ACME 协议(RFC 8555)用 nonce 做防重放,服务端对时间敏感,偏差超过 5 分钟就会收到 JWS has an invalid anti-replay nonce,看起来像证书问题,其实是时钟问题。

timedatectl status | grep -i synchronized   # 必须为 yes
sudo timedatectl set-ntp true               # 为 no 时开启 NTP(systemd-timesyncd)
chronyc tracking                            # 已装 chrony 时,关注 System time 偏移量

4)客户端怎么装?三条路径按发行版选,不要混装(apt 版与 snap 版共存会出现两个 /etc/letsencrypt 认知不一致的问题):

# Debian 12 / Ubuntu 22.04 / 24.04
sudo apt update && sudo apt install -y certbot python3-certbot-nginx
# RHEL 9 / Rocky 9 / AlmaLinux 9(certbot 在 EPEL 中)
sudo dnf install -y epel-release && sudo dnf install -y certbot python3-certbot-nginx
# 官方推荐:snap 包版本最新、自动升级
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
certbot --version   # 建议 ≥ 2.x,1.x 已不再收到新特性

5)目录结构搞清楚,迁移才不会断链。/etc/letsencrypt/live/<domain>/ 里全是软链接,真正文件在 archive/,续期参数在 renewal/<domain>.conf,日志在 /var/log/letsencrypt/letsencrypt.log。只拷 live 目录,软链会指向不存在的 ../../archive/,certbot renew 直接报 Failed to find .../fullchain.pem。迁移一律整目录打包:

ls -l /etc/letsencrypt/live/example.com/     # 确认 fullchain.pem -> ../../archive/...
sudo tar czf le-backup-$(date +%F).tgz /etc/letsencrypt   # 换机时整包迁移
自检项 通过标准 失败时的典型报错
DNS A/AAAA 均指向本机公网 IP,或无 AAAA Timeout during connect (likely firewall problem)
80/443 端口 ss 有监听 + 安全组与防火墙放行 Invalid response from ...: 404
时间同步 System clock synchronized: yes,偏差 < 5 分钟 JWS has an invalid anti-replay nonce
客户端版本 certbot --version ≥ 2.x,来源唯一 unrecognized arguments / 插件缺失
目录与备份 live 软链可解析,renewal 配置存在 Failed to find .../fullchain.pem

全部通过后,别急着正式签发——先跑一次 sudo certbot certonly --webroot -w /var/www/html -d example.com --dry-run。它走的是 staging 环境,不占用生产速率配额,能在 10 秒内告诉你 http-01 到底通不通。

三、certbot 申请免费证书完整实操步骤(6 步)

步骤 1:确认版本与插件能力。不同版本的 certbot 参数差异很大,--key-type 从 certbot 2.0 起可用,且默认值已从 RSA 2048 改为 ECDSA(secp256r1)。先确认版本和已安装的验证/安装插件,再决定后续用哪种方式验证域名所有权。

# 版本 >= 2.0 才支持 --key-type;3.x 输出形如 certbot 3.1.0
sudo certbot --version

# 列出可用插件,决定用 nginx / apache / webroot / standalone / dns-*
sudo certbot plugins
# 典型输出:* nginx  * standalone  * webroot  * dns-cloudflare

步骤 2:用 –staging 先演练。Let’s Encrypt 文档明确写着 Failed Validation 限流为“每账号、每主机名、每小时 5 次失败”,一旦超过限制,就要等待一小时才能重试。因此首次配置务必走 staging 端点,它的失败计数与生产环境独立,不会消耗生产配额。参数含义:--agree-tos 同意 ACME 服务协议(RFC 8555 要求),--no-eff-email 表示不订阅 EFF 邮件,-w 指 webroot 目录,-d 可重复指定多个域名。

sudo certbot certonly --webroot -w /var/www/html 
  -d example.com -d www.example.com 
  --email ops@example.com --agree-tos --no-eff-email 
  --staging        # 关键:走 staging ACME 端点,成功会打印 The dry run was successful.

步骤 3:去掉 –staging 正式签发。命令完全同上,仅删除 --staging。成功时输出 Successfully received certificate,并给出 Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem 与 Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem。注意 live/ 下是软链,指向 ../../archive/ 中的带序号文件,所以续期后无需改配置。

维度 –staging 演练 正式签发
ACME 端点 acme-staging-v02.api.letsencrypt.org acme-v02.api.letsencrypt.org
浏览器是否信任 否,报 NET::ERR_CERT_AUTHORITY_INVALID 是
失败验证限流 独立计数,不占生产配额 5 次/账号/主机名/小时
证书有效期 90 天(测试用) 90 天,到期前 30 天可续

步骤 4:让 certbot 直接改 Web 服务器配置。已有 nginx 站点时,用 installer 插件比手工粘贴更不容易漏项:它会写入 ssl_certificate / ssl_certificate_key、把 listen 80 改成 listen 443 ssl 并追加 301 跳转。Apache 环境把 --nginx 换成 --apache。

sudo certbot --nginx -d example.com -d www.example.com --redirect
# 自动生成的等价配置片段:
#   ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
#   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
#   return 301 https://$host$request_uri;

步骤 5:重载并自检。改配置后先语法检查再重载,避免直接 reload 把线上打挂:

sudo nginx -t && sudo systemctl reload nginx

# 把重载动作交给 certbot,每次续期成功后自动执行
sudo certbot renew --deploy-hook "systemctl reload nginx"

步骤 6:验证续期链路。据 Let’s Encrypt 文档,官方推荐在部署完成后立刻跑一次 dry-run,它只模拟签发、不写真实证书:

sudo certbot renew --dry-run
# 期望输出:Congratulations, all simulated renewals succeeded
systemctl list-timers | grep certbot   # 应看到 certbot.timer,默认每日两次随机延迟触发

常见卡点:webroot 模式下若报 Type: unauthorized ... Invalid response from http://example.com/.well-known/acme-challenge/xxx: 404,多半是 AAAA 记录指向了另一台未配置的机器,或 nginx 的 location 规则拦截了该路径——ACME 校验只认 HTTP/80 端口的明文响应,这也正是 http-01 与 dns-01、tls-alpn-01 三种验证方式的核心差别。

四、certbot 申请免费证书的验证命令与 Nginx TLS 配置示例

验证方式是整个 certbot 申请免费证书流程里最容易翻车的一步:命令本身不长,但目录、端口、DNS 记录任何一处对不上,ACME 服务器就会回一个 404 或 unauthorized。下面把三种验证方式的完整命令、失败时的真实报错,以及签发后的 Nginx TLS 配置一次给全。

webroot:不停服、最常用。前提是 -w 指向的目录与 nginx 中该 server 块的 root 完全一致(root /var/www/html; 对应 -w /var/www/html,不能写成子目录)。据 Let’s Encrypt 文档,HTTP-01 校验请求路径固定为 /.well-known/acme-challenge/<token>:

# certbot 1.x/2.x 通用;--deploy-hook 在证书真正续期成功后才会触发
sudo certbot certonly --webroot -w /var/www/html -d example.com 
  --deploy-hook 'systemctl reload nginx'
# 目录不匹配时的典型报错:
#   Type: unauthorized
#   Detail: Invalid response from http://example.com/.well-known/acme-challenge/xxx: 404

standalone:临时抢占 80 端口。适合一次性迁移、机器上还没跑 Web 服务的场景。certbot 会自行绑定 80 端口起一个临时服务,所以必须先停 nginx,验证完立刻拉起:

sudo systemctl stop nginx && 
sudo certbot certonly --standalone -d example.com --preferred-challenges http && 
sudo systemctl start nginx
# 后续自动续期若仍用 standalone,需在 renew 时自动停服:
#   certbot renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

dns-01:通配符证书的唯一可行路径。因为 *.example.com 无法用 HTTP-01 验证:

sudo certbot certonly --manual --preferred-challenges dns 
  -d example.com -d '*.example.com'
# 按提示在 DNS 商处添加 TXT 记录,值形如:
#   _acme-challenge.example.com. 300 IN TXT "8b1X...随机串"
dig +short TXT _acme-challenge.example.com   # 必须回显该串后再回车

手动模式无法自动续期,生产环境应换成 DNS 插件。以 Cloudflare 为例,API Token 权限只需 Zone:DNS:Edit,凭证文件必须 chmod 600,否则 certbot 会直接拒绝读取:

sudo apt install python3-certbot-dns-cloudflare
sudo install -d -m 700 /root/.secrets
printf 'dns_cloudflare_api_token = %sn' 'YOUR_TOKEN' | sudo tee /root/.secrets/cloudflare.ini
sudo chmod 600 /root/.secrets/cloudflare.ini
sudo certbot certonly --dns-cloudflare 
  --dns-cloudflare-credentials /root/.secrets/cloudflare.ini 
  --dns-cloudflare-propagation-seconds 30 
  -d example.com -d '*.example.com'
方式 需停服 支持通配符 可自动续期 典型失败原因
webroot 否 否 是 -w 与 root 不一致 → 404
standalone 是(80 端口) 否 需配 pre/post-hook 80 被 nginx 占用 → Address already in use
dns-01(手动) 否 是 否 TXT 未生效或填错主机名
dns-01(插件) 否 是 是 Token 权限不足、凭证文件权限过宽

签发后的 Nginx TLS 片段。加密套件直接取 Mozilla SSL Configuration Generator 生成的 intermediate 档(兼容 Firefox 27、Chrome 31、Android 4.4.2 以上),TLS 1.3 的套件由 OpenSSL 自动协商,无需写入 ssl_ciphers:

server {
    listen 443 ssl;
    http2 on;                       # nginx 1.25.1+ 语法;旧版用 listen 443 ssl http2;
    server_name example.com;

    # 必须是 fullchain.pem,不能是 cert.pem
    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_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;   # 10MB 约可缓存 40000 个会话
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # HSTS 分两阶段上线,先 300 秒观察
    add_header Strict-Transport-Security "max-age=300" always;
}

fullchain.pem 与 cert.pem 的差别就是中间证书。缺了它,桌面 Chrome 往往因 AIA 补链而“看起来正常”,但部分 Android 与 Java 客户端会报 PKIX path building failed。用 OpenSSL 验证链完整性比看浏览器更可靠:

openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt 
  -untrusted /etc/letsencrypt/live/example.com/chain.pem 
  /etc/letsencrypt/live/example.com/cert.pem
# 期望输出:cert.pem: OK

HSTS 遵循 RFC 6797,一旦下发 max-age=31536000,浏览器会强制 HTTPS 一年且无法撤销。所以先跑 max-age=300 观察 24 小时,确认所有子域都能走 HTTPS,再改成 max-age=31536000; includeSubDomains。

OCSP stapling 还要不要配?据 Let’s Encrypt 2025 年公告,OCSP 响应服务已分阶段下线,新签发证书不再包含 OCSP URL。这意味着 ssl_stapling on; 保留也无害(对无 OCSP URL 的证书 nginx 会静默跳过),但绝不能再把它当作可用性依赖。证书健康度检查应改为 openssl verify 加 testssl.sh --severity MEDIUM https://example.com,关注链完整性、协议版本与套件强度。

五、certbot 申请免费证书方案选型:4 类验证方式与客户端对比表

验证方式选错,是 certbot 申请免费证书最常见的返工原因。先看下面这张按关键维度对比的表,再决定用哪个插件。

验证方式 是否需停 Web 服务 通配符 端口要求 自动化难度 典型场景
--webroot 否 不支持 需 80 可访问 低 已有 Nginx/Apache 的站点
--standalone 是(占用 80) 不支持 独占 80 低 无 Web 服务的纯 API 机
--nginx / --apache 否 不支持 开放 80/443 最低 标准 LAMP/LEMP 单机
--dns-*(dns-01) 否 支持 *.example.com 仅需 DNS API 权限 中(需配置凭据) 内网机、泛域名
# webroot:不停服务,challenge 文件落在站点根目录
certbot certonly --webroot -w /var/www/html 
  -d example.com -d www.example.com 
  --deploy-hook "systemctl reload nginx"   # 续期成功后自动重载

# standalone:80 被占用会直接失败,需用 hook 腾出端口
certbot certonly --standalone -d api.example.com 
  --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

# dns-01 泛域名(以 Cloudflare 插件为例,需 apt install python3-certbot-dns-cloudflare)
certbot certonly --dns-cloudflare 
  --dns-cloudflare-credentials /root/.secrets/cf.ini 
  --dns-cloudflare-propagation-seconds 30 
  -d example.com -d '*.example.com'

webroot 最常见的失败输出是 Type: unauthorized ... Invalid response from http://example.com/.well-known/acme-challenge/xxx: 404,根因几乎都是 -w 指向的目录与实际 root 不一致;standalone 则报 Problem binding to port 80: Could not bind to IPv4 or IPv6。

密钥类型怎么选?据 certbot 官方 changelog,2.0.0 起 --key-type 默认值已从 rsa 改为 ecdsa(曲线 secp256r1),ECDSA P-256 证书体积约 1.2 KB,握手与签名开销明显低于 RSA 2048(约 2 KB)。仅在需要兼容老旧客户端或硬件设备时改用 --key-type rsa --rsa-key-size 2048;4096 位除合规硬性要求外不建议,TLS 握手与 OCSP 签名耗时都会上升。

客户端 维护方 依赖 优势
certbot EFF Python 插件生态最全、文档最完整
acme.sh 社区 纯 Shell 无 Python,适合 Alpine / OpenWrt
Caddy Caddy Go 二进制 内置自动 HTTPS,零配置
Traefik Traefik Labs Go 二进制 容器 label 驱动,动态发现
CA 费用 有效期 速率限制 / 备注
Let’s Encrypt 免费 90 天 每注册域名 50 张/周
ZeroSSL 免费 90 天;付费约 $10/月起 90 天 ACME 与 REST API 双通道
Buypass Go 免费 180 天 支持 ACME,续期频次更低

国内云厂商的免费 DV 证书多为 3 个月有效期且需控制台手动申请,无法接入 ACME 自动续期,自动化程度低于用 certbot 申请免费证书。单机 Nginx 优先 certbot;容器集群优先 Traefik 或 Caddy。

部署方式怎么定?--nginx 最省事,但会重写 server 块、插入 managed by Certbot 注释,不利于配置进 Git 版本管理;certonly + 手写 ssl_certificate 可控性最好。容器环境推荐 --webroot 并把 /var/www/certbot 挂成共享 volume,由一个 sidecar 容器定时执行 certbot renew --webroot -w /var/www/certbot,续期后通过共享信号或 --deploy-hook 触发 Nginx reload。续期逻辑本身见后续章节。

六、certbot 申请免费证书常见报错与排查(附报错原文)

六类高频报错可以按“证书签发链路”分层定位:端口占用 → HTTP 挑战回源 → DNS 解析 → CAA 授权 → CA 限流 → 挑战类型不匹配。先看总览表,再按报错原文逐条处理。certbot 2.x(官方推荐 snap 安装)与 1.x 的报错文案基本一致,参数默认值也相同。

报错关键词 根因 首查命令 修复方向
Problem binding to port 80 80 被 nginx/apache 占用 ss -lntp | grep :80 停服务走 standalone,或改 webroot
Invalid response … 404 webroot 与站点 root 不一致 / 重写规则吞掉路径 curl -I http://域名/.well-known/acme-challenge/test 加 location ^~ 白名单
DNS problem: NXDOMAIN 记录未生效或只配了 AAAA dig @权威NS 域名 A 补 A 记录,等 TTL
…acme:error:caa CAA 未放行 letsencrypt.org dig CAA 域名 补 0 issue "letsencrypt.org"
too many failed authorizations 触发失败上限 — 等 1 小时,期间用 --staging
does not support any combination of challenges 通配符只能用 dns-01 — 换 --manual 或 DNS 插件

为什么提示“Problem binding to port 80: Could not bind to IPv4 or IPv6”?因为 --standalone 会自己监听 80 端口做 http-01 校验,而 nginx 已占用。先定位 PID,再二选一:

ss -lntp | grep :80
# 输出示例:LISTEN 0 511 0.0.0.0:80 users:(("nginx",pid=1234,fd=6))

# 方案 A:短暂停服后 standalone 签发(证书路径 /etc/letsencrypt/live/域名/)
systemctl stop nginx
certbot certonly --standalone -d example.com -d www.example.com
systemctl start nginx

# 方案 B:不停服,用 webroot(-w 为站点根目录,必须与 nginx root 一致)
certbot certonly --webroot -w /var/www/html -d example.com

为什么报“Invalid response from http://example.com/.well-known/acme-challenge/xxxx: 404”?ACME 服务器回源取文件时,nginx 把请求交给了应用或伪静态规则。最省事的验证方式是在 server 块内加一条高优先级 location(^~ 前缀匹配优先于正则重写):

location ^~ /.well-known/acme-challenge/ {
    root /var/www/html;   # 与 certbot -w 保持一致
    default_type text/plain;
}
# 重载并本地自测,返回 200 即通
nginx -t && systemctl reload nginx
curl -I http://example.com/.well-known/acme-challenge/test

NXDOMAIN 到底该怪谁?直接问权威 NS,绕开本地缓存:dig @ns1.example.com example.com A。有 ANSWER 说明是递归缓存未过期;NOANSWER 而 AAAA 有值,说明你只配了 IPv6,而 Let’s Encrypt 优先走 IPv4。新域名按 TTL(常见 300–3600 秒)等待即可。

CAA 报错怎么解?据 RFC 8659,CA 签发前必须校验 CAA。执行 dig CAA example.com,若返回了其他 CA 的 issue 记录却无 letsencrypt.org,删除该记录或补一条:

example.com. 300 IN CAA 0 issue "letsencrypt.org"

被限流了要等多久?据 Let’s Encrypt 官方 Rate Limits 文档,Failed Validation 上限为每账号、每主机名 5 次/小时,1 小时后自然恢复。调试期务必加 --staging(指向 staging 环境,不消耗生产配额),确认通过后再去掉该参数正式签发。

为什么申请 *.example.com 时提示不支持任何挑战组合?因为通配符域名只允许 dns-01 校验(RFC 8555),--nginx 与 --webroot 只能做 http-01。改用 certbot certonly --manual --preferred-challenges dns -d "*.example.com" 手动加 TXT 记录,或安装 python3-certbot-dns-cloudflare 等 DNS 插件实现全自动续期。

七、certbot 申请免费证书之后:自动续期、hook 与到期监控

证书签下来只是开始。真正让线上服务掉链子的,往往不是签发失败,而是”续期任务没跑”或”续期跑了但 Web 服务没重新加载证书”。据 Certbot 官方文档(eff.org/certbot),certbot renew 默认只对剩余有效期不足 30 天的证书执行续期,其余证书直接跳过并输出 Certificate not yet due for renewal——所以每天跑两次是安全的,不会浪费 Let’s Encrypt 的速率配额。

问:用 systemd 还是 cron 来续期?官方推荐 systemd,因为 certbot 的 deb/rpm 包已自带 timer,无需手工配置。

# 查看 timer 定义(deb/rpm 包默认安装路径)
systemctl cat certbot.timer
# 输出关键行:
# [Timer]
# OnCalendar=*-*-* 00,12:00:00   # 每天 00:00 与 12:00 各一次
# RandomizedDelaySec=43200       # 叠加 0~12 小时随机延迟,避免全网同时请求

systemctl status certbot.timer     # 查看 Active 与 Trigger(下次触发时间)
systemctl list-timers certbot.timer
certbot renew --dry-run            # 演练:走 staging 环境验证续期与 hook,不消耗正式配额

RandomizedDelaySec 的价值在于把全球用户的续期请求摊平,避免整点洪峰触发 Let’s Encrypt 的限流。若用 cron,需自己写绝对路径——snap 安装的 certbot 在 /snap/bin/certbot,而 cron 的 PATH 通常只有 /usr/bin:/bin,写成裸 certbot 会得到 /bin/sh: 1: certbot: not found。

# crontab -e(root 用户)
0 3 * * * /usr/bin/certbot renew --quiet --deploy-hook 'systemctl reload nginx'
# snap 安装请改为 /snap/bin/certbot;--quiet 只输出错误,便于 cron 邮件告警
维度 systemd timer cron
触发精度 OnCalendar + 随机延迟,支持 Persistent=true 补跑 分钟级,关机期间错过的任务不补跑
日志 journalctl -u certbot.service 结构化可查 依赖 MAILTO / 重定向,默认静默
路径环境 由 unit 定义,稳定 PATH 精简,必须写绝对路径
适用版本 certbot ≥ 0.30 的 deb/rpm 包 任意版本,容器内常用

问:--deploy-hook 和 --post-hook 有什么区别?--deploy-hook 仅在本次真正发生了续期时执行,--post-hook 每次 renew 运行结束都执行(哪怕全部跳过)。因此 reload 服务应放 deploy-hook。重载方式也有讲究:nginx -s reload 是平滑的,旧 worker 处理完存量连接才退出,不会中断 TLS 会话;Apache 应使用 apachectl graceful 而非 restart。

问:怎么知道证书快到期了?续期窗口由 /etc/letsencrypt/renewal/<domain>.conf 里的 renew_before_expiry 字段决定(默认 30 天)。外部监控建议把告警阈值设为 21 天,留 9 天人工介入余量:

# 解析远端证书到期时间(Uptime Kuma 的 HTTP(s) Keyword/证书监控同理)
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -enddate
# 输出示例:notAfter=Sep 28 08:12:33 2025 GMT

# 剩余天数小于 21 时告警
end=$(echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
  | openssl x509 -noout -enddate | cut -d= -f2)
days=$(( ( $(date -d "$end" +%s) - $(date +%s) ) / 86400 ))
[ "$days" -lt 21 ] && echo "WARN: $days days left"

问:私钥泄露了怎么办?先吊销再删除再重签,顺序不能反:

certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem 
  --reason keycompromise
certbot delete --cert-name example.com
certbot certonly --nginx -d example.com -d www.example.com   # 重新签发

据 Let’s Encrypt 速率限制文档,同一注册域名(registered domain)每 7 天的 Duplicate Certificate 上限为 5 张;反复 delete + 重签试探会直接触发 too many certificates already issued。调试请一律用 --dry-run 或 --staging。

八、如何确认 certbot 申请免费证书已生效?5 条验证命令

certbot 打印出 “Congratulations! You have successfully enabled HTTPS” 只能说明 ACME 流程走完了,并不代表 Nginx/Apache 已 reload,更不代表线上 443 端口真的在发新证书。按下面 5 条命令由内到外查一遍,全部通过才算 certbot 申请免费证书 的部署闭环。据 Let’s Encrypt 文档,证书有效期固定 90 天,且自 2024 年链变更后默认签发链为 ISRG Root X1 → R10/R11(RSA)或 E5/E6(ECDSA),issuer 字段可据此核对。

  1. 本地清单:一次性确认所有证书状态与到期日。
    # 列出 /etc/letsencrypt/live 与 renewal 目录下全部证书
    sudo certbot certificates
    # 关注三行:
    #   Key Type: ECDSA
    #   Domains: example.com www.example.com
    #   Expiry Date: 2026-03-01 08:12:33+00:00 (VALID: 89 days)

    VALID 低于 30 天说明 systemd timer / cron 续期没跑起来;Key Type 与申请时 --key-type 不符(certbot 2.0 起默认 ecdsa)说明沿用了旧版配置。

  2. 文件层校验:确认 issuer 与有效期。
    sudo openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem 
      -noout -subject -issuer -dates
    # issuer=C = US, O = Let's Encrypt, CN = R11
    # notAfter=Mar  1 08:12:33 2026 GMT   ← 应约为签发后 90 天

    注意 cert.pem 只含叶子证书,完整链在 fullchain.pem;若 issuer 显示 “Fake LE Intermediate”,说明当前是 --staging 测试证书,浏览器必然报不受信任。

  3. 线上层校验:确认对外提供的正是新证书。
    echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
      | openssl x509 -noout -issuer -dates
    # 再单独看握手校验结果:
    echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null 
      | grep 'Verify return code'
    # Verify return code: 0 (ok)

    返回 21 (unable to verify the first certificate) 或 20 基本就是漏配中间证书——Nginx 的 ssl_certificate 应指向 fullchain.pem 而非 cert.pem。-servername 不可省,否则 SNI 站点可能拿到默认站点的证书。

  4. 跳转与 HSTS:验证 HTTP→HTTPS 闭环。
    curl -sI http://example.com | head -n 3
    # HTTP/1.1 301 Moved Permanently
    # Location: https://example.com/
    curl -sI https://example.com | grep -i strict-transport-security
    # strict-transport-security: max-age=31536000; includeSubDomains

    两条都通过,才算 certbot 申请免费证书 后的访问路径没有明文残留;max-age 建议先设 300 秒验证,再逐步升到 31536000。

  5. 协议与套件:确认无弱协议。
    # testssl.sh 3.2+,--fast 跳过耗时枚举
    ./testssl.sh --fast https://example.com
    # 期望:TLS 1.2 / TLS 1.3 offered;SSLv3、TLS1.0/1.1 not offered;
    #       3DES、RC4 不出现;Chain of Trust: OK

    浏览器开发者工具 Security 面板应显示 “Connection is secure”,证书签发者为 Let’s Encrypt。

核对 issuer 时可对照下表,避免把测试链或旧链当成正式证书:

证书类型 签发者 CN(2024 年后) openssl 输出特征
RSA(--key-type rsa) R10 / R11 Public Key Algorithm: rsaEncryption
ECDSA(certbot ≥ 2.0 默认) E5 / E6 id-ecPublicKey,256 bit
Staging 测试证书 Fake LE Intermediate 浏览器报 SEC_ERROR_UNKNOWN_ISSUER

第 3 条通过后还可顺手查一次 CT 日志收录与 OCSP 装订状态(openssl s_client -status),这是后续排查“证书已签发但客户端仍报错”类问题的入口。

证书本身 0 元,Let's Encrypt 是非营利 CA,不收签发费用。限制在配额与能力:单张有效期 90 天,每注册域名 50 张/周、重复证书 5 张/周、失败验证 5 次/小时/主机名,且只签发 DV 证书,不提供 OV/EV 与人工客服 SLA,出问题只能查文档与社区。

单机 Nginx/Apache 且服务器有 Python 3.9+,用 certbot 更省事,–nginx 插件能直接改配置,文档与报错信息最全。Alpine、OpenWrt、群晖等没有 Python 环境或追求零依赖的场景,acme.sh 的纯 Shell 实现更轻,安装包不到 100 KB,两者都走 ACME v2 协议,可随时互换。

这是触发了 Let's Encrypt 每账号每主机名 5 次/小时的失败验证上限,只能等待约 1 小时自然解除。等待期间把命令加上 –staging 对测试环境反复调试,确认 404、DNS、CAA 等问题都解决后,再对生产环境正式签发一次。

可以,走 dns-01 验证:certbot certonly –preferred-challenges dns 配合 DNS 服务商插件(如 certbot-dns-cloudflare、certbot-dns-aliyun),只要机器能出网访问 acme-v02.api.letsencrypt.org 的 443 端口即可,无需任何入向端口。插件凭证文件权限设 600,续期同样能全自动完成。

支持,但只能用 dns-01 验证,因为通配符无法通过 http-01 校验。命令形如 certbot certonly –manual –preferred-challenges dns -d example.com -d '*.example.com',注意通配符只覆盖一级子域,a.b.example.com 需要单独添加或再申请一张 *.b.example.com。

九成是 Web 服务器没有重载,执行 systemctl reload nginx 或 apachectl graceful 即可。其次检查 ssl_certificate 是否指向 fullchain.pem、是否有多份 server 块仍指向旧路径,以及前面是否有 CDN/SLB 缓存了旧证书,需要在云控制台同步更新。

纯加密与浏览器信任层面两者等价,都是 DV 级别、同样的密钥强度,不换也能正常跑 HTTPS。需要付费 DV/OV 的场景通常是:要求一年以上有效期以减少运维、需要商业保险赔付、需要证书内含组织名称供合规审计,或需要厂商工单支持。


本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。

内容审核

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