在CentOS上配置Nginx以自动续期SSL证书,通常需要使用Certbot工具。Certbot是一个由Let’s Encrypt提供的自动化工具,可以轻松地为Nginx配置SSL证书,并支持自动续期。以下是详细步骤:
首先,确保你的系统是最新的,并安装EPEL仓库(如果尚未安装):
sudo yum update -y
sudo yum install epel-release -y
然后,安装Certbot和Nginx插件:
sudo yum install certbot python3-certbot-nginx -y
使用Certbot获取SSL证书。假设你的域名是example.com,运行以下命令:
sudo certbot --nginx -d example.com -d www.example.com
Certbot会自动配置Nginx并重启服务以应用新的SSL证书。
Certbot会创建一个定时任务(cron job)来自动续期证书。你可以检查这个定时任务:
sudo crontab -l
你应该能看到类似以下的条目:
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot renew --deploy-hook "systemctl reload nginx"
这个定时任务每12小时运行一次,检查证书是否需要续期,并在需要时自动续期并重新加载Nginx。
你可以手动测试续期过程,确保一切正常:
sudo certbot renew --dry-run
如果没有错误,运行以下命令来实际续期证书:
sudo certbot renew
确保你的Nginx配置文件正确设置了SSL证书和密钥。通常,Certbot会自动更新这些文件。以下是一个示例配置:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
root /var/www/html;
index index.html index.htm;
}
}
最后,重启Nginx以应用配置更改:
sudo systemctl restart nginx
通过以上步骤,你就可以在CentOS上配置Nginx以自动续期SSL证书了。Certbot会处理证书的获取和续期,确保你的网站始终使用有效的SSL证书。