在Debian上为Nginx SSL配置Let’s Encrypt,你可以使用Certbot工具,它是一个自动化的证书管理器,可以轻松地为你的网站获取和续订Let’s Encrypt证书。以下是详细步骤:
首先,你需要安装Certbot及其Nginx插件。打开终端并运行以下命令:
sudo apt update
sudo apt install certbot python3-certbot-nginx
Certbot会引导你完成证书的获取过程。运行以下命令来启动Certbot:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将yourdomain.com
和www.yourdomain.com
替换为你的实际域名。
Certbot会自动修改你的Nginx配置文件以启用HTTPS。它会创建一个新的配置文件,并将其链接到你的站点配置中。通常,这个文件位于/etc/letsencrypt/options-ssl-nginx.conf
。
确保你的Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.com
)包含以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
在重新加载Nginx之前,测试配置文件是否有语法错误:
sudo nginx -t
如果没有错误,重新加载Nginx以应用更改:
sudo systemctl reload nginx
Certbot会自动验证你的域名所有权。验证完成后,它会自动获取并安装证书。
Let’s Encrypt证书每90天需要续订一次。Certbot会自动处理续订过程,但你可以手动触发续订:
sudo certbot renew --dry-run
如果一切正常,Certbot会在证书到期前30天自动续订证书。
为了确保Certbot能够自动续订证书,你可以设置一个定时任务(cron job):
sudo crontab -e
添加以下行:
0 0,12 * * * certbot renew --post-hook "systemctl reload nginx"
这会在每天午夜和中午检查并续订证书,并在续订成功后重新加载Nginx。
通过以上步骤,你就可以在Debian上为Nginx SSL配置Let’s Encrypt证书了。