介绍
Let's Encrypt是一个证书授权机构(CA),它为 Transport Layer Security (TLS) 加密提供免费证书,它通过提供软件客户端 - Certbot来简化创建,验证,签署,安装和更新证书的过程。
在本教程中,您将从 Let’s Encrypt设置一个 TLS/SSL 证书,在运行 Web 服务器的 Rocky Linux 8 服务器上运行 Nginx。
前提条件
为了完成本指南,您将需要:
一个Rocky Linux 8初始服务器设置的服务器(Rocky Linux 8初始服务器设置指南)(https://andsky.com/tech/tutorials/initial-server-setup-with-rocky-linux-8),包括一个具有sudo
特权和防火墙的非根用户。
- Nginx安装在Rocky Linux 8服务器上,配置了服务器块。您可以通过遵循我们的教程(How To Install Nginx on Rocky Linux 8)(https://andsky.com/tech/tutorials/how-to-install-nginx-on-rocky-linux-8)来学习如何设置这个服务器。
- 一个完全注册的域名。本教程将使用
your_domain
作为整个例子。您可以在 Namecheap上购买域名,在 Freenom上免费获取一个,或者使用您选择的公共域名注册商。 _
步骤 1 – 安装 Certbot Let’s Encrypt 客户端
首先,您需要安装certbot
软件包. 登录您的 Rocky Linux 8 机器作为您的非根用户:
1[environment local]
2ssh sammy@your_server_ip
默认情况下,certbot
包无法通过包管理器使用,您需要启用 EPEL存储库来安装Certbot。
要添加 Rocky Linux 8 EPEL 存储库,请使用 dnf install
:
1sudo dnf install epel-release
当被要求确认安装时,键入并输入y
。
现在你可以访问额外的存储库,安装所有所需的包:
1sudo dnf install certbot python3-certbot-nginx
这将安装Certbot本身和Certbot的Nginx插件,这需要运行该程序。
安装过程会询问您是否要导入 GPG 密钥. 请确认,以便完成安装。
您现在已经安装了 Let's Encrypt 客户端,但在获得证书之前,您需要确保所有所需的端口都打开。
第2步:更新防火墙规则
由于您的预先设置允许防火墙
,您需要调整防火墙设置,以便在您的 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并收集你的证书。
步骤三:获得证书
现在您可以为您的域名申请SSL证书。
在使用certbot
Let’s Encrypt 客户端生成 Nginx 的 SSL 证书时,客户端将自动获得并安装一个新的 SSL 证书,该证书适用于作为参数提供的域。
如果您想要安装一个适用于多个域或子域的单一证书,则可以将其作为附加参数传输到命令中。参数列表中的第一个域名将是使用 Let’s Encrypt 创建该证书的 base 域名,因此您将先在列表中传输顶级域名,然后再添加任何附加的子域或副名称:
1sudo certbot --nginx -d your_domain -d www.your_domain
这将运行certbot
与--nginx
插件,而基础域将是your_domain
。为了执行交互式安装并获得仅覆盖一个域的证书,请运行certbot
命令:
1sudo certbot --nginx -d your_domain
该certbot
实用程序还可以在证书请求程序中向您请求域信息。 若要使用此功能,请拨打certbot
而无域名:
1sudo certbot --nginx
您将收到一步一步的指南来定制您的证书选项。Certbot将要求您提供电子邮件地址以恢复丢失的密钥和通知,并同意服务条款。如果您没有在命令行中指定您的域名,Certbot将寻找一个server_name
指令,并为您提供发现的域名列表。
为了提高安全性,Certbot 会自动配置在端口80
上的所有流量重定向到443
。
当安装顺利完成时,您将收到类似于此的消息:
1[secondary_label Output]
2IMPORTANT NOTES:
3 - Congratulations! Your certificate and chain have been saved at:
4 /etc/letsencrypt/live/your_domain/fullchain.pem
5 Your key file has been saved at:
6 /etc/letsencrypt/live/your_domain/privkey.pem
7 Your cert will expire on 2022-11-14. To obtain a new or tweaked
8 version of this certificate in the future, simply run certbot again
9 with the "certonly" option. To non-interactively renew *all* of
10 your certificates, run "certbot renew"
11 - If you like Certbot, please consider supporting our work by:
12
13 Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
14 Donating to EFF: https://eff.org/donate-le
生成的证书文件将在您的基域名为/etc/letsencrypt/live
目录的子目录中可用。
现在你已经完成了使用Certbot,你可以检查你的SSL证书的状态,通过在你喜欢的网页浏览器中打开下面的链接来验证你的SSL证书的状态(不要忘了用你的基本域名替换your_domain
):
1https://www.ssllabs.com/ssltest/analyze.html?d=your_domain
本网站包含由 SSL Labs进行的SSL测试,该测试将自动启动. 在此写作时,默认设置将给出一个 A评级。
您现在可以使用https
前缀访问您的网站,但是,您必须定期更新证书以保持此设置。
步骤4:设置自动更新
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
来离开插入模式,然后按:wq
和ENTER
来保存和退出文件. 有关文本编辑器 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 博客 有关重要更新。