如何在 Rocky Linux 9 上使用 Let’s Encrypt 保護 Nginx

介紹

Let’s Encrypt 是一家憑證授權機構(CA),提供了一種簡便的方式來獲取並安裝免費的 TLS/SSL 憑證,從而在 Web 伺服器上啟用加密的 HTTPS。它通過提供一個軟體客戶端 Certbot,嘗試自動化大部分(如果不是全部)所需的步驟,從而簡化了整個獲取和安裝憑證的過程。目前,在 Apache 和 Nginx 上,獲取和安裝憑證的整個過程都是完全自動化的。

在本教程中,您將使用 Certbot 為 Rocky Linux 9 的 Nginx 獲取免費的 SSL 憑證,並設定您的憑證以自動續訂。

本教程將使用單獨的 Nginx 伺服器配置文件而不是默認文件。您應該為每個域創建新的 Nginx 伺服器塊文件,因為這有助於避免常見的錯誤並保持默認文件作為備用配置。

先決條件

要按照本教程進行,您將需要:

  • 按照此教程設置的一台Rocky Linux 9伺服器,包括使用Rocky Linux 9的初始伺服器設置教程,其中包括一個啟用sudo的非root使用者和一個防火牆。

  • 已註冊的域名。本教程將始終使用example.com。您可以從Namecheap購買域名,使用Freenom免費獲得一個,或使用您選擇的域名註冊機構。

  • 為您的伺服器設置以下兩條DNS記錄。如果您使用DigitalOcean,請參閱我們的DNS文檔以獲取有關如何添加它們的詳細信息。

    • 一個A記錄,指向您伺服器的公共IP地址:example.com
    • 一個A記錄,指向您伺服器的公共IP地址:www.example.com
  • 按照 在 Rocky Linux 9 上安裝 Nginx 中的步驟安裝 Nginx。確保您的域名有一個服務器塊。本教程將以 /etc/nginx/sites-available/example.com 為例。

第 1 步 — 安裝 Certbot

首先,您需要安裝 certbot 軟件包。以非 root 用戶登錄到您的 Rocky Linux 8 機器:

  1. ssh sammy@your_server_ip

certbot 軟件包不是默認通過包管理器提供的。您需要啟用 EPEL 存儲庫以安裝 Certbot。

要添加 Rocky Linux 9 EPEL 存儲庫,運行以下命令:

  1. sudo dnf install epel-release

在要求確認安裝時,輸入 y 並按 Enter。

現在您可以訪問額外的存儲庫,安裝所有需要的軟件包:

  1. sudo dnf install certbot python3-certbot-nginx

這將安裝 Certbot 本身以及 Certbot 的 Nginx 插件,這是運行程序所需的。

安裝過程將要求您匯入 GPG 金鑰。確認以完成安裝。

現在您已經安裝了 Certbot,讓我們運行它以獲得證書。

第2步 — 確認 Nginx 的配置

Certbot 需要能夠在您的 Nginx 配置中找到正確的 server 區塊,以便能夠自動配置 SSL。具體而言,它通過查找與您請求證書的域匹配的 server_name 指令來執行此操作。

如果您按照 Nginx 安裝教程中的 server block 設置步驟進行操作,您應該在 /etc/nginx/conf.d/example.com 有一個用於您的域的 server block,並且 server_name 指令已經適當設置。

要檢查,使用 nano 或您喜歡的文本編輯器打開域的配置文件:

  1. sudo nano /etc/nginx/conf.d/example.com

找到現有的 server_name 行。它應該是這樣的:

/etc/nginx/conf.d/example.com
...
server_name example.com www.example.com;
...

如果是,退出編輯器,然後繼續進入下一步。

如果不是,請更新以匹配。然後保存文件,退出編輯器,並驗證配置編輯的語法。

  1. sudo nginx -t

如果您收到错误,请重新打开服务器块文件并检查是否有任何拼写错误或缺少字符。一旦您的配置文件语法正确,重新加载Nginx以加载新的配置:

  1. sudo systemctl reload nginx

Certbot现在可以找到正确的server块并自动更新它。

接下来,让我们更新防火墙以允许HTTPS流量。

步骤3 — 更新防火墙规则

由于您的先决设置启用了firewalld,您需要调整防火墙设置以允许外部连接到您的Nginx Web服务器。

要检查已启用哪些服务,请运行以下命令:

  1. sudo firewall-cmd --permanent --list-all

您将收到如下输出:

Output
public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

如果在服务列表中看不到http,请通过运行以下命令启用它:

  1. sudo firewall-cmd --permanent --add-service=http

要允许https流量,请运行以下命令:

  1. sudo firewall-cmd --permanent --add-service=https

要应用更改,您需要重新加载防火墙服务:

  1. sudo firewall-cmd --reload

现在您已经打开了服务器以接收https流量,您可以运行Certbot并获取您的证书了。

步骤4 — 获取SSL证书

Certbot 通過插件提供了多種獲取 SSL 憑證的方法。Nginx 插件將負責在必要時重新配置 Nginx 並重新加載配置。要使用此插件,請輸入以下內容:

  1. sudo certbot --nginx -d example.com -d www.example.com

這會運行 certbot,並使用 --nginx 插件,使用 -d 來指定憑證需要有效的域名。

運行該命令時,將提示您輸入電子郵件地址並同意服務條款。完成後,您應該會看到一條消息,告訴您過程成功,以及您的憑證存儲在哪裡:

Output
Successfully 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-12-15. 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/nginx/conf.d/your_domain.conf Successfully deployed certificate for www.your_domain to /etc/nginx/conf.d/your_domain.conf Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain

您的憑證已下載、安裝和加載,您的 Nginx 配置現在將自動將所有 Web 請求重定向到 https://。嘗試重新加載您的網站並注意您的瀏覽器的安全指示器。它應該顯示該站點已經得到了正確的安全保護,通常會有一個鎖定圖標。如果使用 SSL 實驗室服務器測試 測試您的服務器,它將獲得一個 A 級。

最後,讓我們通過測試續約過程來完成。

步驟 5 — 驗證 Certbot 自動續約

Let’s Encrypt 證書有效期為 90 天,但建議您每 60 天更新一次證書,以留有一定的錯誤空間。Certbot Let’s Encrypt 客戶端具有一個 `renew` 命令,它會自動檢查當前安裝的證書,並嘗試在距到期日期不足 30 天時更新它們。

您可以通過運行此命令來測試證書的自動續期:

  1. sudo certbot renew --dry-run

輸出將類似於此:

Output
Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/your_domain.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not due for renewal, but simulating renewal for dry run Plugins selected: Authenticator nginx, Installer nginx Renewing an existing certificate Performing the following challenges: http-01 challenge for monitoring.pp.ua Waiting for verification... Cleaning up challenges - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new certificate deployed with reload of nginx server; fullchain is /etc/letsencrypt/live/your_domain/fullchain.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/your_domain/fullchain.pem (success) ...

請注意,如果您創建了一個帶有多個域的捆綁證書,則輸出中只會顯示基本域名,但續訂將適用於此證書中包括的所有域。

A practical way to ensure your certificates will not get outdated is to create a cron job that will periodically execute the automatic renewal command for you. Since the renewal first checks for the expiration date and only executes the renewal if the certificate is less than 30 days away from expiration, it is safe to create a cron job that runs every week, or even every day.

編輯 crontab 以創建一個新作業,該作業每天運行兩次續訂。要編輯 root 用戶的 crontab,運行:

  1. sudo crontab -e

您的文本編輯器將打開默認的 crontab,這是一個空文本文件。按 i 進入插入模式,然後添加以下行:

crontab
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew --quiet

完成後,按 ESC 鍵退出插入模式,然後按 :wqENTER 鍵保存並退出文件。要了解有關文本編輯器 Vi 及其後繼者 Vim 的更多信息,請查看我們的 在雲服務器上安裝和使用 Vim 文本編輯器 教程。

這將創建一個新的 cron 作業,每天中午和午夜執行。 `python -c 'import random; import time; time.sleep(random.random() * 3600)'` 將在一小時內選擇一個隨機分鐘進行續訂任務。

Certbot 的 renew 命令會檢查系統上安裝的所有憑證,並更新那些在不到30天後到期的憑證。--quiet 參數告訴 Certbot 不要輸出資訊或等待使用者輸入。

關於續訂的更詳細資訊可以在 Certbot 文件中找到。

結論

在這個指南中,您安裝了 Let’s Encrypt 客戶端 Certbot,下載了您域名的 SSL 憑證,並設置了自動續訂憑證。如果您對使用 Certbot 有任何問題,可以參考官方的 Certbot 文件

您也可以定期查看官方的 Let’s Encrypt 博客以獲取重要的更新。

Source:
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-rocky-linux-9