ubuntu

Ubuntu Nginx如何实现自动续期SSL证书

小樊
39
2025-10-17 02:18:26
栏目: 云计算

在Ubuntu上使用Nginx和Certbot实现SSL证书的自动续期,可以按照以下步骤进行操作:

1. 安装Certbot

首先,确保你的系统是最新的,然后安装Certbot及其Nginx插件。

sudo apt update
sudo apt install certbot python3-certbot-nginx

2. 获取SSL证书

使用Certbot获取SSL证书。你可以指定域名和邮箱地址。

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

在提示时,选择是否将Nginx配置文件重写为HTTPS模式(通常是Yes)。

3. 配置自动续期

Certbot会自动创建一个cron任务或systemd定时器来定期检查并续期证书。你可以通过以下命令查看当前的定时任务:

sudo systemctl list-timers | grep certbot

如果你想手动触发续期,可以使用以下命令:

sudo certbot renew --dry-run

如果一切正常,证书将会被续期,并且Nginx配置文件会被更新。

4. 确保定时任务运行

Certbot默认会创建一个每天运行的定时任务来检查证书的有效性。你可以通过以下命令启用或禁用这个定时任务:

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

5. 检查日志

如果遇到问题,可以查看Certbot的日志来诊断问题。

sudo journalctl -u certbot.service

6. 配置防火墙

确保你的防火墙允许HTTP(80)和HTTPS(443)流量。

sudo ufw allow 'Nginx Full'

7. 测试配置

最后,测试Nginx配置是否正确,并重新加载Nginx服务。

sudo nginx -t
sudo systemctl reload nginx

通过以上步骤,你应该能够在Ubuntu上使用Nginx和Certbot实现SSL证书的自动续期。

0
看了该问题的人还看了