如何在 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 的非根用户和防火墙。 <李> 注册域名。本教程将始终使用
- 带有
example.com
的 A 记录指向您服务器的公共 IP 地址。 - 带有
www.example.com
的 A 记录指向您服务器的公共 IP 地址。
example.com
。您可以从 Freenom 购买域名,或使用您选择的域名注册商。 <李> 为您的服务器设置了以下两个 DNS 记录。如果您使用的是 DigitalOcean,请参阅我们的 DNS 文档以了解有关如何添加它们的详细信息。 按照如何在 Rocky Linux 9 上安装 Nginx 安装 Nginx。确保你的域有一个服务器块。本教程将以 /etc/nginx/sites-available/example.com
为例。
第 1 步 — 安装 Certbot
首先,您需要安装certbot
软件包。以非 root 用户身份登录到 Rocky Linux 8 机器:
- ssh sammy@your_server_ip
默认情况下,certbot
包无法通过包管理器使用。您需要启用 EPEL 存储库才能安装 Certbot。
要添加 Rocky Linux 9 EPEL 存储库,请运行以下命令:
- sudo dnf install epel-release
当要求确认安装时,键入并输入 y
。
现在您可以访问额外的存储库,安装所有必需的包:
- sudo dnf install certbot python3-certbot-nginx
这将安装 Certbot 本身和 Certbot 的 Nginx 插件,这是运行该程序所必需的。
安装过程将询问您是否导入 GPG 密钥。确认它以便安装可以完成。
现在您已经安装了 Certbot,让我们运行它来获取证书。
第 2 步 — 确认 Nginx 的配置
Certbot 需要能够在您的 Nginx 配置中找到正确的 server
块,以便它能够自动配置 SSL。具体来说,它通过查找与您请求证书的域相匹配的 server_name
指令来实现。
如果您遵循 Nginx 安装教程中的服务器块设置步骤,您应该在 /etc/nginx/conf.d/example.com
处为您的域设置一个服务器块 已经正确设置了 server_name
指令。
要检查,请使用 nano
或您最喜欢的文本编辑器打开您域的配置文件:
- sudo nano /etc/nginx/conf.d/example.com
查找现有的 server_name
行。它应该是这样的:
...
server_name example.com www.example.com;
...
如果是,请退出您的编辑器并继续下一步。
如果不匹配,请更新它以匹配。然后保存文件,退出编辑器,并验证配置编辑的语法:
- sudo nginx -t
如果出现错误,请重新打开服务器块文件并检查是否有拼写错误或缺失字符。配置文件的语法正确后,重新加载 Nginx 以加载新配置:
- sudo systemctl reload nginx
Certbot 现在可以找到正确的 server
块并自动更新它。
接下来,让我们更新防火墙以允许 HTTPS 流量。
第 3 步 — 更新防火墙规则
由于您的先决条件设置启用了 firewalld
,因此您需要调整防火墙设置以允许 Nginx Web 服务器上的外部连接。
要检查哪些服务已启用,请运行以下命令:
- sudo firewall-cmd --permanent --list-all
你会收到这样的输出:
Outputpublic
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
,请运行以下命令启用它:
- sudo firewall-cmd --permanent --add-service=http
要允许 https
流量,请运行以下命令:
- sudo firewall-cmd --permanent --add-service=https
要应用更改,您需要重新加载防火墙服务:
- sudo firewall-cmd --reload
现在您已经为 https 流量打开了您的服务器,您已经准备好运行 Certbot 并获取您的证书。
第 4 步 — 获取 SSL 证书
Certbot 提供了多种通过插件获取 SSL 证书的方式。 Nginx 插件将负责重新配置 Nginx 并在必要时重新加载配置。要使用此插件,请键入以下内容:
- sudo certbot --nginx -d example.com -d www.example.com
这将使用 --nginx
插件运行 certbot
,使用 -d
指定您需要证书有效的域名。
运行命令时,系统会提示您输入电子邮件地址并同意服务条款。执行此操作后,您应该会看到一条消息,告诉您该过程已成功以及您的证书存储在何处:
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-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 Labs 服务器测试来测试您的服务器,它将获得 A 级。
让我们通过测试更新过程来结束。
第 5 步 — 验证 Certbot 自动续订
Let's Encrypt 证书的有效期为 90 天,但建议您每 60 天更新一次证书以允许误差。 Certbot Let’s Encrypt 客户端有一个 renew
命令,它会自动检查当前安装的证书,并在距离到期日期不到 30 天时尝试更新它们。
您可以通过运行以下命令来测试证书的自动续订:
- sudo certbot renew --dry-run
输出将类似于:
OutputSaving 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)
...
请注意,如果您创建了一个包含多个域的捆绑证书,输出中只会显示基本域名,但续订将适用于该证书中包含的所有域。
确保您的证书不会过时的一种实用方法是创建一个 cron
作业,它会定期为您执行自动续订命令。由于续订首先检查到期日期,并且仅在证书距离到期不到 30 天时才执行续订,因此创建每周甚至每天运行的 cron 作业是安全的。
编辑 crontab 以创建一个每天运行两次更新的新作业。要为 root 用户编辑 crontab,请运行:
- sudo crontab -e
您的文本编辑器将打开默认的 crontab,此时它是一个空文本文件。按 i
进入插入模式并添加以下行:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew --quiet
完成后,按 ESC
退出插入模式,然后按 :wq
和 ENTER
保存并退出文件。要了解有关文本编辑器 Vi 及其后继 Vim 的更多信息,请查看我们的在云服务器上安装和使用 Vim 文本编辑器教程。
这将创建一个新的 cron 作业,该作业将在每天中午和午夜执行。 <代码>python -c 随机导入;导入时间; time.sleep(random.random() * 3600) 将在一小时内随机选择一分钟进行续订任务。
Certbot 的 renew
命令将检查系统上安装的所有证书,并更新任何设置为在 30 天内到期的证书。 --quiet
告诉 Certbot 不要输出信息或等待用户输入。
有关续订的更多详细信息,请参阅 Certbot 文档。
结论
在本指南中,您安装了 Let's Encrypt 客户端 Certbot,为您的域下载了 SSL 证书,并设置了自动证书续订。如果您对使用 Certbot 有任何疑问,可以查看 Certbot 官方文档。
您还可以不时查看官方的 Let's Encrypt 博客以获取重要更新。