温馨提示×

Debian怎样自动续Nginx SSL

小樊
36
2026-10-17 08:33:29
栏目: 云计算

在 Debian 上让 Nginx 的 SSL 证书自动续期,最常见、最稳妥的方案是使用 Let’s Encrypt + Certbot。下面按步骤说明。


一、使用 Certbot 自动续期(推荐)

1. 安装 Certbot

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

2. 申请证书(如果还没申请)

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

Certbot 会自动:

  • 申请证书
  • 修改 Nginx 配置
  • 设置 HTTP → HTTPS 跳转

3. 测试自动续期

Certbot 安装后默认会创建 systemd timer:

sudo systemctl status certbot.timer

手动测试续期(不真正替换证书):

sudo certbot renew --dry-run

4. 自动续期原理

  • Debian 通过 certbot.timer 每天检查
  • 证书 < 30 天到期时自动续期
  • 续期后自动 reload Nginx

二、确认 Nginx 在续期后重载

Certbot 默认会在续期成功后执行:

systemctl reload nginx

如果你用的是非标准配置,可手动指定:

sudo certbot renew --nginx

或在 /etc/letsencrypt/renewal/example.com.conf 中确认:

renew_hook = systemctl reload nginx

三、手动方式(不推荐但可用)

如果你是用 acme.sh 或其他工具:

0 3 * * * /root/.acme.sh/acme.sh --renew -d example.com --nginx

四、常见问题

1. 证书没自动续?

检查:

sudo journalctl -u certbot.timer
sudo certbot certificates

2. 防火墙/80端口被封?

Let’s Encrypt 需要 80 端口

3. 通配符证书

需用 DNS 验证:

certbot certonly --manual --preferred-challenges=dns -d *.example.com

五、总结(最简方案)

✅ Debian + Nginx + Certbot

apt install certbot python3-certbot-nginx
certbot --nginx -d domain.com

无需额外配置,自动续期

如果你愿意,可以告诉我:

  • 你的 Debian 版本
  • Nginx 是否已存在
  • 是否是内网 / 云服务器

我可以给你更精确的命令。

0 踩