用途:Certbot 是 Let's Encrypt 的 ACME 协议客户端,用于获取、续期和吊销 TLS 证书,适合为站点启用 HTTPS 与证书生命周期管理。
主要能力:支持多种验证方式——nginx 插件(sudo certbot --nginx -d example.com -d www.example.com)、Apache 插件(sudo certbot --apache -d example.com)、standalone(sudo certbot certonly --standalone -d example.com)、webroot(sudo certbot certonly --webroot -w /var/www/html -d example.com)以及 DNS-01(通配符仅此方式,sudo certbot certonly --manual --preferred-challenges dns -d '*.example.com' -d example.com)。常用操作包括续期(sudo certbot renew,可加 --dry-run 预演)、查看列表(sudo certbot certificates)、吊销(sudo certbot revoke --cert-path ...)、删除(sudo certbot delete --cert-name ...)和扩展域名(--expand)。
状态与文件:证书位于 /etc/letsencrypt/live/<domain>/(cert.pem、chain.pem、fullchain.pem、privkey.pem,均为指向 archive/ 的符号链接);续期配置在 /etc/letsencrypt/renewal/<domain>.conf;账户在 /etc/letsencrypt/accounts/;日志在 /var/log/letsencrypt/letsencrypt.log。自动续期依赖 certbot.timer(systemd)或 /etc/cron.d/certbot。安装可选 snap install --classic certbot(推荐)、apt/dnf 安装或 pip install certbot。
适用场景与限制:HTTP-01 需 80 端口对公网可达,NAT、企业防火墙或无公网 IP 的环境需改用 DNS-01;通配符证书只能用 DNS-01。存在速率限制:每个注册域名每周 50 张证书,相同域名集合每周 5 张重复证书,测试应使用 --staging(不计入配额但浏览器不信任)。renew 每日运行两次,仅对剩余不足 30 天的证书续期,因此新证书约 60 天内不会续期;--force-renewal 仍计入配额。默认不配置 deploy hook,续期后 Web 服务器不会自动重载,需在 /etc/letsencrypt/renewal-hooks/deploy/ 放置可执行脚本(如 chmod +x reload-nginx.sh)。Web 服务器配置应引用 live/ 路径而非 archive/。
其他故障排查要点:端口 80 被 nginx/Apache 占用时应改用对应插件或先停止服务;DNS 未生效或记录错误会报 NXDOMAIN;多个 certbot 安装(snap 与系统包)可能冲突;插件缺失需安装 python3-certbot-nginx 等。