在Ubuntu上部署SSL證書以啟用HTTPS連接,通常涉及以下幾個步驟。以下是使用Let’s Encrypt免費證書的詳細指南:
Certbot是Let’s Encrypt的官方客戶端工具,用于自動獲取和續訂SSL證書。
sudo apt update
sudo apt install certbot python3-certbot-nginx # 如果使用Nginx
# 或者
sudo apt install certbot python3-certbot-apache # 如果使用Apache
根據你使用的Web服務器,運行相應的Certbot命令。
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Certbot會自動配置你的Web服務器以使用SSL,并創建一個臨時文件來測試配置是否正確。如果一切正常,它會將證書和私鑰文件移動到適當的位置,并更新你的Web服務器配置文件。
Certbot會自動驗證你的域名所有權,并在驗證成功后重啟Web服務器以應用新的SSL配置。
sudo systemctl restart nginx
sudo systemctl restart apache2
Let’s Encrypt證書通常有效期為90天,因此需要設置自動續訂。
Certbot會在安裝時創建一個cron任務或systemd定時器來自動續訂證書。你可以手動測試續訂過程以確保一切正常。
sudo certbot renew --dry-run
如果一切正常,Certbot會輸出續訂成功的信息。
為了提高安全性,可以啟用HTTP Strict Transport Security (HSTS)。
在Nginx配置文件中添加以下行:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
在Apache配置文件中添加以下行:
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
然后重啟Web服務器。
通過以上步驟,你可以在Ubuntu上成功部署SSL證書,并啟用HTTPS連接。確保定期檢查和續訂證書,以保持網站的安全性。