介绍
Let’s Encrypt是一个证书颁发机构(CA),它便于获取和安装免费的TLS/SSL证书,从而在网页服务器上启用加密的HTTPS。它通过提供一个软件客户端Certbot来简化流程,该客户端尝试自动化大部分(如果不是全部)所需的步骤。目前,获取和安装证书的整个流程在Apache和Nginx上已经完全自动化。
在本指南中,您将使用Certbot为Ubuntu上的Apache获取免费的SSL证书,并确保此证书能够自动续期。
本教程使用单独的虚拟主机文件,而不是Apache的默认配置文件来设置将由Let’s Encrypt保护的网站。我们建议为服务器中托管的每个域名创建新的Apache虚拟主机文件,因为这有助于避免常见错误,并保持默认配置文件作为后备设置。
如何在Ubuntu上使用Let’s Encrypt保护Apache
先决条件
要遵循本教程,您需要:
-
一台安装了非root用户并具有
sudo
管理权限和防火墙功能的Ubuntu服务器。您可以按照我们的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
然后回车
确认安装。
Certbot 现已安装在您的服务器上。在下一步中,您将验证 Apache 的配置,以确保您的虚拟主机设置正确。这将确保 certbot
客户端脚本能够检测到您的域名,并自动重新配置您的 Web 服务器以使用您新生成的 SSL 证书。
步骤 2 — 检查您的 Apache 虚拟主机配置
为了自动为您的 Web 服务器获取并配置 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
,您可以退出文本编辑器并继续下一步。如果您的当前虚拟主机配置与示例不匹配,请相应更新。如果您使用的是nano
,可以通过按CTRL+X
,然后Y
和ENTER
来退出,以确认任何更改,如果有更改的话。然后,运行以下命令以验证您的更改:
您应该收到Syntax OK
作为响应。如果您遇到错误,请重新打开虚拟主机文件并检查任何拼写错误或缺少的字符。一旦您的配置文件的语法正确,请重新加载Apache,以便更改生效:
有了这些更改,Certbot将能够找到正确的VirtualHost块并更新它。
接下来,您将更新防火墙以允许HTTPS流量。
步骤3 — 允许HTTPS通过防火墙
如果您启用了UFW防火墙,如前提指南所推荐,您需要调整设置以允许HTTPS流量。在安装过程中,Apache注册了几个不同的UFW应用程序配置文件。您可以利用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完整”配置文件:
然后删除多余的“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的证书仅有效九十天。这是为了鼓励用户自动化他们的证书续期过程,同时也确保被误用或被盗的证书密钥会更快地过期。
您安装的certbot
软件包通过在/etc/cron.d
中包含一个续期脚本来处理续期,该脚本由名为certbot.timer
的systemctl
服务管理。这个脚本每天运行两次,并将自动续期任何在到期前30天的证书。
要检查此服务状态并确保其处于活动状态,请运行以下命令:
您的输出将类似于以下内容:
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