若未安裝Nginx,通過以下命令安裝:
sudo apt update
sudo apt install nginx
安裝完成后,Nginx會自動啟動,可通過systemctl status nginx
驗證狀態。
Let’s Encrypt提供免費SSL證書,通過Certbot可簡化申請與自動續期流程:
sudo apt install certbot python3-certbot-nginx
yourdomain.com
和www.yourdomain.com
為你的實際域名:sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot會自動驗證域名所有權,配置Nginx,并提示輸入電子郵件(用于續期提醒)及是否開啟自動跳轉HTTPS。Certbot通常會自動修改Nginx配置文件(位于/etc/nginx/sites-available/yourdomain.com
),核心配置如下:
fullchain.pem
為證書鏈,privkey.pem
為私鑰)。若選擇手動配置,需完成以下步驟:
.crt
)、私鑰(.key
)及中間證書(若有)上傳至服務器,建議存放于/etc/ssl/certs/
(證書)和/etc/ssl/private/
(私鑰)目錄;/etc/nginx/sites-available/yourdomain.com
,添加以下內容:server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri; # 強制HTTP跳轉HTTPS
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
# 證書路徑
ssl_certificate /etc/ssl/certs/yourdomain.com.crt;
ssl_certificate_key /etc/ssl/private/yourdomain.com.key;
# 加密配置(推薦)
ssl_protocols TLSv1.2 TLSv1.3; # 僅啟用TLS 1.2及以上版本
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; # 優先使用服務器端加密套件
# 可選安全增強
ssl_trusted_certificate /etc/ssl/certs/chain.pem; # 中間證書(若有)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; # 啟用HSTS
# 網站根目錄
root /var/www/yourdomain.com;
index index.html index.htm;
}
sites-enabled
目錄:sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
若輸出Syntax OK
,則繼續下一步;若有錯誤,根據提示修改配置文件。sudo systemctl restart nginx
或重新加載(不中斷現有連接):sudo systemctl reload nginx
https://yourdomain.com
,確認地址欄顯示鎖圖標(表示HTTPS連接成功);curl
命令檢查SSL握手:curl -I https://yourdomain.com
若返回HTTP/2 200
,則表示SSL配置生效。Let’s Encrypt證書有效期為90天,Certbot會自動創建cron任務續期,但需驗證續期流程:
sudo certbot renew --dry-run
若輸出Congratulations, all renewals succeeded
,則表示續期正常;--post-hook "systemctl reload nginx"
),無需手動操作。通過以上步驟,即可在Debian系統上為Nginx成功配置SSL證書,實現HTTPS安全訪問。