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

介绍

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

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

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

前提条件

要遵循本教程,您将需要:

  • 一个Ubuntu 20.04服务器设置通过遵循此 初始服务器设置为Ubuntu 20.04]教程,包括一个 sudo非根用户和一个防火墙
  • 一个完全注册的域名。本教程将使用 your_domain作为一个例子。您可以在 Namecheap上购买域名,在 Freenom上免费获取一个,或者使用您选择的域名注册
  • 两者均为您的服务器设置了下列 DNS 记录。您可以遵循 DigitalOcean DNS 的介绍 有关如何添加它们的详细信息
  • 一个带有

步骤1:安装Certbot

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

我们需要两个包:certbot和python3-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安装教程中的虚拟主机设置步骤(https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-ubuntu-20-04#step-5-%E2%80%94-setting-up-virtual-hosts-(recommended),则您应该为您的域设置一个虚拟主机区块在 /etc/apache2/sites-available/your_domain.confServerName 以及 ServerAlias 指令已经适当设置。

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

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

查找现有的ServerNameServerAlias行,它们应该是这样的:

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

如果你已经有ServerNameServerAlias这样的设置,你可以退出你的文本编辑器并继续到下一步. 如果你使用nano,你可以退出通过键入CTRL+X,然后YENTER来确认。

如果您当前的虚拟主机配置不匹配示例,请相应地更新。完成后,保存文件并离开编辑器。

1sudo apache2ctl configtest

如果您收到错误,请重新打开虚拟主机文件并检查任何输入或缺少的字符。一旦配置文件的语法正确,请重新加载Apache,以便更改生效:

1sudo systemctl reload apache2

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

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

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

如果您已启用 UFW 防火墙,根据前提指南的建议,您需要调整设置以允许 HTTPS 流量。 安装后,Apache 会注册一些不同的 UFW 应用程序配置文件。

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

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 Full配置文件并删除余额的Apache配置文件:

1sudo ufw allow 'Apache Full'
2sudo 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

提供有效的电子邮件地址后,点击输入,然后转到下一步,您将被要求确认您是否同意我们加密服务条款,您可以通过点击A,然后输入来确认:

1- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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

1- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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,以便继续。

1Which names would you like to activate HTTPS for?
2- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31: your_domain
42: www.your_domain
5- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6Select the appropriate numbers separated by commas and/or spaces, or leave input
7blank to select all options shown (Enter 'c' to cancel):

你会看到这样的输出:

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

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

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

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

 1- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 2Congratulations! You have successfully enabled https://your_domain and
 3https://www.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=www.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 2020-07-27. 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 - Your account credentials have been saved in your Certbot
20   configuration directory at /etc/letsencrypt. You should make a
21   secure backup of this folder now. This configuration directory will
22   also contain certificates and private keys obtained by Certbot so
23   making regular backups of this folder is ideal.
24 - If you like Certbot, please consider supporting our work by:
25
26   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
27   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: enabled)
4     Active: active (waiting) since Tue 2020-04-28 17:57:48 UTC; 17h ago
5    Trigger: Wed 2020-04-29 23:50:31 UTC; 12h left
6   Triggers:  certbot.service
7
8Apr 28 17:57:48 fine-turtle 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