Let’s Encrypt 是一个证书颁发机构 (CA),提供免费获取和安装的方法TLS/SSL 证书,从而在 Web 服务器上启用加密的 HTTPS。它通过提供软件客户端简化了流程,Certbot,它尝试自动化大多数(如果不是全部)所需的步骤。目前,获取和安装证书的整个过程在 Apache 和 Nginx 上都是完全自动化的。
在本教程中,您将使用 Certbot 为 Ubuntu 18.04 上的 Apache 获取免费的 SSL 证书,并验证您的证书是否设置为自动续订。
本教程使用单独的 Apache 虚拟主机文件而不是默认配置文件来设置将由 Let’s Encrypt 保护的网站。我们推荐为服务器中托管的每个域创建新的 Apache 虚拟主机文件,因为它有助于避免常见错误并维护默认文件作为后备设置。
要完成本教程,您将需要:
-
按照此设置一台 Ubuntu 18.04 服务器Ubuntu 18.04 的初始服务器设置教程,包括 sudo 非 root 用户和防火墙。
-
完全注册的域名。本教程将使用你的域名作为整个例子。您可以购买域名名称廉价,免费获得一份Freenom,或使用您选择的域名注册商。
-
为您的服务器设置以下两条 DNS 记录。您可以关注DigitalOcean DNS 简介有关如何添加它们的详细信息。
- An A record with
your_domain
pointing to your server’s public IP address.
- An A record with
www.your_domain
pointing to your server’s public IP address.
-
Apache installed by following How To Install Apache on Ubuntu 18.04. Be sure that you have a virtual host file for your domain. This tutorial will use /etc/apache2/sites-available/your_domain.conf
as an example.
要使用 Let’s Encrypt 获取 SSL 证书,您需要在服务器上安装 Certbot 软件。在本教程中,我们将使用默认的 Ubuntu 软件包存储库来安装 Certbot。
运行以下命令,这将安装两个包:certbot
and python3-certbot-apache
。后者是一个将 Certbot 与 Apache 集成的插件,因此可以使用单个命令自动获取证书并在 Web 服务器中配置 HTTPS:
-
sudo apt installcertbot python3-certbot-apache
按确认安装Y
进而ENTER
接受。
Certbot 现已安装在您的服务器上。接下来,您将验证 Apache 的配置以确保您的虚拟主机设置正确。这确保了certbot
客户端脚本将能够检测您的域并重新配置您的 Web 服务器以自动使用新生成的 SSL 证书。
要自动为您的 Web 服务器获取和配置 SSL,Certbot 需要能够在您的 Apache 配置文件中找到正确的虚拟主机。您的服务器域名将从ServerName
and ServerAlias
您定义的指令VirtualHost
配置块。
If you followed the virtual host set up step in the Apache installation tutorial, you should have a VirtualHost block for your domain at /etc/apache2/sites-available/your_domain.conf
with the ServerName
directive already set appropriately.
要进行检查,请使用以下命令打开您的域的虚拟主机文件nano
或您最喜欢的文本编辑器:
-
sudo nano /etc/apache2/sites-available/your_domain.conf
找到现有的ServerName
and ServerAlias
lines:
/etc/apache2/sites-available/your_domain.conf
...
ServerName your_domain;
SeverAlias www.your_domain
...
If your ServerName
and ServerAlias
已经设置完毕,那么您可以退出文本编辑器并继续下一步。如果您正在使用nano
您可以通过按CTRL + X
then Y
and ENTER
.
如果您当前的虚拟主机配置不匹配,请进行相应更新。之后,保存并退出文本编辑器。然后,验证您的更改:
-
sudoapache2ctl 配置测试
如果虚拟主机文件的语法没有任何错误,您将收到Syntax OK
回复。如果收到错误,请重新打开虚拟主机文件并检查是否有任何拼写错误或缺少字符。配置文件的语法正确后,重新加载 Apache 以加载新配置:
-
sudosystemctl 重新加载 apache2
这些更改生效后,Certbot 将能够找到正确的 VirtualHost 块并进行更新。
接下来,您将更新防火墙以允许 HTTPS 流量。
如果您按照先决条件指南的建议启用了 UFW 防火墙,则需要调整设置以允许 HTTPS 流量。 Apache 注册了一些 UFW 应用程序配置文件,您可以利用阿帕奇完整版配置文件以允许服务器上的 HTTP 和 HTTPS 流量。
通过运行以下命令验证服务器当前允许的流量类型:
-
sudoUFW 状态
如果您遵循我们的 Apache 安装指南之一,您的输出将生成以下内容,这意味着端口上只有 HTTP 流量80
被允许:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
要另外允许 HTTPS 流量进入,请允许Apache Full
轮廓:
-
sudoUFW 允许“阿帕奇完整版”
然后删除多余的Apache
轮廓余量:
-
sudoufw 删除允许'Apache'
再次检查状态:
-
sudoUFW 状态
您应该收到以下输出:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
现在您已准备好运行 Certbot 并获取证书。
Certbot提供了多种通过插件获取SSL证书的方式。 Apache 插件将负责重新配置 Apache 并在必要时重新加载配置。要使用此插件,请运行以下命令:
-
sudo证书机器人--apache
此命令将生成一个提示,其中包含一系列问题以配置您的 SSL 证书。首先,系统会要求您提供有效的电子邮件地址,这是为了续订通知和安全通知:
Output
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): you@your_domain
提供有效的电子邮件地址后,按ENTER
并继续下一步。系统会要求您确认是否同意 Let’s Encrypt 服务条款。按确认A
and ENTER
:
Output
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
接下来,系统会询问您是否愿意与电子前沿基金会共享您的电子邮件以接收新闻和其他信息。如果您不想订阅,请按N
,否则按Y
进而ENTER
继续下一步:
Output
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
and ServerAlias
在您的虚拟主机中配置的设置。如果您想为所有列出的域名启用 HTTPS(推荐),请将提示留空并按ENTER
继续。否则,通过列出每个适当的数字(以逗号和/或空格分隔)来选择要启用 HTTPS 的域,然后按ENTER
:
Output
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: your_domain
2: 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):
您将收到以下输出:
Output
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for your_domain
http-01 challenge for your_domain
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/your_domain-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/your_domain-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf
接下来,系统会要求您选择是否要将 HTTP 流量定向到 HTTPS。这意味着当有人通过未加密通道 (HTTP) 访问您的网站时,他们会自动重定向到您网站的 HTTPS 地址。选择2
启用重定向,或者1
如果您希望将 HTTP 和 HTTPS 作为单独的方法来访问您的网站:
Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
输入您的回复后,Certbot 的配置将完成。您将收到有关新证书的最终说明、生成文件的位置以及如何使用外部工具测试您的配置以分析证书的真实性:
Output
Congratulations! You have successfully enabled https://your_domain
and your_domain
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=your_domain
https://www.ssllabs.com/ssltest/analyze.html?d=your_domain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/your_domain/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your_domain/privkey.pem
Your cert will expire on 2022-03-07. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- 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 实验室服务器测试从外部服务的角度验证您的证书等级并获取有关证书的详细信息。
在下一步中,您将测试 Certbot 的自动续订功能,该功能保证您的证书将在到期日期之前自动续订。
Let’s Encrypt 证书的有效期只有九十天。这是为了鼓励用户自动执行证书续订过程,并确保滥用的证书或被盗的密钥早日过期。
The certbot
您安装的软件包通过包含更新脚本来处理更新/etc/cron.d
,它由一个systemctl
服务调用certbot.timer
。该脚本每天运行两次,并将自动续订过期三十天内的任何证书。
检查此服务的状态并确保其处于活动状态并正在运行:
-
sudosystemctl 状态 certbot.timer
您将收到类似于以下内容的输出:
Output
● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: en
Active: active (waiting) since Tue 2021-12-07 20:04:42 UTC; 1h 45min ago
Trigger: Wed 2021-12-08 11:22:45 UTC; 13h left
Dec 07 20:04:42 encrypt systemd[1]: Started Run certbot twice daily.
通过进行空运行来测试更新过程certbot
:
-
sudocertbot 更新 --dry-run
如果没有收到错误,则一切就绪。必要时,Certbot 将更新您的证书并重新加载 Apache 以获取更改。如果自动续订过程失败,Let’s Encrypt 会向您指定的电子邮件发送一条消息,在您的证书即将过期时向您发出警告。
在本教程中,您安装了 Let’s Encrypt 客户端certbot
,为您的域配置并安装 SSL 证书,并确认 Certbot 的自动证书续订服务在以下时间内处于活动状态systemctl
。如果您对使用 Certbot 有其他疑问,他们的文档是一个很好的起点。