引言
Let’s Encrypt 是一個證書授權中心(CA),它提供了獲取和安裝免費的 TLS/SSL 證書的方式,從而實現在網頁伺服器上啟用加密的 HTTPS。它通過提供一個軟件客戶端 Certbot,試圖自動化大部分(如果不是全部)所需的步驟,從而簡化過程。目前,獲取和安裝證書的整個過程在 Apache 和 Nginx 上都已完全自動化。
在這份指南中,您將使用 Certbot 為 Ubuntu 上的 Apache 獲取免費的 SSL 證書,並確保該證書能夠自動續約。
本教程使用单独的虛擬主機文件,而不是 Apache 的默認配置文件,來設置將由 Let’s Encrypt 進行保護的網站。我們建議為伺服器中托管的每個域名創建新的 Apache 虛擬主機文件,因為這有助於避免常見錯誤並保持默認配置文件作為後備設置。
如何在 Ubuntu 上使用 Let’s Encrypt 保障 Apache 的安全
先決條件
為了遵循本教學,您需要:
-
一個設定好的Ubuntu伺服器,具有非root使用者並具有
sudo
管理權限以及啟用的防火牆。您可以透過遵循我們的Ubuntu的初始伺服器設置教學來完成設置。 -
一個完全註冊的域名。本教學將使用your_domain作為範例。您可以在Namecheap購買域名,在Freenom免費獲取一個,或者使用您選擇的域名註冊商。
-
以下兩個 DNS 記錄已為您的伺服器設置。您可以參考 DigitalOcean DNS 的介紹 以了解如何添加它們的詳細信息。
- 一個 A 記錄,其中
your_domain
指向您的伺服器的公開 IP 地址。 - 一個 A 記錄,其中
www.your_domain
指向您的伺服器的公開 IP 地址。
- 一個 A 記錄,其中
-
按照 如何在 Ubuntu 上安裝 Apache 安裝了 Apache。請確保您為您的域名有一個 虛擬主機文件。本教程將使用
/etc/apache2/sites-available/your_domain.conf
作為示例。
第 1 步 — 安裝 Certbot
要使用 Let’s Encrypt 獲取 SSL 證書,您需要在您的伺服器上安裝 Certbot 軟件。您將使用 Ubuntu 的預設套件庫來完成此操作。
首先,更新本地套件索引:
您需要兩個套件:certbot
和 python3-certbot-apache
。後者是將 Certbot 與 Apache 整合的插件,可以讓您使用單一命令自動化獲取證書並在您的網頁伺服器中配置 HTTPS:
系統會提示您按 Y
然後 ENTER
確認安裝。
Certbot 已經在你的服務器上安裝完成。在下一步,你將要驗證 Apache 的配置,以確保你的虛擬主機設定得當。這將確保 `certbot` 客戶端腳本能夠侦測到你的域名並自動重新配置你的網絡服務器以使用你剛才生成的 SSL 憑證。
步驟 2 — 檢查你的 Apache 虛擬主機配置
為了自動化獲取和配置 SSL 憑證,Certbot 需要在你的 Apache 配置文件中找到正確的虛擬主機。你的服務器域名将从你在 `VirtualHost` 配置區段中定的 `ServerName` 和 `ServerAlias` 指令中獲取。
如果你遵循了 Apache 安裝教程中的虛擬主機設定步驟,你應該在 `/etc/apache2/sites-available/your_domain.conf` 文件中為你的域名設置了一個虛擬主機區段,並已經適當設定 `ServerName` 以及 `ServerAlias` 指令。
為了確認这一点,請使用 `nano` 或你偏好的文字編輯器打開你的虛擬主機文件:
尋找现有的 `ServerName` 和 `ServerAlias` 行。它們應該如下所示:
...
ServerName your_domain
ServerAlias www.your_domain
...
如果您已經將 `ServerName` 和 `ServerAlias` 設定為這樣,您可以退出文字編輯器並進行下一步。如果您當前的虛擬主機配置與示例不匹配,請 accordingly 進行更新。如果您使用 `nano`,您可以通過按 `CTRL+X`,然後 `Y` 和 `ENTER` 來退出,以確認任何更改,如果有的話。然後,運行以下命令以驗證您的更改:
您應該收到 `Syntax OK` 作為回应。如果您遇到錯誤,請重新打開虛擬主機文件並檢查任何拼寫錯誤或缺少的字符。一旦您的配置文件的語法正確,請重新載入 Apache,以便更改生效:
携帶這些更改,Certbot 將能夠找到正確的 VirtualHost 區塊並更新它。
接下來,您將更新防火墙以允許 HTTPS 流量。
步驟 3 — 允許 HTTPS 通過防火墙
如果您啟用了 UFW 防火墙,正如前提指南中所推薦的那樣,您需要調整設定以允許 HTTPS 流量。在安裝後,Apache 註冊了幾個不同的 UFW 應用配置文件。您可以 leverate 使用了 `Apache Full` 配置文件,以允許您的服務器上 HTTP 和 HTTPS 流量。
為了驗證目前您的伺服器允許什麼樣的流量,請檢查狀態:
如果您遵循了我們其中一個Apache安裝指南,您將會看到類似以下的輸出,意味著目前只允許在端口80
上的HTTP流量:
OutputStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
要允許HTTPS流量,請允許“Apache Full”配置檔:
然後刪除多余的“Apache”配置檔:
您的狀態將顯示如下:
OutputStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
現在您已經準備好運行Certbot並獲取您的證書。
第4步 — 獲取SSL證書
Certbot通過插件提供了多種獲取SSL證書的方式。Apache插件將負責重新配置Apache並在需要時重新加載配置。要使用此插件,請運行以下命令:
此腳本將提示您回答一系列問題以配置您的SSL證書。首先,它會要求您提供有效的電子郵件地址。這個電子郵件將用於續訂通知和安全性通知:
OutputSaving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): you@your_domain
提供有效的電子郵件地址後,按ENTER
進行到下一步。然後您將被提示確認是否同意Let’s Encrypt的服務條款。您可以通过按Y
然後按ENTER
來確認:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
接下來,您將被問及是否願意與電子前沿基金會分享您的電子郵件,以接收新聞和其他信息。如果您不想訂閱他們的內容,請輸入N
。否則,請輸入Y
然後按ENTER
鍵進行到下一步:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
下一個步驟將提示您告知Certbot您希望為哪些域名啟用HTTPS。列出的域名會自動從您的Apache虛擬主機配置中獲取,因此確保您在虛擬主機中配置了正確的ServerName
和ServerAlias
設置非常重要。如果您想為所有列出的域名啟用HTTPS(建議),您可以將提示留空並按ENTER
鍵進行。否則,請選擇您想要啟用HTTPS的域名,通過列出每個適當的數字,之間用逗號和/或空格分隔,然後按ENTER
鍵:
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: your_domain
2: www.your_domain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
此步驟之後,Certbot的配置即完成,您將看到有關新憑證和生成文件位置的最後評論:
OutputSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2022-07-10.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Successfully deployed certificate for www.your_domain.com to /etc/apache2/sites-available/your_domain-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https:/your_domain and https://www.your_domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
您的憑證現在已安裝並載入到Apache的配置中。嘗試使用https://
重新加載您的網站,並注意您的瀏覽器的安全指示符。它應該表明您的網站正確安全,通常在地址欄中有一個鎖定圖標。
您可以使用SSL Labs伺服器測試來驗證您的憑證等級,並從外部服務的角度獲取關於它的詳細信息。
在下一個也是最後一步中,您將測試Certbot的自動續期功能,該功能保證您的證書將在到期日前自動續期。
第5步 — 驗證Certbot自動續期
Let’s Encrypt的證書有效期 only 僅九十天。這是為了鼓勵用戶自動化他們的證書續期流程,同時也確保被濫用或被盜的密鑰會盡快過期。
您安裝的certbot
套件通過在/etc/cron.d
中包含一個續期腳本來處理續期,該腳本由稱為certbot.timer
的systemctl
服務管理。此腳本每天運行兩次,並將自動續期任何在到期前三十天內的證書。
要檢查此服務的狀態並確保其為活躍狀態,運行以下命令:
您的輸出將類似於以下內容:
Output● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset:>
Active: active (waiting) since Mon 2022-04-11 20:52:46 UTC; 4min 3s ago
Trigger: Tue 2022-04-12 00:56:55 UTC; 4h 0min left
Triggers: ● certbot.service
Apr 11 20:52:46 jammy-encrypt systemd[1]: Started Run certbot twice daily.
要測試續期流程,您可以用certbot
進行一個乾運行:
OutputSaving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your_domain.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your_domain and www.your_domain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/your_domain/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
如果您沒有收到任何錯誤,那麼一切設置正常。當需要時,Certbot將續期您的證書並重新加載Apache以採用變更。如果自動續期過程失敗,Let’s Encrypt將向您指定的電子郵件發送一條消息,警告您證書即將到期。
結論
在這個教學中,您安裝了Let’s Encrypt客戶端certbot
,為您的域名配置並安裝了SSL證書,並確認Certbot的自動續期服務在systemctl
內是活躍的。如果您有關於使用Certbot的進一步問題,他們的文檔是一個不錯的起步點。
Source:
https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu