安装 Certbot
在 CentOS 上安装 Certbot 和 Nginx 插件:
对于 CentOS 8 或更高版本:
sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y
对于 CentOS 7:
sudo yum install epel-release -y
sudo yum install certbot python2-certbot-nginx -y
生成证书
使用 Certbot 自动为 Nginx 配置 HTTPS:
sudo certbot --nginx -d example.com -d www.example.com
-d example.com -d www.example.com:指定你的域名,可以替换为你实际的域名。- Certbot 会自动检测 Nginx 配置并申请证书。
在交互过程中:
- 输入你的邮箱地址(用于接收通知)。
- 同意服务条款。
自动续期
Let’s Encrypt 的证书有效期为 90 天,但 Certbot 提供自动续期功能。以下命令测试自动续期是否正常:
sudo certbot renew --dry-run
如果测试成功,说明自动续期功能已经正常工作。系统会默认设置自动续期任务,你也可以手动添加到 cron 或 systemd 计划任务中。
手动验证证书更新
你可以随时查看当前的证书:
sudo certbot certificates
如果需要更新证书:
sudo certbot renew