如何在CentOS7上使用Let'sEncrypt保护Apache

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

介绍

Let's Encrypt 是一个证书颁发机构 (CA),它为 传输层安全 (TLS) 加密 提供免费证书,从而在 Web 服务器上启用加密的 HTTPS。 它通过提供自动化大多数步骤的软件客户端——Certbot,简化了证书的创建、验证、签名、安装和续订过程。

在本教程中,您将使用 Certbot 在运行 Apache 作为 Web 服务器的 CentOS 7 服务器上设置来自 Let's Encrypt 的 TLS/SSL 证书。 此外,您将使用 cron 作业自动执行证书更新过程,您可以通过阅读 如何使用 Cron 在 VPS 上自动执行任务来了解更多信息。

先决条件

为了完成本指南,您需要:

  • 一台 CentOS 7 服务器按照 CentOS 7 初始服务器设置指南 设置一个具有 sudo 权限的非 root 用户。
  • 按照 Additional Recommended Steps for New CentOS 7 Servers 指南配置的基本防火墙。
  • Apache 安装在配置了虚拟主机的 CentOS 7 服务器上。 您可以按照我们的教程 如何在 CentOS 7 上安装 Apache Web 服务器来学习如何设置它。 确保您的域具有 虚拟主机文件 。 本教程将以 /etc/httpd/sites-available/example.com.conf 为例。
  • 您应该拥有或控制您希望使用证书的注册域名。 如果您还没有注册域名,您可以在 Namecheap 上购买一个,在 Freenom 上免费获得一个,或者使用您选择的域名注册商。
  • DNS A 记录 将您的域指向您服务器的公共 IP 地址。 您可以关注 这个 DigitalOcean DNS 介绍,详细了解如何在 DigitalOcean 平台上添加它们。 DNS A 记录是必需的,因为 Let's Encrypt 如何验证您是否拥有它为其颁发证书的域。 例如,如果您想获得 example.com 的证书,则该域必须解析到您的服务器,验证过程才能正常工作。 我们的设置将使用 example.comwww.example.com 作为域名,这两个域名都需要有效的 DNS 记录。

完成所有这些先决条件后,继续安装 Let's Encrypt 客户端软件。

第 1 步 — 安装 Certbot Let's Encrypt 客户端

要使用 Let's Encrypt 获取 SSL 证书,您首先需要安装 Certbot 和 mod_ssl,这是一个支持 SSL v3 加密的 Apache 模块。

默认情况下,certbot 包无法通过包管理器获得。 您需要启用 EPEL 存储库来安装 Certbot。

要添加 CentOS 7 EPEL 存储库,请运行以下命令:

sudo yum install epel-release

现在您可以访问存储库,安装所有必需的包:

sudo yum install certbot python2-certbot-apache mod_ssl

在安装过程中,系统会询问您有关导入 GPG 密钥的信息。 此密钥将验证您正在安装的软件包的真实性。 要完成安装,请输入 y 接受 GPG 密钥,并在提示时按 ENTER

安装这些服务后,您现在可以运行 Certbot 并获取您的证书。

第 2 步 — 获得证书

现在 Certbot 已安装,您可以使用它为您的域请求 SSL 证书。

使用 certbot Let's Encrypt 客户端为 Apache 生成 SSL 证书可自动执行该过程中的许多步骤。 客户端将自动获取并安装对您作为参数提供的域有效的新 SSL 证书。

要执行交互式安装并获取仅涵盖单个域的证书,请使用以下命令运行 certbot 命令:

sudo certbot --apache -d example.com

这将使用 --apache 插件运行 certbot 并使用 -d 标志指定要为其配置证书的域。

如果要安装对多个域或子域有效的单个证书,可以将它们作为附加参数传递给命令,用 -d 标志标记每个新域或子域。 参数列表中的第一个域名将是 Let's Encrypt 用来创建证书的 base 域。 出于这个原因,将基本域名作为列表中的第一个传递,然后是任何其他子域或别名:

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

此示例中的基域是 example.com

certbot 实用程序还可以在证书申请过程中提示您输入域信息。 要使用此功能,请在没有任何域的情况下调用 certbot

sudo certbot --apache

该程序将为您提供自定义证书选项的分步指南。 它将要求您提供丢失密钥恢复和通知的电子邮件地址,然后提示您同意服务条款。 如果您没有在命令行上指定域,系统也会提示您输入。 如果您的虚拟主机文件没有使用 ServerName 指令明确指定它们服务的域,您将被要求选择虚拟主机文件。 在大多数情况下,默认的 ssl.conf 文件将起作用。

您还可以在启用 httphttps 访问或强制所有请求重定向到 https 之间进行选择。 为了更好的安全性,如果您没有任何特殊需要允许未加密的连接,建议选择选项2: Redirect。 选择您的选择,然后点击 ENTER

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

安装成功完成后,您将看到类似以下的消息:

OutputIMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2019-08-14. 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

生成的证书文件将在 /etc/letsencrypt/live 目录中以您的基本域命名的子目录中可用。

现在您的证书已下载、安装和加载,您可以检查 SSL 证书状态以确保一切正常。

第 3 步 — 检查您的证书状态

此时,您可以使用云安全公司 QualysSSL Server Test 确保 Certbot 正确创建了您的 SSL 证书。

在您首选的 Web 浏览器中打开以下链接,将 example.com 替换为您的 base 域:

https://www.ssllabs.com/ssltest/analyze.html?d=example.com

您将登陆一个页面,该页面会立即开始测试与您的服务器的 SSL 连接:

测试开始运行后,可能需要几分钟才能完成。 测试状态将在您的浏览器中更新。

测试完成后,页面将显示一个字母等级,用于评估服务器配置的安全性和质量。 在撰写本文时,默认设置将给出 A 评级:

有关 SSL Labs 如何确定这些等级的更多信息,请查看 SSL Labs 分级帖子 ,其中详细介绍了 2018 年 1 月对分级方案所做的更新。

尝试使用 https:// 重新加载您的网站,并注意浏览器的安全指示器。 它现在将指示该站点已正确保护,通常带有绿色锁定图标。

设置并验证您的 SSL 证书后,下一步是为您的证书设置自动续订以保持您的证书有效。

第 4 步 — 设置自动续订

Let's Encrypt 证书的有效期为 90 天,但建议您每 60 天更新一次证书,以允许存在误差。 因此,最佳实践是自动化此过程以定期检查和更新证书。

首先,让我们检查一下您将用于更新证书的命令。 certbot Let's Encrypt 客户端有一个 renew 命令,该命令会自动检查当前安装的证书,并在距离到期日期不到 30 天时尝试更新它们。 通过使用 --dry-run 选项,您可以运行此任务的模拟来测试 renew 的工作原理:

sudo certbot renew --dry-run

输出应与此类似:

OutputSaving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for example.com
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges
Resetting dropped connection: acme-staging-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of apache server; fullchain is
/etc/letsencrypt/live/example.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/example.com/fullchain.pem (success)
...

请注意,如果您创建了包含多个域的捆绑证书,则输出中将仅显示基本域名,但续订将对该证书中包含的所有域有效。

确保您的证书不会过时的一种实用方法是创建一个 cron 作业,它将定期为您执行自动更新命令。 由于续订首先检查到期日期,并且仅在证书距离到期不到 30 天时才执行续订,因此创建每周甚至每天运行的 cron 作业是安全的。

官方 Certbot 文档 建议每天运行两次 cron。 这将确保在 Let's Encrypt 发起证书吊销的情况下,Certbot 更新您的证书不会超过半天。

编辑 crontab 以创建一个每天运行两次更新的新作业。 要为 root 用户编辑 crontab,请运行:

sudo crontab -e

您的文本编辑器将打开默认的 crontab,此时它是一个空文本文件。 本教程将使用 vi 文本编辑器。 要了解有关此文本编辑器及其后续版本 vim 的更多信息,请查看我们的 在云服务器上安装和使用 Vim 文本编辑器 教程。

i 进入插入模式并添加以下行:

crontab0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew

完成后,按 ESC 退出插入模式,然后按 :wqENTER 保存并退出文件。 这将创建一个新的 cron 作业,该作业将在每天中午和午夜执行。 为您的 cron 作业添加随机元素将确保每小时作业不会全部发生在同一分钟,从而导致服务器峰值; python -c 'import random; import time; time.sleep(random.random() * 3600)' 将在一小时内随机选择一分钟用于您的续订任务。

有关如何创建和安排 cron 作业的更多信息,您可以在 VPS 指南中查看我们的 How to Use Cron to Automate Tasks。 有关续订的更多详细信息,请参阅 Certbot 文档

结论

在本指南中,您安装了 Let's Encrypt Certbot 客户端,为您的域下载了 SSL 证书,并设置了自动证书续订。 如果您对使用 Certbot 有任何疑问,可以查看官方 Certbot 文档。 我们还建议您不时查看官方 Let's Encrypt 博客 以获取重要更新。