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

介绍

Let's Encrypt 是一个证书授权机构(CA),可方便获得和安装免费的 TLS/SSL 证书,从而在 Web 服务器上允许加密 HTTPS。

在本教程中,您将使用Certbot在CentOS 8上的ApacheWeb服务器上设置TLS/SSL证书。

前提条件

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

  • 1个CentOS 8服务器,配备一个非root sudo启用的用户和一个基本的 " 防火墙 " ,通过跟踪CentOS 8初始服务器设置指南而设置。 (_) ( )* Apache 安装在服务器上, 为您的域配置了一个虚拟主机 。 您可以按照我们的教程[如何安装 CentOS 8 上的 Apache 网络服务器( https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-centos-8) 来学习如何设置此功能 。 请确定您为您的域设置了 [虚拟主机] (https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-centos-8#step-4-%E2%80%94-setting-up-virtual-hosts-(recommended ) 。 此教程将使用`/etc/httpd/sites-captive/example.com.conf'作为示例。
  • 您应该拥有或控制您想要使用证书的已注册域名 。 如果您还没有注册域名,您可以在 [Namecheap (https://namecheap.com) 上购买一个,在 [Freenom (https://www.freenom.com 上免费获得一个),或者使用您选择的域名注册员.
  • 一个 DNS QQA ' Record ** 将您的域指向您的服务器的公开IP地址. 如果通过 DigitalOcean 托管您的域名,可以参考 [DigitalOcean DNS 文档] (https://www.digitalocean.com/docs/networking/dns/),以了解如何用 DigitalOcean 平台添加这些记录的细节. DNS A' 记录是必需的,因为让我们加密如何验证您拥有它发给证书的域。 例如,如果想要获得example.com'的证书,该域必须解决到您的服务器上,以便验证进程发挥作用。 我们的设置将使用example.com'和www.example.com'作为域名,这两个域名都需要有效的DNS记录。 (_) (英语)

当您完成所有这些前提条件后,继续安装Certbot客户端软件。

步骤 1 – 安装 Certbot Let’s Encrypt 客户端

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

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

若要添加 CentOS 8 EPEL 存储库,请执行以下命令:

1sudo dnf install epel-release

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

1sudo dnf install certbot python3-certbot-apache mod_ssl

有了这些服务,您现在已经准备好运行Certbot并获取证书。

步骤二:获得证书

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

使用certbotLet’s Encrypt客户端来生成Apache的SSL证书,将自动化进程中的许多步骤,客户端将自动获得并安装一个新的SSL证书,该证书适用于您提供的域作为参数。

要执行交互式安装并获得仅覆盖单个域的证书,请运行certbot命令:

1sudo certbot --apache -d example.com

这将运行certbot--apache插件,并指定该域以-d旗配置证书。

如果您想安装一个适用于多个域或子域的单一证书,您可以将其作为附加参数传递给命令,将每个新域或子域标记为 -d 旗帜。参数列表中的第一个域名将是 Let’s Encrypt 用来创建证书的 base 域名。

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

此示例中的基本域名为example.com

certbot实用程序还可以要求您根据现有的Apache配置选择一个域名. 要使用此功能,请拨打certbot而没有指定任何域名:

1sudo certbot --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 2020-09-24. 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

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

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

第3步:测试证书和SSL配置

在此时,您可以通过使用云安全公司Qualys的SSL服务器测试(https://www.ssllabs.com/ssltest/)来确保Certbot正确地创建您的SSL证书。

在您喜爱的网页浏览器中打开以下链接,以您的域名取代example.com:

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

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

SSL Server Test

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

当测试结束时,页面将显示一个字母级别,评估您的服务器配置的安全性和质量。

SSL Report - A

有关 SSL Labs 如何确定这些评级的更多信息,请参阅 SSL Labs 评级帖子,详细介绍了 2018 年 1 月对评级计划的更新。

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

安装和验证您的 SSL 证书后,下一步是为您的证书设置自动更新,以保持证书的有效性。

步骤 4 – 设置自动更新

Let’s Encrypt 证书有效期为 90 天,但建议您在发生撤销或其他问题时每天检查两次更新。

首先,让我们看看您将使用的命令来更新证书。certbot``Let’s Encrypt客户端有一个renew命令,该命令会自动检查当前安装的证书,并尝试更新它们,如果它们在到期日期不到30天。

1sudo certbot renew --dry-run

结果应该是这样的:

 1[secondary_label Output]
 2Saving debug log to /var/log/letsencrypt/letsencrypt.log
 3
 4- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 5Processing /etc/letsencrypt/renewal/example.com.conf
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7Cert not due for renewal, but simulating renewal for dry run
 8Plugins selected: Authenticator apache, Installer apache
 9Starting new HTTPS connection (1): acme-staging-v02.api.letsencrypt.org
10Renewing an existing certificate
11Performing the following challenges:
12http-01 challenge for example.com
13http-01 challenge for www.example.com
14Waiting for verification...
15Cleaning up challenges
16Resetting dropped connection: acme-staging-v02.api.letsencrypt.org
17
18- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
19new certificate deployed with reload of apache server; fullchain is
20/etc/letsencrypt/live/example.com/fullchain.pem
21- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
23- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
24** DRY RUN: simulating 'certbot renew' close to cert expiry
25**          (The test certificates below have not been saved.)
26
27Congratulations, all renewals succeeded. The following certs have been renewed:
28  /etc/letsencrypt/live/example.com/fullchain.pem (success)
29...

请注意,如果您创建了包含多个域的组合证书,则输出中只会显示基本域名,但更新将适用于该证书中包含的所有域名。

确保您的证书不会过时的一种实用方法是创建一个 cron job,它会定期为您执行自动更新命令。

官方 Certbot 文档建议每天运行cron两次,这将确保如果 Let's Encrypt 启动证书撤销,Certbot 才会更新您的证书超过半天。

1echo "0 0,12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null

这将创建一个新的 cron 任务,每天在中午和午夜执行(0 0.12 * * *)。它将运行短的python3脚本,该脚本在零到六十分钟之间随机停留。

在 python 脚本退出后,实际的certbot renew命令将运行。

有关如何创建和日程安排 cron 任务的更多信息,请参阅我们的 How To Use Cron To Automate Tasks on CentOS 8指南。

结论

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

Published At
Categories with 技术
comments powered by Disqus