证书 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协议实现证书自动续期

相关推荐
Archy_Wang_117 小时前
LVS+KeepAlived+Nginx实现企业级业务高可靠部署的实战教程
运维·nginx·lvs
Ai拆代码的曹操19 小时前
Nginx upstream keepalive 连接池中毒排查实录
chrome·nginx·php
空谷有来人1 天前
Ubuntu22.04上安装配置站点
nginx
PC2005-cloud1 天前
KubeSphere学习笔记:部署nginx
笔记·学习·nginx
后除1 天前
Debian 安装与使用 Certbot
后端·nginx·https
风雨_831 天前
轻量级 Nginx 日志分析和可视化平台 NginxPulse安装
运维·nginx
若无情我 纸小铭1 天前
Nginx学习笔记(二) Nginx--connection&request
笔记·学习·nginx
xixingzhe21 天前
net::ERR_INCOMPLETE_CHUNKED_ENCODING解决
nginx
轻揉小乔 真新人2 天前
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制
运维·nginx·缓存
月落归舟2 天前
深入浅出:短信验证码登录场景下的 Nginx 负载均衡与 Session 共享
运维·nginx·负载均衡