Centos 8 使用 Certbot 替 Nginx 自動添加免費 ssl 憑證
透過 certbot 添加免費 ssl 憑證,需依賴 firewall、snapd 以及 certbot,不另外購買 ssl 憑證的狀態下,達到每 3 個月自動更換 ssl 憑證的效果。
firewall
- 開啟 ssh、http、https 服務防火牆對應 port
firewall-cmd --zone=public --permanent --add-service=ssh
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
2. 重新啟動防火牆
firewall-cmd --reload
3. 檢查防火牆 ssh、http、https 是否已開啟
firewall-cmd --zone=public --permanent --list-services
snapd
- 安裝 epel 倉庫
dnf install epel-release
dnf upgrade
2. 安裝 snapd
dnf install snapd
3. 啟用 snapd
systemctl enable --now snapd.socket
4. 建立 snapd 軟連結
ln -s /var/lib/snapd/snap /snap
5. 重新開機
reboot
certbot
- 刪除原本的 certbot 避免版本衝突
dnf remove certbot
2. 使用 snapd 安裝 certbot
snap install --classic certbot
3. 建立 certbot 軟連結
ln -s /snap/bin/certbot /usr/bin/certbot
4. 請求 ssl 憑證
certbot --nginx
5. 輸入申請 ssl 憑證用的聯繫 email
輸入 email 後按 enter
6. 是否接受服務協議
輸入 Yes 後按 enter
7. 選擇要配置 ssl 的網域
輸入 1,2 後按 enter
8. certbot 將自動配置好憑證並生效於網站
瀏覽 https://example.com 看看
9. 測試自動續約是否成功
certbot renew --dry-run
10. 編輯 crontab 設定 certbot
crontab -e
11. 每週檢查 SSL 證書是否需要續約
0 0 * * 0 root /usr/bin/certbot renew >> /dev/null 2>&1
註一. 想要同時配置 example.com 跟 www.example.com 該怎麼做?
請先在 /etc/nginx/nginx.conf 的 server_name 填上 example.com www.example.com,會自動將選項帶出來,所以才有第 3–6 項的 1,2 選項
註二. 為什麼沒辦法成功申請 ssl 憑證?
務必將防火牆 ssh (22 port)、http (80 port)以及 https (443port)開通後再進行申請,需要注意的是失敗太多次還會暫時停用
註三. 如何查詢證書到期日?
certbot certificates
註四. 如何了解 domain SSL 的異常情形?
官方 debug 工具 (https://letsdebug.net/)