如何在 Debian 9 上使用 Let's Encrypt 保护 Apache

介绍

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

在本教程中,您将使用 Certbot 获取 Debian 9 上 Apache 的免费 SSL 证书,并设置您的证书自动更新。

本教程将使用单独的 Apache 虚拟主机文件而不是默认配置文件。 我们建议为每个域创建新的 Apache 虚拟主机文件,因为它有助于避免常见的错误,并将默认文件作为回归配置。

前提条件

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

  • 一个 Debian 9 服务器设置通过遵循此 Debian 9 初始服务器设置教程,包括一个非root用户具有 sudo 特权和防火墙
  • 一个完全注册的域名。本教程将使用 example.com 在整个过程中。您可以在 Namecheap购买域名,在 Freenom免费获取一个域名,或使用您选择的域名注册商
  • 两者均为您的服务器设置了下列 DNS 记录。您可以遵循 DigitalOcean DNS 介绍 有关如何添加域名的详细信息( ) -

步骤1:安装Certbot

使用 Let's Encrypt 获得 SSL 证书的第一步是将 Certbot 软件安装到您的服务器上。

要下载使用apt的软件,您将需要将后端存储库添加到您的sources.list文件中,其中apt寻找包源。

要添加后端存储库,请在您的 /etc/apt/ 目录中打开(或创建) sources.list 文件:

1sudo nano /etc/apt/sources.list

在文件的底部,添加以下行:

1[label /etc/apt/sources.list.d/sources.list]
2. . .
3deb http://ftp.debian.org/debian stretch-backports main

这包括符合 Debian Free Software Guidelines (DFSG)主要包,以及非免费贡献的组件,这些组件本身不符合DFSG,或者包括这个类别中的依赖。

保存并关闭文件,按CTRL+X,Y,然后按ENTER,然后更新您的包列表:

1sudo apt update

然后使用以下命令安装 Certbot. 请注意,t选项告诉apt通过查看您刚刚添加的后端存储库来搜索该包:

1sudo apt install python-certbot-apache -t stretch-backports

Certbot 现在已经准备好使用,但为了为 Apache 配置 SSL,我们需要验证 Apache 是否正确配置。

第2步:设置SSL证书

Certbot 需要能够在您的 Apache 配置中找到正确的虚拟主机,以便自动配置 SSL。

如果您遵循了 Apache 安装教程中的 [虚拟主机设置步骤]( https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-debian-9#step-5-%E2%80%94-setting-up-virtual-hosts-(recommended),则您应该在 /etc/apache2/sites-available/example.com.conf 对您的域名设有一个 VirtualHost 区块,并且已设置适当的 ServerName 指令。

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

1sudo nano /etc/apache2/sites-available/example.com.conf

查找现有的ServerName行. 它应该是这样的,用自己的域名而不是example.com:

1[label /etc/apache2/sites-available/example.com.conf]
2...
3ServerName example.com;
4...

如果尚未完成,请更新ServerName指令以指向您的域名,然后保存文件,退出编辑器,并验证配置编辑的语法:

1sudo apache2ctl configtest

如果没有语法错误,你会看到这个输出:

1[secondary_label Output]
2Syntax OK

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

1sudo systemctl reload apache2

Certbot 现在可以找到正确的 VirtualHost 块并更新它。

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

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

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

您可以通过键入查看当前设置:

1sudo ufw status

如果您遵循《如何在 Debian 9 上安装 Apache 》的第 2 步,此命令的输出将是这样的,显示只有 HTTP 流量允许到 Web 服务器:

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

要进一步允许 HTTPS 流量,请允许WWW 完整配置文件并删除冗余的WWW配置文件允许:

1sudo ufw allow 'WWW Full'
2sudo ufw delete allow 'WWW'

你的状态现在应该是这样的:

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

接下来,让我们运行Certbot并获取我们的证书。

第4步:获取SSL证书

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

1sudo certbot --apache -d example.com -d www.example.com

这将运行certbot--apache插件,使用-d来指定您希望证书有效的名称。

如果这是你第一次运行certbot,你将被要求输入电子邮件地址并同意服务条款. 这样做后,certbot将与 Let’s Encrypt 服务器进行通信,然后运行一个挑战,以验证你控制的域,你正在请求证书。

如果成功,certbot 会问你想如何配置你的 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):

选择你的选择,然后点击ENTER。配置将被更新,Apache将重新加载以获取新的设置。

 1[secondary_label Output]
 2IMPORTANT NOTES:
 3 - Congratulations! Your certificate and chain have been saved at:
 4   /etc/letsencrypt/live/example.com/fullchain.pem
 5   Your key file has been saved at:
 6   /etc/letsencrypt/live/example.com/privkey.pem
 7   Your cert will expire on 2018-12-04. To obtain a new or tweaked
 8   version of this certificate in the future, simply run certbot again
 9   with the "certonly" option. To non-interactively renew *all* of
10   your certificates, run "certbot renew"
11 - Your account credentials have been saved in your Certbot
12   configuration directory at /etc/letsencrypt. You should make a
13   secure backup of this folder now. This configuration directory will
14   also contain certificates and private keys obtained by Certbot so
15   making regular backups of this folder is ideal.
16 - If you like Certbot, please consider supporting our work by:
17
18   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
19   Donating to EFF:                    https://eff.org/donate-le

您的证书被下载、安装和加载。 尝试使用https://重新加载您的网站,并注意您的浏览器的安全指标。 它应该表明该网站是正确保护的,通常用绿色锁定图标。 如果您使用 SSL Labs Server Test测试您的服务器,它将获得 A等级。

让我们通过测试更新过程来结束。

步骤 5 – 验证 Certbot 自动更新

Let's Encrypt 的证书仅有效 90 天,这是为了鼓励用户自动化其证书更新流程. 我们安装的 certbot 包通过将更新脚本添加到 `/etc/cron.d 来为我们提供此功能。

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

1sudo certbot renew --dry-run

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

结论

在本教程中,您安装了 Let’s Encrypt 客户端certbot,下载了您的域的 SSL 证书,配置了 Apache 以使用这些证书,并设置了自动证书更新。

Published At
Categories with 技术
comments powered by Disqus