Nginx + Certbot 為 WordPress 網站設定 HTTPS
本教學將說明如何透過 Certbot 申請 Let’s Encrypt 免費 SSL 憑證,並整合至 Nginx,成功為你的 WordPress 網站啟用 HTTPS。
📌 前置準備
網站程式碼已部署於
/var/www/你的網站資料夾網域名稱(例如
yourdomain.com)已正確指向你的伺服器 IP已安裝 Nginx、PHP-FPM、Certbot
步驟一:建立 Nginx 設定檔
使用 nano 編輯對應網域的 Nginx 設定檔:
1 | sudo nano /etc/nginx/sites-available/yourdomain.com |
Bash
設定內容範例如下(HTTPS 區塊會由 Certbot 自動補上):
1 | server { |
Bash
啟用站點:
1 | sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/ |
步驟二:使用 Certbot 啟用 HTTPS
使用以下指令安裝 SSL 憑證並自動設定 Nginx:
1 | sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com |
1 | sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com |
Bash
若成功,會出現類似以下訊息:
1 | Successfully deployed certificate for yourdomain.com to /etc/nginx/sites-enabled/yourdomain.com |
1 | Successfully deployed certificate for yourdomain.com to /etc/nginx/sites-enabled/yourdomain.com |
Bash
步驟三:檢查 Nginx 設定
確認設定檔是否正確:
1 | sudo nginx -t |
1 | sudo nginx -t |
Bash
重新載入 Nginx:
1 | sudo systemctl reload nginx |
1 | sudo systemctl reload nginx |
Bash
步驟四:確認 HTTPS 連接埠(443)已開啟
檢查 443 port 是否啟用中:
1 | sudo ss -tulpn | grep ':443' |
1 | sudo ss -tulpn | grep ':443' |
Bash
若防火牆未開啟 443,請加入防火牆規則:
1 | sudo ufw allow 443 |
1 | sudo ufw allow 443 |
Bash
可再次確認 UFW 狀態:
1 | sudo ufw status |
1 | sudo ufw status |
Bash
步驟五:確認網站正常運作
瀏覽器開啟:
應顯示 WordPress 首頁,且網址列應顯示鎖頭圖示(HTTPS 已啟用)。
✅ 補充資訊
Certbot 預設會自動排程憑證續期,無需手動干預
憑證有效期限為 90 天,自動續期機制會確保不中斷
可手動測試續期機制:
sudo certbot renew --dry-run
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 全端企鵝!




