如何在 Rocky Linux 9 上使用 Let's Encrypt 加密 Nginx

介绍

Let's Encrypt 是一个证书授权机构(CA),它提供了一个可访问的方式来获取和安装免费的 TLS/SSL 证书,从而在 Web 服务器上允许加密 HTTPS。它通过提供一个软件客户端,Certbot,它试图自动化大多数(如果不是所有)所需的步骤来简化这个过程。

在本教程中,您将使用 Certbot 获得在 Rocky Linux 9 上免费的 Nginx SSL 证书,并设置您的证书自动更新。

本教程将使用单独的 Nginx 服务器配置文件,而不是默认文件. 您应该为每个域创建新的 Nginx 服务器封锁文件,因为它有助于避免常见的错误,并将默认文件作为倒退配置。

前提条件

要遵循本教程,您将需要:

  • 一个 Rocky Linux 9 服务器设置通过以下 初始服务器设置为 Rocky Linux 9 教程,包括一个 sudo-enabled non-root 用户和防火墙.
  • 注册域名. 本教程将使用example.com在整个过程中。您可以从 Namecheap购买域名,获得一个免费的 Freenom,或使用您选择的域名注册器。
  • 两个下列 DNS 记录为您的服务器设置。 如果您正在使用 DigitalOcean,请参阅我们的 DNS 文档 有关如何添加它们的详细信息。
  • 一个记录与 `example.com 指

步骤1:安装Certbot

首先,您需要安装certbot软件包. 登录您的 Rocky Linux 8 机器作为您的非根用户:

1[environment local]
2ssh sammy@your_server_ip

默认情况下,certbot包无法通过包管理器使用,您需要启用 EPEL存储库来安装Certbot。

若要添加 Rocky Linux 9 EPEL 存储库,请执行以下命令:

1sudo dnf install epel-release

当被要求确认安装时,键入并输入y

现在你可以访问额外的存储库,安装所有所需的包:

1sudo dnf install certbot python3-certbot-nginx

这将安装Certbot本身和Certbot的Nginx插件,这需要运行该程序。

安装过程会询问您是否要导入 GPG 密钥. 请确认,以便完成安装。

现在你已经安装了Certbot,让我们运行它来获得证书。

步骤 2 — 确认 Nginx 的配置

Certbot 需要能够在您的 Nginx 配置中找到正确的服务器块,以便能够自动配置 SSL。

如果您遵循了 Nginx 安装教程中的服务器块设置步骤,则您应该在 /etc/nginx/conf.d/example.com 上为您的域设置服务器块,并且已设置适当的 server_name 指令。

要检查,请使用nano或您最喜欢的文本编辑器打开您的域的配置文件:

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

查找现有的server_name行,它应该是这样的:

1[label /etc/nginx/conf.d/example.com]
2...
3server_name example.com www.example.com;
4...

如果是,请退出编辑器并转到下一步。

如果不是,则更新以匹配,然后保存文件,离开编辑器,并验证配置编辑的语法:

1sudo nginx -t

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

1sudo systemctl reload nginx

Certbot 现在可以找到正确的服务器块,并自动更新。

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

步骤 3 – 更新防火墙规则

由于您的预先设置允许防火墙,您需要调整防火墙设置,以便在您的 Nginx Web 服务器上允许外部连接。

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

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

你会收到这样的输出:

 1[secondary_label Output]
 2public
 3  target: default
 4  icmp-block-inversion: no
 5  interfaces:
 6  sources:
 7  services: cockpit dhcpv6-client http ssh
 8  ports:
 9  protocols:
10  masquerade: no
11  forward-ports:
12  source-ports:
13  icmp-blocks:
14  rich rules:

如果您在服务列表中看不到http,请通过运行:

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

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

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

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

1sudo firewall-cmd --reload

现在,您已将服务器打开到 https 流量,您已经准备好运行 Certbot 并获取证书。

第4步:获取SSL证书

Certbot 提供了通过插件获取 SSL 证书的多种方式。 Nginx 插件将负责重新配置 Nginx 并在必要时重新加载配置。

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

这将运行certbot--nginx插件,使用-d来指定您需要证书有效的域名。

在运行命令时,您将被要求输入电子邮件地址并同意服务条款. 这样做后,您应该看到一个消息,告诉您过程成功,您的证书存储在哪里:

 1[secondary_label Output]
 2Successfully received certificate.
 3Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
 4Key is saved at:         /etc/letsencrypt/live/your_domain/privkey.pem
 5This certificate expires on 2022-12-15.
 6These files will be updated when the certificate renews.
 7Certbot has set up a scheduled task to automatically renew this certificate in the background.
 8
 9Deploying certificate
10Successfully deployed certificate for your_domain to /etc/nginx/conf.d/your_domain.conf
11Successfully deployed certificate for www.your_domain to /etc/nginx/conf.d/your_domain.conf
12Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain
13

您的证书已下载、安装和加载,您的 Nginx 配置现在将自动重定向所有网页请求到 https://. 尝试重新加载您的网站并注意您的浏览器的安全指标。 它应该表明该网站是正确保护的,通常用锁定图标。 如果您使用 SSL Labs Server Test测试您的服务器,它将获得 A级别。

让我们通过测试更新过程来结束。

步骤 5 – 验证 Certbot 自动更新

Let’s Encrypt 证书有效期为 90 天,但建议您每隔 60 天更新证书,以便出现错误。Certbot Let’s Encrypt 客户端有一个更新命令,该命令会自动检查当前安装的证书并尝试更新它们,如果它们在到期日期不到 30 天。

您可以通过运行此命令测试证书的自动更新:

1sudo certbot renew --dry-run

结果将类似于此:

 1[secondary_label Output]
 2Saving debug log to /var/log/letsencrypt/letsencrypt.log
 3
 4- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 5Processing /etc/letsencrypt/renewal/your_domain.conf
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7Cert not due for renewal, but simulating renewal for dry run
 8Plugins selected: Authenticator nginx, Installer nginx
 9Renewing an existing certificate
10Performing the following challenges:
11http-01 challenge for monitoring.pp.ua
12Waiting for verification...
13Cleaning up challenges
14
15- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16new certificate deployed with reload of nginx server; fullchain is
17/etc/letsencrypt/live/your_domain/fullchain.pem
18- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19
20- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21** DRY RUN: simulating 'certbot renew' close to cert expiry
22**          (The test certificates below have not been saved.)
23
24Congratulations, all renewals succeeded. The following certs have been renewed:
25  /etc/letsencrypt/live/your_domain/fullchain.pem (success)
26...

请注意,如果您创建了包含多个域的组合证书,则输出中只会显示基本域名,但更新将适用于该证书中包含的所有域名。

确保您的证书不会过时的一个实用方法是创建一个 cron任务,该任务会定期为您执行自动更新命令。

编辑 crontab 以创建一个新工作,每天运行更新两次. 为了编辑 root 用户的 crontab,运行:

1sudo crontab -e

您的文本编辑器将打开默认 crontab,这是一个空文本文件在这个时刻。

1[label crontab]
20 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 的更新命令会检查系统上安装的所有证书,并更新任何在不到 30 天内到期的证书。

有关更新的详细信息可在Certbot文档(https://certbot.eff.org/docs/using.html#renewal)中找到。

结论

在本指南中,您安装了 Let's Encrypt 客户端 Certbot,下载了您的域的 SSL 证书,并设置了自动证书更新。

您还可以检查官方的 Let’s Encrypt 博客 有关重要更新。

Published At
Categories with 技术
comments powered by Disqus