如何在 Ubuntu 20.04 上使用 Let's Encrypt 保護(hù) Nginx
介紹
Let's Encrypt 是一個(gè)證書頒發(fā)機(jī)構(gòu) (CA),它提供了一種簡(jiǎn)單的方法來獲取和安裝免費(fèi)的TLS/SSL 證書,從而在 Web 服務(wù)器上啟用加密的 HTTPS。它通過提供軟件客戶端 Certbot 來簡(jiǎn)化流程,該客戶端嘗試自動(dòng)執(zhí)行大部分(如果不是全部)所需步驟。目前,獲取和安裝證書的整個(gè)過程在 Apache 和 Nginx 上都是完全自動(dòng)化的。
在本教程中,您將使用 Certbot 在 Ubuntu 20.04 上為 Nginx 獲取免費(fèi)的 SSL 證書,并將您的證書設(shè)置為自動(dòng)續(xù)訂。
本教程將使用單獨(dú)的 Nginx 服務(wù)器配置文件而不是默認(rèn)文件。我們建議為每個(gè)域創(chuàng)建新的 Nginx 服務(wù)器塊文件,因?yàn)樗兄诒苊獬R婂e(cuò)誤并維護(hù)默認(rèn)文件作為后備配置。
先決條件
要學(xué)習(xí)本教程,您需要:
- 設(shè)置一個(gè) Ubuntu 20.04 服務(wù)器,包括啟用 sudo 的非root用戶和防火墻。
- 一個(gè)注冊(cè)的域名。本教程將example.com貫穿始終。
- 為您的服務(wù)器設(shè)置的以下兩個(gè) DNS 記錄。
- example.com指向您服務(wù)器的公共 IP 地址的 A 記錄。
- 指向您服務(wù)器的公共 IP 地址的 A 記錄。www.example.com
- 按照如何在 Ubuntu 20.04 上安裝 Nginx 安裝 Nginx。確保您的域有一個(gè)服務(wù)器塊。本教程將用作示例。/etc/nginx/sites-available/example.com
步驟 1 - 安裝 Certbot
使用 Let's Encrypt 獲取 SSL 證書的第一步是在您的服務(wù)器上安裝 Certbot 軟件。
安裝 Certbot 和它的 Nginx 插件apt:
- sudo apt install certbot python3-certbot-nginx
Certbot 現(xiàn)在可以使用了,但是為了讓它自動(dòng)為 Nginx 配置 SSL,我們需要驗(yàn)證 Nginx 的一些配置。
步驟 2 — 確認(rèn) Nginx 的配置
Certbot 需要能夠server在您的 Nginx 配置中找到正確的塊才能自動(dòng)配置 SSL。具體來說,它通過查找server_name與您為其申請(qǐng)證書的域匹配的指令來實(shí)現(xiàn)這一點(diǎn)。
如果您按照Nginx 安裝教程中的服務(wù)器塊設(shè)置步驟進(jìn)行操作,則您的域中應(yīng)該有一個(gè)服務(wù)器塊,并且已正確設(shè)置了指令。/etc/nginx/sites-available/example.comserver_name
要檢查,請(qǐng)使用nano或您喜歡的文本編輯器打開您的域的配置文件:
- sudo nano /etc/nginx/sites-available/example.com
找到現(xiàn)有的server_name行。它應(yīng)該是這樣的:
/etc/nginx/sites-available/example.com
- ...
- server_name example.com www.example.com;
- ...
如果是,請(qǐng)退出編輯器并繼續(xù)下一步。
如果不是,請(qǐng)更新它以匹配。然后保存文件,退出編輯器,并驗(yàn)證配置編輯的語法:
- sudo nginx -t
如果出現(xiàn)錯(cuò)誤,請(qǐng)重新打開服務(wù)器塊文件并檢查是否有任何拼寫錯(cuò)誤或缺失字符。配置文件的語法正確后,重新加載 Nginx 以加載新配置:
- sudo systemctl reload nginx
Certbot 現(xiàn)在可以找到正確的server塊并自動(dòng)更新它。
接下來,讓我們更新防火墻以允許 HTTPS 流量。
步驟 3 — 允許 HTTPS 通過防火墻
如果您ufw按照先決條件指南的建議啟用了防火墻,則需要調(diào)整設(shè)置以允許 HTTPS 流量。幸運(yùn)的是,Nginxufw在安裝時(shí)注冊(cè)了一些配置文件。
您可以通過鍵入以下內(nèi)容查看當(dāng)前設(shè)置:
- sudo ufw status
它可能看起來像這樣,這意味著 Web 服務(wù)器只允許 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 流量,請(qǐng)?jiān)试S Nginx 完整配置文件并刪除冗余的 Nginx HTTP 配置文件限額:
- sudo ufw allow 'Nginx Full'
- sudo ufw delete allow 'Nginx HTTP'
您現(xiàn)在的狀態(tài)應(yīng)如下所示:
- 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)
接下來,讓我們運(yùn)行 Certbot 并獲取我們的證書。
步驟 4 - 獲取 SSL 證書
Certbot 提供了多種通過插件獲取 SSL 證書的方式。Nginx 插件將負(fù)責(zé)重新配置 Nginx 并在必要時(shí)重新加載配置。要使用此插件,請(qǐng)鍵入以下內(nèi)容:
- sudo certbot --nginx -d example.com -d www.example.com
這certbot與--nginx插件一起運(yùn)行,-d用于指定我們希望證書有效的域名。
如果這是您第一次運(yùn)行certbot,系統(tǒng)會(huì)提示您輸入電子郵件地址并同意服務(wù)條款。執(zhí)行此操作后,certbot將與 Let's Encrypt 服務(wù)器通信,然后運(yùn)行質(zhì)詢以驗(yàn)證您是否控制要為其申請(qǐng)證書的域。
如果成功,certbot將詢問您希望如何配置 HTTPS 設(shè)置。
- 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):
選擇您的選擇,然后點(diǎn)擊ENTER。配置將被更新,Nginx 將重新加載以獲取新設(shè)置。certbot將以一條消息結(jié)束,告訴您該過程已成功以及您的證書的存儲(chǔ)位置:
- 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
您的證書已下載、安裝和加載。嘗試使用重新加載您的網(wǎng)站https://并注意瀏覽器的安全指示器。它應(yīng)該表明該站點(diǎn)已得到適當(dāng)保護(hù),通常帶有鎖定圖標(biāo)。如果您使用SSL Labs Server Test測(cè)試您的服務(wù)器,它將獲得A級(jí)。
讓我們通過測(cè)試更新過程來結(jié)束。
步驟 5 - 驗(yàn)證 Certbot 自動(dòng)續(xù)訂
Let's Encrypt 的證書有效期只有九十天。這是為了鼓勵(lì)用戶自動(dòng)化他們的證書更新過程。certbot我們安裝的軟件包通過添加一個(gè)每天運(yùn)行兩次的 systemd 計(jì)時(shí)器來為我們處理這個(gè)問題,并自動(dòng)更新任何在到期后三十天內(nèi)的證書。
您可以使用以下命令查詢計(jì)時(shí)器的狀態(tài)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
要測(cè)試更新過程,您可以使用以下命令進(jìn)行試運(yùn)行certbot:
- sudo certbot renew --dry-run
如果您沒有看到任何錯(cuò)誤,則說明一切就緒。必要時(shí),Certbot 將更新您的證書并重新加載 Nginx 以獲取更改。如果自動(dòng)續(xù)訂過程失敗,Let's Encrypt 將向您指定的電子郵件發(fā)送一條消息,在您的證書即將到期時(shí)向您發(fā)出警告。
結(jié)論
在本教程中,您安裝了 Let's Encrypt 客戶端certbot,為您的域下載了 SSL 證書,將 Nginx 配置為使用這些證書,并設(shè)置了自動(dòng)證書續(xù)訂。如果您對(duì) Certbot的使用還有其他疑問,可以從官方文檔開始。