在Debian系统上配置Nginx以使用Let’s Encrypt SSL证书,可以按照以下步骤进行:
Certbot是一个自动化的工具,用于获取和续订Let’s Encrypt证书。你可以使用以下命令安装Certbot:
sudo apt update
sudo apt install certbot python3-certbot-nginx
使用Certbot获取SSL证书。运行以下命令并按照提示操作:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
-d yourdomain.com
和 -d www.yourdomain.com
是你的域名。Certbot会自动修改你的Nginx配置文件以启用SSL。通常,配置文件位于 /etc/nginx/sites-available/yourdomain.com
和 /etc/nginx/sites-enabled/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;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/html;
index index.html index.htm;
location ~ /\.ht {
deny all;
}
}
在重新加载Nginx之前,测试配置文件是否有语法错误:
sudo nginx -t
如果一切正常,你会看到类似以下的输出:
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新加载Nginx以应用新的配置:
sudo systemctl reload nginx
Certbot会自动设置一个cron作业来定期续订证书。你可以手动测试续订过程:
sudo certbot renew --dry-run
如果没有错误,证书将会被续订,并且Nginx配置会自动更新。
确保你的防火墙允许443端口的流量:
sudo ufw allow 'Nginx Full'
使用浏览器访问你的域名,确保HTTPS连接正常,并且证书有效。
通过以上步骤,你应该能够在Debian系统上成功配置Nginx以使用Let’s Encrypt SSL证书。