温馨提示×

debian nginxssl怎么自动续签

小樊
37
2026-09-11 14:52:58
栏目: 云计算

在 Debian 上让 Nginx + SSL 自动续签,最常用、最省事的做法是 使用 Let’s Encrypt 的 certbot

下面给你一套标准、稳定、可自动续签的方案。


一、使用 certbot(推荐)

1. 安装 certbot(Debian 10+)

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

2. 自动申请并配置 Nginx SSL

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

替换 example.com 为你自己的域名
证书会自动写入 Nginx 配置


3. 验证自动续签是否可用

sudo certbot renew --dry-run

如果没报错,说明续签机制正常。


4. 自动续签是怎么生效的?

certbot 安装后会自动创建:

  • ✅ systemd timer:certbot.timer
  • ✅ 每天检查一次证书是否快到期(<30天自动续)

查看状态:

systemctl status certbot.timer

二、手动 Nginx 配置(如果你已自己配 SSL)

如果你 不是用 --nginx 参数,而是手动配置:

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

那续签用:

sudo certbot renew

并在 Nginx 配置里加:

location /.well-known/acme-challenge/ {
    root /var/www/html;
}

三、证书到期自动重载 Nginx(重要)

certbot 默认会尝试 reload Nginx:

systemctl reload nginx

如果你想更稳妥,可以加 hook:

sudo certbot renew --nginx

或在:

/etc/letsencrypt/renewal/example.com.conf

确认有:

renew_hook = systemctl reload nginx

四、常见坑提醒

❌ 防火墙没开 80 端口(HTTP 验证必须)
❌ DNS 没解析到服务器
❌ 用了 CDN 但没关 SSL 代理
❌ 证书目录权限被改


五、如果你用的是 DNS 验证(无 80 端口)

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

这种方式不能全自动续签,需要 DNS API 插件(如 Cloudflare)。


总结(最简流程)

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

✅ 全自动续签
✅ Nginx 零维护


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

  • Debian 版本
  • 是否用 CDN / Cloudflare
  • 是否是内网 / 无公网 IP

我可以给你定制方案

0