如何在Ubuntu20.04上使用Let'sEncrypt保护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 的 A 记录指向您服务器的公共 IP 地址。
    • 带有 www.your_domain 的 A 记录指向您服务器的公共 IP 地址。
  • 按照 How To Install Apache on Ubuntu 20.04 安装 Apache。 确保您的域具有 虚拟主机文件 。 本教程将以 /etc/apache2/sites-available/your_domain.conf 为例。

第 1 步 — 安装 Certbot

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

我们需要两个包:certbotpython3-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 配置文件中找到正确的虚拟主机。 您的服务器域名将从 VirtualHost 配置块中定义的 ServerNameServerAlias 指令中检索。

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

要检查这一点,请使用 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 Full 配置文件在您的服务器上允许 HTTP 和 HTTPS 流量。

要验证您的服务器上当前允许哪种流量,您可以使用:

sudo ufw status

如果您遵循我们的 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 Full”配置文件并删除冗余的“Apache”配置文件:

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

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

sudo ufw status
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 并在必要时重新加载配置。 要使用此插件,请键入以下内容:

sudo certbot --apache

此脚本将提示您回答一系列问题以配置您的 SSL 证书。 首先,它会要求您提供有效的电子邮件地址。 此电子邮件将用于更新通知和安全通知:

OutputSaving 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 中包含更新脚本来处理更新,该脚本由名为 certbot.timersystemctl 服务管理。 此脚本每天运行两次,并将自动更新到期后三十天内的任何证书。

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

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 有其他疑问, 他们的文档 是一个很好的起点。