本文适用于已经通过 Nginx 对外提供 HTTPS 服务的网站。示例以以下占位符表示实际值,执行前必须替换:
- `example.com`:主域名
- `www.example.com`:附加域名
- `/etc/nginx/conf.d/example.conf`:网站的 Nginx 配置文件
本文采用 Nginx 插件完成域名验证和证书安装。正常切换只会平滑重载 Nginx,不需要停止网站。
1. 前置条件
配置前确认:
1. 主域名和所有附加域名的 DNS 记录均指向目标服务器公网 IP。
2. 云安全组和服务器防火墙允许公网访问 TCP 80、443 端口。
3. Nginx 已为这些域名配置正确的 `server_name`。
4. Nginx 当前配置可以通过语法检查。
5. 使用 root 用户或具有 sudo 权限的用户操作。
getent ahostsv4 example.com www.example.com curl -I http://example.com/ nginx -t systemctl is-active nginx如果域名解析结果不是当前服务器公网 IP,先修正 DNS,等待解析生效后再申请证书。
2. 备份现有配置和证书
如果网站已经使用其他证书,先备份 Nginx 配置和证书。不要删除原证书,以便快速回退。
backup_dir="/root/tls-backup-$(date +%Y%m%d-%H%M%S)" mkdir -p "$backup_dir" cp -a /etc/nginx/conf.d/example.conf "$backup_dir/" cp -a /path/to/current/fullchain.pem "$backup_dir/" cp -a /path/to/current/private.key "$backup_dir/" chmod 700 "$backup_dir"私钥属于敏感文件,不要通过聊天、邮件或未加密网盘传输。
3. 安装 Certbot 和 Nginx 插件
Alibaba Cloud Linux 3、Rocky Linux 8、AlmaLinux 8、RHEL 8 等系统:
dnf install -y certbot python3-certbot-nginxDebian、Ubuntu:
apt update apt install -y certbot python3-certbot-nginx确认安装结果:
certbot --version4. 使用测试环境验证签发流程
先使用 Let’s Encrypt 测试环境验证域名、80 端口和 Nginx 配置。测试证书不受浏览器信任,不要把它安装到线上网站。
certbot certonly \ --nginx \ --staging \ --cert-name example.com \ -d example.com \ -d www.example.com \ --non-interactive \ --agree-tos \ --register-unsafely-without-email成功后会生成:
/etc/letsencrypt/live/example.com/fullchain.pem /etc/letsencrypt/live/example.com/privkey.pem此时证书仍为测试证书。可以用下面的命令确认:
certbot certificates测试环境通常会显示 `INVALID: TEST_CERT`,这是预期结果。
5. 申请正式证书
使用同一个证书名称申请正式证书:
certbot certonly \ --nginx \ --server https://acme-v02.api.letsencrypt.org/directory \ --force-renewal \ --cert-name example.com \ -d example.com \ -d www.example.com \ --non-interactive \ --agree-tos \ --register-unsafely-without-email正式申请成功后,再安装到 Nginx:
certbot install --cert-name example.com --nginx --non-interactiveCertbot 会把 Nginx 的证书路径改为类似以下内容:
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;检查配置并确认网站正常:
nginx -t systemctl is-active nginx curl -I https://example.com/ curl -I https://www.example.com/6. 启用自动续签定时器
在 Alibaba Cloud Linux 3 / RHEL 8 系软件包中,定时器通常名为 `certbot-renew.timer`:
systemctl enable --now certbot-renew.timer systemctl is-enabled certbot-renew.timer systemctl is-active certbot-renew.timer systemctl list-timers --all | grep certbot在 Debian、Ubuntu 上通常名为 `certbot.timer`:
systemctl enable --now certbot.timer systemctl is-enabled certbot.timer systemctl is-active certbot.timer systemctl list-timers --all | grep certbot只启用实际存在的那个定时器。可先查看:
systemctl list-unit-files | grep -E 'certbot(-renew)?\.timer'不需要再额外添加 cron。如果同时配置 systemd timer 和 cron,可能造成重复执行。
7. 模拟完整续签
必须执行一次续签模拟:
certbot renew --dry-run成功时应看到类似信息:
Congratulations, all simulated renewals succeeded`--dry-run` 使用测试环境,不会把线上正式证书替换成测试证书。
8. 最终验证
检查 Certbot 记录:
certbot certificates检查 Nginx 当前实际提供的证书:
openssl s_client -connect 127.0.0.1:443 -servername example.com </dev/null 2>/dev/null \ | openssl x509 -noout -subject -issuer -dates -ext subjectAltName检查公网访问:
curl -I https://example.com/ curl -I https://www.example.com/证书正常时需要同时满足:
- 颁发机构显示 Let’s Encrypt。
- `subjectAltName` 包含所有申请的域名。
- 到期时间约为签发后的 90 天。
- 网站返回正常的 HTTP 状态码。
- Certbot 定时器为 `active`。
- `certbot renew --dry-run` 成功。
9. 自动续签的工作方式
Let’s Encrypt 证书有效期通常为 90 天。Certbot 定时器会定期检查证书,并在接近到期时续签。使用 Nginx 安装器时,成功续签后会更新 `/etc/letsencrypt/live/` 下的链接并重新加载 Nginx。
不要手工复制 `/etc/letsencrypt/live/` 中的证书到其他固定路径,否则续签后 Nginx 可能继续读取旧副本。让 Nginx 直接引用 Certbot 管理的路径。
### 到期提醒说明
Let’s Encrypt 已于 2025 年 6 月 4 日停止证书到期提醒邮件。通过 ACME/Certbot 提供邮箱地址,也不能作为证书到期或续签失败的通知方式。官方说明:<https://letsencrypt.org/2025/06/26/expiration-notification-service-has-ended>
如需提醒,应另外配置独立监控,例如:
- 使用 HTTPS/SSL 监控服务检查证书剩余天数。
- 使用服务器监控系统检查 `certbot-renew.service` 的失败状态。
- 配置自己的 SMTP、短信或即时通信告警,在续签失败时发送通知。
自动续签本身不依赖邮箱;应以 systemd 定时器状态和 `certbot renew --dry-run` 的结果为准。
10. 常见故障
域名验证失败
检查:
getent ahostsv4 example.com www.example.com ss -ltnp | grep -E ':80|:443' curl -I http://example.com/.well-known/acme-challenge/test常见原因包括 DNS 尚未生效、域名指向其他服务器、云安全组未开放 80 端口、CDN 回源错误、Nginx 中存在重复的 `server_name`。
HTTP 返回 301 跳转到 HTTPS 通常可以工作。若验证仍失败,检查 Nginx 是否将 `/.well-known/acme-challenge/` 请求转发给了应用并返回 404。
正式证书申请次数受限
调试期间始终先使用 `--staging`。只有测试签发成功后才申请正式证书,避免触发 Let’s Encrypt 频率限制。
### 定时器存在但没有运行
systemctl enable --now certbot-renew.timer # 或 Debian/Ubuntu 上: systemctl enable --now certbot.timer查看执行日志:
journalctl -u certbot-renew.service --since today # 或: journalctl -u certbot.service --since today查看 Certbot 日志:
tail -n 100 /var/log/letsencrypt/letsencrypt.logNginx 切换后无法启动
先执行:
nginx -t journalctl -u nginx -n 100 --no-pager不要在 `nginx -t` 失败时重启 Nginx。
11. 回滚到旧证书
如果切换后出现问题,将备份的 Nginx 配置恢复:
cp -a /root/tls-backup-日期时间/example.conf /etc/nginx/conf.d/example.conf nginx -t systemctl reload nginx恢复后使用 `openssl s_client` 和 `curl` 再次验证。旧证书文件应继续保留到新证书经过至少一次自动续签后。
12. 日常维护建议
每月检查一次以下命令即可:
certbot certificates systemctl list-timers --all | grep certbot systemctl is-active nginx系统升级时同步更新 Certbot:
dnf update certbot python3-certbot-nginx # 或 Debian/Ubuntu: apt update apt install --only-upgrade -y certbot python3-certbot-nginx不要依赖 Let’s Encrypt 邮件判断证书是否续签。建议由独立监控系统至少每天从公网检查一次证书到期时间,并在剩余时间低于 30 天时告警。