Let’s Encrypt 是一個免費的 SSL/TLS 證書頒發機構,它可以幫助你為你的網站提供 HTTPS 安全連接
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
將 yourdomain.com 和 www.yourdomain.com 替換為你的域名。Certbot 將自動檢測你的 Nginx 配置文件,并在 /etc/nginx/sites-available/ 目錄下創建一個新的配置文件。
sudo nginx -t
如果配置文件沒有問題,你將看到以下輸出:
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx
現在,你的網站應該已經通過 Let’s Encrypt 獲得了 SSL/TLS 證書,并且可以通過 HTTPS 訪問了。
注意:Let’s Encrypt 的證書有效期為 90 天,因此你需要定期更新證書。你可以使用 sudo certbot --nginx 命令來更新證書。此外,你還可以設置自動更新證書,以便 Certbot 在證書即將過期時自動更新它們。要設置自動更新,請運行以下命令:
sudo certbot --nginx --renew-hook "/etc/letsencrypt/renewal/yourdomain.com.sh"
將 yourdomain.com.sh 替換為一個腳本文件名,該腳本將在證書更新后執行。確保該腳本具有可執行權限:
sudo chmod +x /etc/letsencrypt/renewal/yourdomain.com.sh