证书 47 天就过期?我在 EC2 上用 Certbot 搞定了自动续期

上周收到运维群里转发的消息:CA/Browser Forum 通过决议,SSL/TLS 证书有效期要逐步缩短到 47 天。一年续 8 次以上,手动?想都别想。

我正好有几台亚马逊云科技的 EC2 跑 Nginx,证书是 Let's Encrypt 的。趁这个机会把自动续期整明白了,顺便试了三种 Certbot 模式,踩了两个坑,记录一下。

有效期缩短时间表

时间 有效期
现在 398 天
2026.3 200 天
2027.3 100 天
2029.3 47 天

ACM 用户可以不看这篇

如果你用 ALB / CloudFront / API Gateway,直接用 ACM 申请证书,自动续期不需要操心。本文是给那些在 EC2 上自建 Nginx、用第三方证书的同学看的。

三种 Certbot 模式

Standalone:简单粗暴但有停机

bash 复制代码
sudo systemctl stop nginx
sudo certbot certonly --standalone -d yourdomain.com
sudo systemctl start nginx

每次续期 Nginx 要停 10-30 秒。开发环境凑合用,生产别碰。

Webroot:零停机但需要改配置

核心是给 ACME 验证路径开个口子:

nginx 复制代码
server {
    listen 80;
    server_name yourdomain.com;

    location /.well-known/acme-challenge/ {
        root /usr/share/nginx/html;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

踩坑:我第一次忘了加这个 location,80 端口全部 301 到 HTTPS,Let's Encrypt 验证请求也被重定向了,签发失败。排查半小时。

bash 复制代码
sudo certbot certonly --webroot -w /usr/share/nginx/html -d yourdomain.com

Nginx 插件:零停机 + 全自动(推荐)

bash 复制代码
sudo certbot --nginx -d yourdomain.com

一条命令搞定验证、下载、配置、reload。后续 certbot renew 自动搞定一切。

自动续期

bash 复制代码
# 检查定时任务是否存在
sudo systemctl list-timers | grep certbot

# 没有就手动加
echo "0 3,15 * * * root certbot renew --quiet" | sudo tee /etc/cron.d/certbot

DigiCert 也支持 ACME

商业证书同样能自动化:

bash 复制代码
sudo certbot register \
  --server "https://acme.digicert.com/v2/acme/directory/" \
  --eab-kid "YOUR_KID" --eab-hmac-key "YOUR_KEY" \
  --email admin@yourdomain.com --agree-tos

sudo certbot --nginx \
  --server "https://acme.digicert.com/v2/acme/directory/" \
  -d yourdomain.com

总结

模式 停机 复杂度 适合
Standalone 10-30s 测试
Webroot 需要掌控配置
Nginx 插件 大部分生产

能用 ACM 就用 ACM,自建就 Nginx 插件模式一把梭。别等 47 天有效期落地了才慌。


参考:亚马逊云科技官博 - 使用ACME协议实现证书自动续期

相关推荐
辰_砂13 小时前
国产服务器操作系统编译nginx生成rpm包
运维·nginx
finyouIT1 天前
限制国外ip访问网站
nginx
qq_312920112 天前
高并发防护:Nginx 流量控制
nginx
秋漓2 天前
Nginx学习与应用
运维·学习·nginx
skywalk81632 天前
nginx的配置软件Nginx UI
运维·nginx·ui
NGINX开源社区2 天前
NGINX Ingress Controller 中的 Cache Policy:VirtualServer 实战指南
java·前端·nginx
johnny2332 天前
Nginx可视化管理工具:NPM、nginx config、Nginx UI、NginxWebUI、Nginx Pulse
nginx
Linux运维老纪2 天前
nginx 打造高性能 API 网关(‌Building a High-Performance API Gateway with Nginx)
linux·运维·mysql·nginx·云计算·运维开发
FenceRain3 天前
Nginx 升级,平滑升级不停服务
服务器·网络·nginx
武器大师723 天前
实战踩坑:Gerrit HTTP 克隆失败解决方案
运维·nginx·gerrit