Nginx 服务器 SSL 证书续签操作说明

一、适用环境

  • Web 服务器:Nginx
  • 证书工具:Certbot
  • 申请模式:standalone
  • 系统:Linux(Ubuntu / Debian / CentOS)

⚠ 说明:standalone 模式在续签时需要占用 80 端口,因此必须先停止 nginx。


二、查看当前证书状态

执行:

sudo certbot certificates

检查:

  • Certificate Name
  • Expiry Date
  • 是否显示 VALID
  • 是否接近过期(90 天有效期)

三、证书续签流程

步骤 1:停止 Nginx

sudo systemctl stop nginx

说明:

  • 释放 80 端口

  • 避免 certbot 报错:

    Could not bind TCP port 80

步骤 2:执行续签

续签所有证书:

sudo certbot renew

如果只续某一个证书:

sudo certbot renew --cert-name yourdomain.com

执行成功会看到:

Congratulations, all renewals succeeded

步骤 3:启动 Nginx

sudo systemctl start nginx

步骤 4:重新加载配置

sudo systemctl reload nginx

四、续签后验证

1. 再次查看证书有效期

sudo certbot certificates

确认:

  • Expiry Date 已更新
  • 显示 VALID
  • 有效期约 90 天

2. 检查 nginx 是否正常

sudo nginx -t

输出应为:

syntax is ok
test is successful

3. 检查线上证书

openssl s_client -connect yourdomain.com:443 -servername yourdomain.com \
| openssl x509 -noout -dates

确认:

  • notAfter 时间为最新日期

五、自动续签机制说明

Certbot 默认会安装 systemd 定时任务:

检查是否存在:

systemctl list-timers | grep certbot

如果存在:

certbot.timer

说明系统每天会自动尝试续签。

⚠ 但由于当前使用 standalone 模式,
如果 nginx 未停止,自动续签会失败。


六、注意事项

1. 80 端口必须空闲

如果续签时报错:

Could not bind TCP port 80

说明 nginx 未停止或有其他程序占用 80 端口。

排查命令:

sudo lsof -i :80

2. 多域名服务器说明

如果服务器有多个域名证书:

sudo certbot renew

会自动续签所有即将过期的证书。


七、优化建议(长期推荐)

当前使用 standalone 模式存在问题:

  • 必须停止 nginx
  • 自动续签可能失败
  • 存在服务中断风险

建议改为 nginx 插件模式:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

优点:

  • 不需要停止 nginx
  • 自动验证
  • 自动 reload
  • 更稳定

总结

当前续签流程:

sudo certbot certificates
sudo systemctl stop nginx
sudo certbot renew
sudo systemctl start nginx
sudo systemctl reload nginx

适用于 standalone 模式证书。

如服务器长期运行生产业务,建议迁移至 nginx 插件模式以提升稳定性。


作者:admin  创建时间:2026-02-26 10:12
最后编辑:admin  更新时间:2026-02-26 10:26