在 Debian 上给 Nginx 申请 SSL 证书,最常用、最简单、免费的方式是使用 Let’s Encrypt + Certbot。下面给你一套完整、可操作的步骤。
sudo apt update
sudo apt upgrade -y
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
替换
yourdomain.com为你自己的域名
申请过程中会:
sudo certbot certonly --nginx -d yourdomain.com
证书路径:
/etc/letsencrypt/live/yourdomain.com/
示例 Nginx 配置:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
root /var/www/html;
index index.html;
}
}
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx
Let’s Encrypt 证书 90 天 有效,Certbot 已自带续期:
测试续期:
sudo certbot renew --dry-run
系统已自动添加定时任务(无需手动):
systemctl list-timers | grep certbot
确保:
sudo ufw allow 80
sudo ufw allow 443
ping yourdomain.com
✅ 最简单方案:
apt install certbot python3-certbot-nginx
certbot --nginx -d 域名
如果你愿意,可以告诉我:
我可以给你更精准的配置。