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

介绍

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

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

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

前提条件

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

您可以通过遵循我们的 初始服务器设置为 Ubuntu 22.04 教程

  • 一个完全注册的域名。本教程将使用 your_domain 作为整个例子。您可以在 Namecheap购买域名,在 Freenom获得一个免费的教程 或使用您选择的域名注册器
  • 两者均为您的服务器设置了下列 DNS 记录。您可以遵循 DigitalOcean DNS(Namecheap 这个介绍,以了解如何将其添加到公共位置 ) - 一个记录与 youromain

步骤1:安装Certbot

要使用 Let's Encrypt 获得 SSL 证书,您需要在您的服务器上安装 Certbot 软件。

首先,更新本地包索引:

1sudo apt update

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

如果您遵循了Apache安装教程中的虚拟主机设置步骤(https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-ubuntu-22-04#step-5-setting-up-virtual-hosts-recommended),则您应该为您的域设置一个虚拟主机区块在/etc/apache2/sites-available/your_domain.conf上,并且已经设置了ServerNameServerAlias指令。

要确认此设置,请使用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配置文件:

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
3Enter email address (used for urgent renewal and security notices)
4 (Enter 'c' to cancel): you@your_domain

提供有效的电子邮件地址后,请按ENTER,然后请您确认您是否同意 Let’s Encrypt 服务条款。

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(Y)es/(N)o: Y

接下来,您将被问及是否想与电子边界基金会分享您的电子邮件以获得新闻和其他信息. 如果您不希望订阅他们的内容,请写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来继续。否则,通过列出每个相应的号码,分隔为和/或间隔,选择您想要激活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):

此步骤结束后,Certbot的配置完成,您将收到有关新证书以及如何找到生成的文件的最后评论:

 1[secondary_label Output]
 2Successfully received certificate.
 3Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
 4Key is saved at:         /etc/letsencrypt/live/your_domain/privkey.pem
 5This certificate expires on 2022-07-10.
 6These files will be updated when the certificate renews.
 7Certbot has set up a scheduled task to automatically renew this certificate in the background.
 8
 9Deploying certificate
10Successfully deployed certificate for your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
11Successfully deployed certificate for www.your_domain.com to /etc/apache2/sites-available/your_domain-le-ssl.conf
12Congratulations! You have successfully enabled HTTPS on https:/your_domain and https://www.your_domain.com
13
14- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
15If you like Certbot, please consider supporting our work by:
16 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
17 * Donating to EFF:                    https://eff.org/donate-le
18- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

您的证书现在已安装并加载到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:>
4     Active: active (waiting) since Mon 2022-04-11 20:52:46 UTC; 4min 3s ago
5    Trigger: Tue 2022-04-12 00:56:55 UTC; 4h 0min left
6   Triggers:  certbot.service
7
8Apr 11 20:52:46 jammy-encrypt systemd[1]: Started Run certbot twice daily.

要测试更新过程,您可以使用certbot进行干跑:

1sudo certbot renew --dry-run
 1[secondary_label Output]
 2Saving debug log to /var/log/letsencrypt/letsencrypt.log
 3
 4- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 5Processing /etc/letsencrypt/renewal/your_domain.conf
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7Account registered.
 8Simulating renewal of an existing certificate for your_domain and www.your_domain.com
 9
10- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11Congratulations, all simulated renewals succeeded:
12  /etc/letsencrypt/live/your_domain/fullchain.pem (success)
13- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

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

结论

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

Published At
Categories with 技术
comments powered by Disqus