如何在Ubuntu18.04上使用Let'sEncrypt保护Apache

来自菜鸟教程
跳转至:导航、​搜索

介绍

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 用户和防火墙。
  • 完全注册的域名。 本教程将自始至终使用 your_domain 作为示例。 您可以在 Namecheap 上购买一个域名,在 Freenom 上免费获得一个域名,或者使用您选择的域名注册商。
  • 为您的服务器设置了以下两个 DNS 记录。 您可以关注这个DigitalOcean DNS的介绍,详细了解如何添加它们。
    • 带有 your_domain 的 A 记录指向您服务器的公共 IP 地址。
    • 带有 www.your_domain 的 A 记录指向您服务器的公共 IP 地址。
  • 按照 How To Install Apache on Ubuntu 18.04 安装 Apache。 确保您的域具有 虚拟主机文件 。 本教程将以 /etc/apache2/sites-available/your_domain.conf 为例。

第 1 步 — 安装 Certbot

要使用 Let's Encrypt 获得 SSL 证书,您需要在服务器上安装 Certbot 软件。 对于本教程,我们将使用默认的 Ubuntu 软件包存储库来安装 Certbot。

运行以下命令,将安装两个包: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 指令。

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

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

找到现有的 ServerNameServerAlias 行:

/etc/apache2/sites-available/your_domain.conf

...
ServerName your_domain;
SeverAlias 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 配置文件:

sudo ufw allow 'Apache Full'

然后,删除多余的 Apache 配置文件余量:

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 服务条款。 按 AENTER 确认:

OutputPlease 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进行下一步:

OutputWould 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

OutputWhich 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):

您将收到以下输出:

OutputObtaining 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 作为单独的方法来访问您的网站:

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

OutputCongratulations! 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 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: 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 进行试运行来测试更新过程:

sudo certbot renew --dry-run

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

结论

在本教程中,您安装了 Let's Encrypt 客户端 certbot,为您的域配置和安装 SSL 证书,并确认 Certbot 的自动证书续订服务在 systemctl 内处于活动状态。 如果您对使用 Certbot 有其他疑问, 他们的文档 是一个很好的起点。