Nginx SSL证书续期主要通过Certbot工具实现(以Let’s Encrypt证书为例),步骤如下:
-
安装Certbot
# Debian/Ubuntu sudo apt update && sudo apt install certbot python3-certbot-nginx # CentOS/RHEL sudo yum install certbot python3-certbot-nginx -
获取证书(首次申请)
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comCertbot会自动配置Nginx并重启服务。
-
配置自动续期
- Certbot默认创建cron任务(每天检查,到期前30天续期),可通过以下命令测试:
sudo certbot renew --dry-run若输出“Congratulations”则配置成功。
- 若需自定义定时任务,可编辑
/etc/crontab,添加类似以下内容(每月1日续期):0 0 1 * * certbot renew --quiet
- Certbot默认创建cron任务(每天检查,到期前30天续期),可通过以下命令测试:
-
手动续期(可选)
sudo certbot renew续期后需手动重启Nginx:
sudo nginx -t && sudo systemctl reload nginx -
验证证书
- 命令行:
openssl x509 -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem -noout -dates - 浏览器:访问
https://yourdomain.com,检查锁标志。
- 命令行:
说明:Certbot会自动更新Nginx配置中的证书路径,无需手动修改配置文件(除非自定义路径)。