如何在 Ubuntu 18.04 上使用 Let's Encrypt 加密技术保护 Apache

介绍

Let's Encrypt 是一个证书授权机构(CA),它提供了获取和安装免费的 TLS/SSL 证书的方法,从而在 Web 服务器上允许加密 HTTPS. 它通过提供软件客户端 Certbot来简化这个过程,它试图自动化大多数(如果不是所有)所需的步骤。

在本教程中,您将使用Certbot在Ubuntu 18.04上获得免费的SSL证书,并验证您的证书已设置为自动更新。

本教程使用一个独立的Apache虚拟主机文件,而不是默认配置文件来设置网站,该网站将由Let’s Encrypt保护。

前提条件

要完成本教程,您将需要:

一个Ubuntu 18.04服务器设置通过以下 初始服务器设置为Ubuntu 18.04 教程,包括一个 sudo非根用户和一个防火墙

  • 一个完全注册的域名。本教程将使用 your_domain作为一个例子。您可以在 Namecheap上购买域名,在 Freenom上免费获取一个,或者使用您选择的域名注册器
  • 两者均为您的服务器设置了下列 DNS 记录。您可以遵循 DigitalOcean DNS 的介绍 有关如何添加它们的详细信息
  • 一个带有 `yo

步骤1:安装Certbot

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

运行以下命令,将安装两个包: certbotpython3-certbot-apache. 后者是集成 Certbot 与 Apache 的插件,以便通过单个命令自动获取证书并在您的 Web 服务器中配置 HTTPS:

1sudo apt install certbot python3-certbot-apache

通过按Y,然后按ENTER来确认安装。

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

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

为了自动获取和配置您的 Web 服务器的 SSL,Certbot 需要能够在您的 Apache 配置文件中找到正确的虚拟主机。

如果您遵循了 Apache 安装教程中的 虚拟主机设置步骤,则您应该在 /etc/apache2/sites-available/your_domain.conf 域中为您的域设置一个 VirtualHost 区块,并且已设置适当的 ServerName 指令。

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

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

查找现有的ServerNameServerAlias行:

1[label /etc/apache2/sites-available/your_domain.conf]
2...
3ServerName your_domain;
4SeverAlias www.your_domain
5...

如果你的ServerNameServerAlias已经设置,那么你可以离开文本编辑器并继续到下一个步骤. 如果你使用nano,你可以通过按CTRL + X然后YENTER来做到这一点。

如果您当前的虚拟主机配置不匹配,则可以相应地更新。之后,保存并退出文本编辑器。

1sudo apache2ctl configtest

如果您的虚拟主机文件的语法没有任何错误,您将收到一个Syntax OK响应。如果您收到错误,请重新打开虚拟主机文件并检查任何输入或缺少的字符。

1sudo systemctl reload apache2

随着这些变化生效,Certbot 将能够找到正确的 VirtualHost 块并更新它。

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

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

如果您已启用 UFW 防火墙,如前提指南所建议,您需要调整设置以允许 HTTPS 流量。

通过运行以下操作来验证您服务器上目前允许的流量类型:

1sudo ufw status

如果您遵循我们的 Apache 安装指南之一,您的输出将生成以下内容,这意味着只允许在端口 `80 上使用 HTTP 流量:

1[secondary_label Output]
2Status: active
3
4To Action From
5--                         ------      ----
6OpenSSH ALLOW Anywhere                  
7Apache ALLOW Anywhere                  
8OpenSSH (v6)               ALLOW Anywhere (v6)             
9Apache (v6)                ALLOW Anywhere (v6)

要进一步允许 HTTPS 流量,请允许Apache 完整配置文件:

1sudo ufw allow 'Apache Full'

然后,删除冗余的Apache配置文件许可:

1sudo ufw delete allow 'Apache'

再次检查状态:

1sudo ufw status

你应该得到以下的输出:

1[secondary_label Output]
2Status: active
3
4To Action From
5--                         ------      ----
6OpenSSH ALLOW Anywhere                  
7Apache Full ALLOW Anywhere                  
8OpenSSH (v6)               ALLOW Anywhere (v6)             
9Apache Full (v6)           ALLOW Anywhere (v6)

现在你已经准备好运行Certbot并获得您的证书。

第4步:获取SSL证书

Certbot 提供了通过插件获取 SSL 证书的多种方式. Apache 插件将负责重新配置 Apache 并在必要时重新加载配置。

1sudo certbot --apache

此命令将生成一系列问题提示,以配置您的 SSL 证书. 首先,您将被要求提供有效的电子邮件地址,这是用于更新通知和安全通知的目的:

1[secondary_label Output]
2Saving debug log to /var/log/letsencrypt/letsencrypt.log
3Plugins selected: Authenticator apache, Installer apache
4Enter email address (used for urgent renewal and security notices) (Enter 'c' to
5cancel): you@your_domain

在您提供有效的电子邮件地址后,请按ENTER,然后转到下一步,您将被要求确认您是否同意 Let’s Encrypt 服务条款。

1[secondary_label Output]
2Please read the Terms of Service at
3https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
4agree in order to register with the ACME server at
5https://acme-v02.api.letsencrypt.org/directory
6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7(A)gree/(C)ancel: A

接下来,您将被问及是否想与电子边界基金会分享您的电子邮件以获得新闻和其他信息. 如果您不希望订阅,请按N,否则按Y,然后按ENTER,以继续下一步:

1[secondary_label Output]
2Would you be willing to share your email address with the Electronic Frontier
3Foundation, a founding partner of the Let's Encrypt project and the non-profit
4organization that develops Certbot? We'd like to send you email about our work
5encrypting the web, EFF news, campaigns, and ways to support digital freedom.
6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7(Y)es/(N)o: N

提示将告知Certbot您想要激活HTTPS的域名。域名列表将自动从您的Apache虚拟主机配置中取出,这就是为什么确认您在虚拟主机中配置了正确的ServerNameServerAlias设置很重要。如果您想激活所有列出的域名(推荐)的HTTPS,请将提示留在空格中,然后按ENTER来继续。否则,请通过列出每个相应的号码,分别为字符串和/或空间,选择您想要激活HTTPS的域名,然后按ENTER:

1[secondary_label Output]
2Which names would you like to activate HTTPS for?
3- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41: your_domain
52: your_domain
6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7Select the appropriate numbers separated by commas and/or spaces, or leave input
8blank to select all options shown (Enter 'c' to cancel):

您将收到以下输出:

 1[secondary_label Output]
 2Obtaining a new certificate
 3Performing the following challenges:
 4http-01 challenge for your_domain
 5http-01 challenge for your_domain
 6Enabled Apache rewrite module
 7Waiting for verification...
 8Cleaning up challenges
 9Created an SSL vhost at /etc/apache2/sites-available/your_domain-le-ssl.conf
10Enabled Apache socache_shmcb module
11Enabled Apache ssl module
12Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf
13Enabling available site: /etc/apache2/sites-available/your_domain-le-ssl.conf
14Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf

接下来,您将被要求选择是否希望 HTTP 流量导向 HTTPS. 这意味着当有人通过未加密渠道(HTTP)访问您的网站时,他们将自动重定向到您的网站的 HTTPS 地址。

1[secondary_label Output]
2Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
3-------------------------------------------------------------------------------
41: No redirect - Make no further changes to the webserver configuration.
52: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
6new sites, or if you're confident your site works on HTTPS. You can undo this
7change by editing your web server's configuration.
8-------------------------------------------------------------------------------
9Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入回复后,Certbot的配置将完成,您将收到有关新证书的最后评论,如何找到生成的文件,以及如何使用外部工具来测试您的配置来分析证书的真实性:

 1[secondary_label Output]
 2Congratulations! You have successfully enabled https://your_domain
 3and your_domain
 4
 5You should test your configuration at:
 6https://www.ssllabs.com/ssltest/analyze.html?d=your_domain
 7https://www.ssllabs.com/ssltest/analyze.html?d=your_domain
 8- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 9
10IMPORTANT NOTES:
11 - Congratulations! Your certificate and chain have been saved at:
12   /etc/letsencrypt/live/your_domain/fullchain.pem
13   Your key file has been saved at:
14   /etc/letsencrypt/live/your_domain/privkey.pem
15   Your cert will expire on 2022-03-07. To obtain a new or tweaked
16   version of this certificate in the future, simply run certbot again
17   with the "certonly" option. To non-interactively renew *all* of
18   your certificates, run "certbot renew"
19 - If you like Certbot, please consider supporting our work by:
20
21   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
22   Donating to EFF:                    https://eff.org/donate-le

您的证书被下载,安装,并加载到Apache的配置中。尝试使用https://重新加载您的网站,并注意您的浏览器的安全指标。

您可以使用 SSL Labs Server Test来验证您的证书的分级,并从外部服务的角度获取有关证书的详细信息。

在下一步中,您将测试Certbot的自动更新功能,这保证您的证书将在到期日期之前自动更新。

步骤 5 – 验证 Certbot 自动更新

Let’s Encrypt 证书仅有效 90 天,旨在鼓励用户自动更新证书,并确保滥用证书或被盗密钥的有效期更快。

您安装的certbot包通过将更新脚本添加到/etc/cron.d来处理更新,该程序由一个名为certbot.timersystemctl服务管理。

检查此服务的状态,并确保它是活跃和运行:

1sudo systemctl status certbot.timer

您将收到类似于以下的输出:

1[secondary_label Output]
2 certbot.timer - Run certbot twice daily
3   Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: en
4   Active: active (waiting) since Tue 2021-12-07 20:04:42 UTC; 1h 45min ago
5  Trigger: Wed 2021-12-08 11:22:45 UTC; 13h left
6
7Dec 07 20:04:42 encrypt systemd[1]: Started Run certbot twice daily.

通过使用certbot进行干跑来测试更新过程:

1sudo certbot renew --dry-run

如果你没有收到任何错误,你都已经设置了。当需要时,Certbot将更新你的证书并重新加载Apache来收集更改。如果自动更新过程有时失败,Let’s Encrypt将向你指定的电子邮件发送消息,通知你你的证书即将到期。

结论

在本教程中,您安装了 Let’s Encrypt 客户端certbot,为您的域配置和安装了 SSL 证书,并确认 Certbot 的自动证书更新服务在systemctl中是活跃的。

Published At
Categories with 技术
comments powered by Disqus