debian

如何在Debian上配置Nginx SSL自动续期

小樊
34
2025-05-24 21:50:37
栏目: 云计算

在Debian上配置Nginx SSL自动续期,可以使用Certbot工具。Certbot是一个自动化的SSL证书管理工具,支持Let’s Encrypt和其他证书颁发机构。以下是详细步骤:

1. 安装Certbot

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

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

2. 获取SSL证书

使用Certbot获取SSL证书。假设你的域名是example.com,运行以下命令:

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

Certbot会自动配置Nginx以使用SSL,并创建一个临时的Nginx配置文件来测试配置。如果一切正常,Certbot会提示你重载Nginx服务:

sudo systemctl reload nginx

3. 配置自动续期

Certbot会创建一个定时任务(cron job)来自动续期证书。你可以检查这个定时任务是否存在:

sudo crontab -l | grep certbot

你应该会看到类似以下的条目:

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小时检查一次证书的有效期,并在证书即将过期时自动续期。

4. 手动测试续期

你可以手动测试续期过程,以确保一切正常:

sudo certbot renew --dry-run

如果续期成功,Certbot会输出类似以下的信息:

Congratulations, your certificate and chain have been saved to:
/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem
Your key file needs to be secured at /etc/letsencrypt/live/example.com/privkey.pem
Your certificate Certificates will expire on 2023-12-31. To renew this certificate, run:
  certbot renew

5. 监控续期状态

你可以使用以下命令来查看Certbot的续期状态:

sudo certbot certificates

这将显示所有已安装的证书及其状态。

6. 配置防火墙

确保你的防火墙允许HTTPS流量(端口443):

sudo ufw allow 'Nginx Full'

总结

通过以上步骤,你已经在Debian上成功配置了Nginx SSL自动续期。Certbot会自动处理证书的获取和续期,确保你的网站始终使用有效的SSL证书。

0
看了该问题的人还看了