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

相关推荐
夜影风2 小时前
Nginx部署Vue/React项目时无法直接访问页面其他路径的问题及解决方案
vue.js·nginx·react.js
小小的木头人2 小时前
Nginx 访问控制及安全配置文档
运维·nginx·安全
七七powerful17 小时前
loki监控docker容器&系统&nginx日志的告警规则
nginx·docker·容器
dovens20 小时前
httpslocalhostindex 配置的nginx,一刷新就报404了
运维·nginx
riNt PTIP1 天前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
wellc1 天前
Nginx作用以及应用场景
运维·nginx
lclcooky1 天前
Nginx代理到https地址忽略证书验证配置
运维·nginx·https
草木红1 天前
Vue3 + Docker + Nginx 完整部署流程
nginx·docker·容器
lljss20201 天前
1. Nginx架构,反向代理和负载均衡
nginx