如何在 Ubuntu 20.04 上使用 Let’s Encrypt 保护 Apache

介绍

Let’s Encrypt 是一个证书颁发机构 (CA),它有助于获取和安装免费的TLS/SSL 证书,从而在 Web 服务器上启用加密的 HTTPS。它通过提供软件客户端 Certbot 来简化流程,该客户端尝试自动执行大部分(如果不是全部)所需步骤。目前,获取和安装证书的整个过程在 Apache 和 Nginx 上都是完全自动化的。

在本指南中,我们将使用Certbot为 Ubuntu 20.04 上的 Apache 获取免费 SSL 证书,并确保将此证书设置为自动更新。

本教程使用单独的虚拟主机文件而不是 Apache 的默认配置文件来设置将由 Let’s Encrypt 保护的网站。我们建议为服务器中托管的每个域创建新的 Apache 虚拟主机文件,因为这有助于避免常见错误并将默认配置文件保留为后备设置。

先决条件

要学习本教程,您需要:

  • 按照Ubuntu 20.04教程的初始服务器设置设置一台 Ubuntu 20.04 服务器,包括 sudo 非 root 用户和防火墙。

  • 完全注册的域名。本教程将始终使用your_domain作为示例。你可以购买一个域名Namecheap,免费获得一个在Freenom,或使用你选择的域名注册商。

  • 为您的服务器设置的以下两个 DNS 记录。您可以按照DigitalOcean DNS的介绍了解有关如何添加它们的详细信息。

    • your_domain指向您服务器的公共 IP 地址的 A 记录
    • 指向您服务器的公共 IP 地址的 A 记录www.your_domain
  • 按照如何在 Ubuntu 20.04 上安装 Apache 安装 Apache确保您的域有一个虚拟主机文件本教程将用作示例。/etc/apache2/sites-available/your_domain.conf

步骤 1 — 安装 Certbot

为了通过 Let’s Encrypt 获得 SSL 证书,我们首先需要在您的服务器上安装 Certbot 软件。为此,我们将使用默认的 Ubuntu 软件包存储库。

我们需要两个包:certbot, 和python3-certbot-apache. 后者是一个将 Certbot 与 Apache 集成的插件,可以使用单个命令在您的 Web 服务器中自动获取证书和配置 HTTPS。

  • sudo apt install certbot python3-certbot-apache

系统将提示您按Y,然后按 确认安装ENTER

Certbot 现在已安装在您的服务器上。在下一步中,我们将验证 Apache 的配置以确保您的虚拟主机设置正确。这将确保certbot客户端脚本能够检测您的域并重新配置您的 Web 服务器以自动使用您新生成的 SSL 证书。

步骤 2 — 检查您的 Apache 虚拟主机配置

为了能够为您的 Web 服务器自动获取和配置 SSL,Certbot 需要在您的 Apache 配置文件中找到正确的虚拟主机。您的服务器域名将从配置块中定义ServerNameServerAlias指令中检索VirtualHost

如果您按照Apache 安装教程中虚拟主机设置步骤进行操作,您应该为您的域设置一个 VirtualHost 块并且已经正确设置指令。/etc/apache2/sites-available/your_domain.confServerNameServerAlias

要检查这一点,请使用nano或您喜欢的文本编辑器打开您的域的虚拟主机文件

  • sudo nano /etc/apache2/sites-available/your_domain.conf

找到现有的ServerNameServerAlias行。它们应该如下所示:

/etc/apache2/sites-available/your_domain.conf
...
ServerName your_domain
ServerAlias www.your_domain
...

如果你已经有了ServerNameServerAlias建立这样,你可以退出文本编辑器,并移动到下一个步骤。如果您正在使用nano,则可以通过键入CTRL+X、 然后YENTER进行确认来退出

如果您当前的虚拟主机配置与示例不匹配,请相应地更新它。完成后,保存文件并退出编辑器。然后,运行以下命令来验证您的更改:

  • sudo apache2ctl configtest

你应该得到一个Syntax OK作为回应。如果出现错误,请重新打开虚拟主机文件并检查是否有任何拼写错误或缺失字符。一旦您的配置文件的语法正确,请重新加载 Apache 以使更改生效:

  • sudo systemctl reload apache2

通过这些更改,Certbot 将能够找到正确的 VirtualHost 块并更新它。

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

步骤 3 — 允许 HTTPS 通过防火墙

如果您按照先决条件指南的建议启用了 UFW 防火墙,则需要调整设置以允许 HTTPS 流量。安装后,Apache 会注册一些不同的 UFW 应用程序配置文件。我们可以利用Apache 完整配置文件在您的服务器上允许 HTTP 和 HTTPS 流量。

要验证您的服务器当前允许什么样的流量,您可以使用:

  • sudo ufw status

如果您遵循了我们的 Apache 安装指南之一,您的输出应如下所示,这意味着当前仅80允许端口上的HTTP 流量

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”配置文件并删除冗余的“Apache”配置文件:

  • sudo ufw allow 'Apache Full'
  • sudo ufw delete allow 'Apache'

您的状态现在将如下所示:

  • sudo ufw status
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 并获取您的证书。

第 4 步 – 获取 SSL 证书

Certbot 提供了多种通过插件获取 SSL 证书的方式。Apache 插件将负责重新配置 Apache 并在必要时重新加载配置。要使用此插件,请键入以下内容:

  • sudo certbot --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,然后确认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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

接下来,系统会询问您是否愿意与电子前沿基金会共享您的电子邮件以接收新闻和其他信息。如果您不想订阅他们的内容,请键入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 虚拟主机配置中自动获取的,这就是为什么确保您在虚拟主机中配置正确ServerNameServerAlias设置很重要的原因如果您想为所有列出的域名启用 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): 

你会看到这样的输出:

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for your_domain
http-01 challenge for www.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 保留为访问您网站的单独方法。

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 的配置就完成了,您将看到关于新证书的最终说明、生成文件的位置以及如何使用分析证书真实性的外部工具测试您的配置:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://your_domain and
https://www.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=www.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 2020-07-27. 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"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - 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 Server Test从外部服务的角度验证您的证书的等级并获取有关它的详细信息。

在下一步也是最后一步中,我们将测试 Certbot 的自动续订功能,该功能可确保您的证书在到期日期之前自动续订。

步骤 5 — 验证 Certbot 自动续订

Let’s Encrypt 的证书有效期只有九十天。这是为了鼓励用户自动化他们的证书更新过程,并确保误用的证书或被盗的密钥尽早过期。

certbot我们安装包通过包含一个更新脚本来处理更新/etc/cron.d,该脚本systemctl名为服务管理certbot.timer此脚本每天运行两次,并将自动更新任何在到期后三十天内的证书。

要检查此服务的状态并确保它处于活动状态并正在运行,您可以使用:

  • sudo systemctl status certbot.timer

你会得到类似这样的输出:

Output
● certbot.timer - Run certbot twice daily Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled) Active: active (waiting) since Tue 2020-04-28 17:57:48 UTC; 17h ago Trigger: Wed 2020-04-29 23:50:31 UTC; 12h left Triggers: ● certbot.service Apr 28 17:57:48 fine-turtle systemd[1]: Started Run certbot twice daily.

要测试更新过程,您可以使用以下命令进行试运行certbot

  • sudo certbot renew --dry-run

如果您没有看到任何错误,则说明一切就绪。必要时,Certbot 将更新您的证书并重新加载 Apache 以获取更改。如果自动续订过程失败,Let’s Encrypt 将向您指定的电子邮件发送一条消息,在您的证书即将到期时向您发出警告。

结论

在本教程中,您已经安装了 Let’s Encrypt 客户端certbot,为您的域配置并安装了 SSL 证书,并确认 Certbot 的自动续订服务在systemctl. 如果您对使用 Certbot 有其他疑问,可以从他们的文档开始。

觉得文章有用?

点个广告表达一下你的爱意吧 !😁