CentOS + Nginx 自动续期SSL
在 CentOS 上使用 Nginx 和 Certbot 自动续期 Let’s Encrypt SSL 证书的详细流程,2025年最新简便流程。
安装 Certbot
sudo yum install epel-release sudo yum install certbot python2-certbot-nginx
生成证书
sudo certbot certonly --webroot -w /www/wwwroot/xigk.com -d xigk.com -d www.xigk.com
这里 -w /www/wwwroot/xigk.com 指定网站的根目录路径,确保 Let’s Encrypt 可以访问 .well-known/acme-challenge 文件夹以验证域名所有权。
注意,如果用框架,是要加/public目录的
按提示下一步,然后会生成证书的目录/etc/letsencrypt/live/xigk.com/fullchain.pem /etc/letsencrypt/live/xigk.com/privkey.pem
去更新 Nginx 配置文件,配置最新的SSL路径上去。
- 添加,每周定时shell任务
任务类型:shell脚本
脚本内容:certbot renew
成功搞定SSL证书的申请和自动续期啦! - 注意,证书续期频率限制:Let's Encrypt 对证书续期有频率限制(每周最多 5 次),因此不要频繁运行 certbot renew。
Let's Encrypt 证书的有效期为 90 天,certbot renew 只会在证书到期前 30 天内尝试续期。所以每周一次请求就行了。 脚本内容修改为
sudo certbot renew --deploy-hook "systemctl reload nginx"
因此在证书续期成功以后,Nginx 进程里仍然用的是旧证书的内存副本,浏览器就会继续看到过期证书.
centos时间同步
# 查看当前系统时间 timedatectl # 如果时区或时间不正确,手动同步 sudo yum install -y ntpdate sudo ntpdate pool.ntp.org sudo hwclock --systohc # 同步到硬件时钟 # 启用 NTP 自动同步(防止未来偏差) # 安装并启用 NTP 服务(CentOS 7/8) sudo yum install -y chrony sudo systemctl enable --now chronyd # 或手动同步一次(临时方案) sudo ntpdate pool.ntp.org