温馨提示×

Debian Nginx SSL如何自动续期

小樊
41
2026-08-18 05:32:40
栏目: 云计算

在 Debian 上使用 Nginx + SSL最常见、最省心的自动续期方案是 Let’s Encrypt + Certbot。下面给你一套从安装到自动续期的完整做法,适用于大多数场景。


一、环境说明

  • 系统:Debian 10 / 11 / 12
  • Web 服务器:Nginx
  • SSL 证书:Let’s Encrypt
  • 自动续期:Certbot + systemd timer(默认自带)

二、安装 Certbot(推荐方式)

1️⃣ 更新软件源

sudo apt update

2️⃣ 安装 Certbot 和 Nginx 插件

sudo apt install certbot python3-certbot-nginx -y

三、申请 SSL 证书(自动配置 Nginx)

1️⃣ 申请证书(推荐)

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

替换 example.com 为你自己的域名

✅ Certbot 会:

  • 自动验证域名
  • 申请 Let’s Encrypt 证书
  • 自动修改 Nginx 配置
  • 自动启用 HTTPS

2️⃣ 证书申请成功提示

Congratulations! You have successfully enabled HTTPS.

四、自动续期(重点)

✅ Certbot 默认已经自动续期

Debian 安装后,Certbot 会自动创建:

systemctl status certbot.timer

应该看到:

Active: active (waiting)

查看下一次执行时间

systemctl list-timers | grep certbot

五、手动测试自动续期(强烈建议)

模拟续期(不真正更新)

sudo certbot renew --dry-run

✅ 没有报错说明自动续期配置正确


六、证书存储位置

证书通常位于:

/etc/letsencrypt/live/example.com/

包含:

cert.pem
chain.pem
fullchain.pem
privkey.pem

Nginx 示例配置:

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

七、Nginx 重载(Certbot 会自动处理)

Certbot 会在续期成功后自动执行:

nginx -s reload

如果你手动修改了证书,可以执行:

sudo nginx -t
sudo systemctl reload nginx

八、常见问题排查

❌ 443 端口未开放

sudo ufw allow 443/tcp
sudo ufw allow 80/tcp

❌ 证书续期失败

查看日志:

sudo journalctl -u certbot

或:

sudo cat /var/log/letsencrypt/letsencrypt.log

九、非 80 端口 / 特殊情况(可选)

DNS 验证(适合 CDN / 无 80 端口)

sudo certbot certonly --manual --preferred-challenges dns -d example.com

⚠️ 需手动添加 TXT DNS 记录(不方便自动续期)


十、总结(最简流程)

apt update
apt install certbot python3-certbot-nginx
certbot --nginx -d example.com
certbot renew --dry-run

证书 90 天有效期
自动续期无需人工干预


如果你愿意,我可以:

  • ✅ 帮你检查 当前 Nginx 配置
  • ✅ 给你一套 生产级 HTTPS 安全配置
  • ✅ 指导 CDN / 国内服务器 的特殊处理

直接把你的 系统版本 + 域名 + Nginx 配置 发我即可。

0