如何在 Ubuntu 20.04 上使用 Let's Encrypt 保護 Nginx
介紹
Let's Encrypt 是一個證書頒發機構 (CA),它提供了一種簡單的方法來獲取和安裝免費的TLS/SSL 證書,從而在 Web 服務器上啟用加密的 HTTPS。它通過提供軟件客戶端 Certbot 來簡化流程,該客戶端嘗試自動執行大部分(如果不是全部)所需步驟。目前,獲取和安裝證書的整個過程在 Apache 和 Nginx 上都是完全自動化的。
在本教程中,您將使用 Certbot 在 Ubuntu 20.04 上為 Nginx 獲取免費的 SSL 證書,并將您的證書設置為自動續訂。
本教程將使用單獨的 Nginx 服務器配置文件而不是默認文件。我們建議為每個域創建新的 Nginx 服務器塊文件,因為它有助于避免常見錯誤并維護默認文件作為后備配置。
先決條件
要學習本教程,您需要:
- 設置一個 Ubuntu 20.04 服務器,包括啟用 sudo 的非root用戶和防火墻。
- 一個注冊的域名。本教程將example.com貫穿始終。
- 為您的服務器設置的以下兩個 DNS 記錄。
- example.com指向您服務器的公共 IP 地址的 A 記錄。
- 指向您服務器的公共 IP 地址的 A 記錄。www.example.com
- 按照如何在 Ubuntu 20.04 上安裝 Nginx 安裝 Nginx。確保您的域有一個服務器塊。本教程將用作示例。/etc/nginx/sites-available/example.com
步驟 1 - 安裝 Certbot
使用 Let's Encrypt 獲取 SSL 證書的第一步是在您的服務器上安裝 Certbot 軟件。
安裝 Certbot 和它的 Nginx 插件apt:
- sudo apt install certbot python3-certbot-nginx
Certbot 現在可以使用了,但是為了讓它自動為 Nginx 配置 SSL,我們需要驗證 Nginx 的一些配置。
步驟 2 — 確認 Nginx 的配置
Certbot 需要能夠server在您的 Nginx 配置中找到正確的塊才能自動配置 SSL。具體來說,它通過查找server_name與您為其申請證書的域匹配的指令來實現這一點。
如果您按照Nginx 安裝教程中的服務器塊設置步驟進行操作,則您的域中應該有一個服務器塊,并且已正確設置了指令。/etc/nginx/sites-available/example.comserver_name
要檢查,請使用nano或您喜歡的文本編輯器打開您的域的配置文件:
- sudo nano /etc/nginx/sites-available/example.com
找到現有的server_name行。它應該是這樣的:
/etc/nginx/sites-available/example.com
- ...
- server_name example.com www.example.com;
- ...
如果是,請退出編輯器并繼續下一步。
如果不是,請更新它以匹配。然后保存文件,退出編輯器,并驗證配置編輯的語法:
- sudo nginx -t
如果出現錯誤,請重新打開服務器塊文件并檢查是否有任何拼寫錯誤或缺失字符。配置文件的語法正確后,重新加載 Nginx 以加載新配置:
- sudo systemctl reload nginx
Certbot 現在可以找到正確的server塊并自動更新它。
接下來,讓我們更新防火墻以允許 HTTPS 流量。
步驟 3 — 允許 HTTPS 通過防火墻
如果您ufw按照先決條件指南的建議啟用了防火墻,則需要調整設置以允許 HTTPS 流量。幸運的是,Nginxufw在安裝時注冊了一些配置文件。
您可以通過鍵入以下內容查看當前設置:
- sudo ufw status
它可能看起來像這樣,這意味著 Web 服務器只允許 HTTP 流量:
- Status: active
- To Action From
- -- ------ ----
- OpenSSH ALLOW Anywhere
- Nginx HTTP ALLOW Anywhere
- OpenSSH (v6) ALLOW Anywhere (v6)
- Nginx HTTP (v6) ALLOW Anywhere (v6)
要額外允許 HTTPS 流量,請允許 Nginx 完整配置文件并刪除冗余的 Nginx HTTP 配置文件限額:
- sudo ufw allow 'Nginx Full'
- sudo ufw delete allow 'Nginx HTTP'
您現在的狀態應如下所示:
- sudo ufw status
- Status: active
- To Action From
- -- ------ ----
- OpenSSH ALLOW Anywhere
- Nginx Full ALLOW Anywhere
- OpenSSH (v6) ALLOW Anywhere (v6)
- Nginx Full (v6) ALLOW Anywhere (v6)
接下來,讓我們運行 Certbot 并獲取我們的證書。
步驟 4 - 獲取 SSL 證書
Certbot 提供了多種通過插件獲取 SSL 證書的方式。Nginx 插件將負責重新配置 Nginx 并在必要時重新加載配置。要使用此插件,請鍵入以下內容:
- sudo certbot --nginx -d example.com -d www.example.com
這certbot與--nginx插件一起運行,-d用于指定我們希望證書有效的域名。
如果這是您第一次運行certbot,系統會提示您輸入電子郵件地址并同意服務條款。執行此操作后,certbot將與 Let's Encrypt 服務器通信,然后運行質詢以驗證您是否控制要為其申請證書的域。
如果成功,certbot將詢問您希望如何配置 HTTPS 設置。
- Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 1: No redirect - Make no further changes to the webserver configuration.
- 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
- new sites, or if you're confident your site works on HTTPS. You can undo this
- change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
選擇您的選擇,然后點擊ENTER。配置將被更新,Nginx 將重新加載以獲取新設置。certbot將以一條消息結束,告訴您該過程已成功以及您的證書的存儲位置:
- IMPORTANT NOTES:
- - Congratulations! Your certificate and chain have been saved at:
- /etc/letsencrypt/live/example.com/fullchain.pem
- Your key file has been saved at:
- /etc/letsencrypt/live/example.com/privkey.pem
- Your cert will expire on 2020-08-18. To obtain a new or tweaked
- version of this certificate in the future, simply run certbot again
- with the "certonly" option. To non-interactively renew *all* of
- your certificates, run "certbot renew"
- - If you like Certbot, please consider supporting our work by:
- Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
- Donating to EFF: https://eff.org/donate-le
您的證書已下載、安裝和加載。嘗試使用重新加載您的網站https://并注意瀏覽器的安全指示器。它應該表明該站點已得到適當保護,通常帶有鎖定圖標。如果您使用SSL Labs Server Test測試您的服務器,它將獲得A級。
讓我們通過測試更新過程來結束。
步驟 5 - 驗證 Certbot 自動續訂
Let's Encrypt 的證書有效期只有九十天。這是為了鼓勵用戶自動化他們的證書更新過程。certbot我們安裝的軟件包通過添加一個每天運行兩次的 systemd 計時器來為我們處理這個問題,并自動更新任何在到期后三十天內的證書。
您可以使用以下命令查詢計時器的狀態systemctl:
- sudo systemctl status certbot.timer
- ● certbot.timer - Run certbot twice daily
- Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
- Active: active (waiting) since Mon 2020-05-04 20:04:36 UTC; 2 weeks 1 days ago
- Trigger: Thu 2020-05-21 05:22:32 UTC; 9h left
- Triggers: ● certbot.service
要測試更新過程,您可以使用以下命令進行試運行certbot:
- sudo certbot renew --dry-run
如果您沒有看到任何錯誤,則說明一切就緒。必要時,Certbot 將更新您的證書并重新加載 Nginx 以獲取更改。如果自動續訂過程失敗,Let's Encrypt 將向您指定的電子郵件發送一條消息,在您的證書即將到期時向您發出警告。
結論
在本教程中,您安裝了 Let's Encrypt 客戶端certbot,為您的域下載了 SSL 證書,將 Nginx 配置為使用這些證書,并設置了自動證書續訂。如果您對 Certbot的使用還有其他疑問,可以從官方文檔開始。