在Debian系統中設置Nginx的SSL證書,可以按照以下步驟進行:
如果你還沒有安裝Nginx,可以使用以下命令進行安裝:
sudo apt update
sudo apt install nginx
你可以從Let’s Encrypt免費獲取SSL證書。使用Certbot工具可以簡化這個過程。
sudo apt install certbot python3-certbot-nginx
運行以下命令來獲取并安裝證書:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
將yourdomain.com
和www.yourdomain.com
替換為你的域名。
Certbot會自動配置Nginx并重啟服務。它會提示你輸入郵箱地址、同意服務條款,并選擇是否設置自動續期。
如果你不想使用Certbot,也可以手動配置SSL證書。
你可以從Let’s Encrypt或其他證書頒發機構獲取SSL證書。通常你需要以下文件:
yourdomain.com.crt
(證書文件)yourdomain.com.key
(私鑰文件)ca_bundle.crt
(CA證書文件,如果需要)編輯Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com
。
sudo nano /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;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /path/to/yourdomain.com.crt;
ssl_certificate_key /path/to/yourdomain.com.key;
ssl_trusted_certificate /path/to/ca_bundle.crt; # 如果需要
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
將/path/to/yourdomain.com.crt
、/path/to/yourdomain.com.key
和/path/to/ca_bundle.crt
替換為實際的文件路徑。
創建一個符號鏈接以啟用配置:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
如果你使用Certbot獲取證書,它會自動設置一個cron任務來定期續期證書。你可以手動測試續期過程:
sudo certbot renew --dry-run
如果沒有問題,Certbot會自動續期證書。
通過以上步驟,你可以在Debian系統中成功設置Nginx的SSL證書。