如何在 CentOS 7 上使用 Let's Encrypt 保护 Apache
介绍
证书机器人。
在本教程中,您将使用 Certbot 在运行 Apache 作为 Web 服务器的 CentOS 7 服务器上设置来自 Let's Encrypt 的 TLS/SSL 证书。此外,您将使用 cron 作业自动执行证书更新过程,您可以通过阅读如何使用 Cron 在 VPS 上自动执行任务来了解更多信息。
先决条件
为了完成本指南,您需要:
- 按照 CentOS 7 初始服务器设置指南使用具有
sudo
权限的非根用户设置一台 CentOS 7 服务器。 - 按照新 CentOS 7 服务器指南的其他推荐步骤配置的基本防火墙。
- 在配置了虚拟主机的 CentOS 7 服务器上安装了 Apache。您可以按照我们针对您的域的教程虚拟主机文件来了解如何设置它。本教程将以
/etc/httpd/sites-available/example.com.conf
为例。 - 您应该拥有或控制您希望使用证书的注册域名。如果您还没有注册域名,您可以在 Freenom 上购买一个,或使用您选择的域名注册商。
- 将您的域指向您服务器的公共 IP 地址的 DNS A 记录。您可以按照 DigitalOcean DNS 的介绍了解有关如何使用 DigitalOcean 平台添加它们的详细信息。 DNS A 记录是必需的,因为 Let's Encrypt 如何验证您拥有它为其颁发证书的域。例如,如果您想为
example.com
获取证书,该域必须解析为您的服务器,验证过程才能正常进行。我们的设置将使用example.com
和www.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
并在出现提示时按 ENTER
接受 GPG 密钥。
安装这些服务后,您现在就可以运行 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 用于创建证书的基域。因此,将基本域名作为列表中的第一个传递,然后是任何其他子域或别名:
- sudo certbot --apache -d example.com -d www.example.com
此示例中的基本域是 example.com
。
certbot
实用程序还可以在证书申请过程中提示您输入域信息。要使用此功能,请调用不带任何域的 certbot
:
- sudo certbot --apache
该程序将向您提供自定义证书选项的分步指南。它会要求您提供用于丢失密钥恢复和通知的电子邮件地址,然后提示您同意服务条款。如果您没有在命令行中指定您的域,系统也会提示您指定。如果您的虚拟主机文件未使用 ServerName
指令明确指定它们服务的域,您将被要求选择虚拟主机文件。在大多数情况下,默认的 ssl.conf
文件都可以使用。
您还可以选择启用 http
和 https
访问或强制所有请求重定向到 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 步 — 检查您的证书状态
此时,您可以确保 Certbot 使用 Qualys 正确创建了您的 SSL 证书。
在您首选的网络浏览器中打开以下链接,将 example.com
替换为您的基本域:
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
退出插入模式,然后按 :wq
和 ENTER
保存并退出文件。这将创建一个新的 cron 作业,该作业将在每天中午和午夜执行。为您的 cron 作业添加随机元素将确保每小时作业不会全部在同一分钟发生,从而导致服务器峰值; <代码>python -c 随机导入;导入时间; time.sleep(random.random() * 3600) 将在一小时内随机选择一分钟进行续订任务。
有关如何创建和安排 cron 作业的更多信息,您可以查看我们的 Certbot 文档。
结论
在本指南中,您安装了 Let's Encrypt Certbot 客户端,为您的域下载了 SSL 证书,并设置了自动证书续订。如果您对使用 Certbot 有任何疑问,可以不时查看 Let's Encrypt 官方博客的重要更新。