如何在RHEL 8上使用Let s Encrypt证书保护Apache Web服务器 ?

在一个不断受到安全威胁的网络世界中,确保您的 Web 服务器安全是最重要的事情。保护您的 Web 服务器的方法之一是使用 SSL/TLS 证书在网站上实现 HTTPS 协议。SSL/TLS 证书不仅通过对 Web 服务器和用户浏览器之间交换的信息进行加密来确保您的网站,还可以帮助 Google 排名。

在本指南中,您将学习在 RHEL 8 上如何使用 Let's Encrypt SSL/TLS 来保护 Apache(HTTP)Web 服务器。

必备条件

  • 配置了 sudo 用户的 RHEL 8 服务器实例。
  • 一个完全限定的域名(FQDN)指向您的服务器的公共 IP 地址。在本指南中,我们将使用域名 linuxtechgeek.info。

(1) 安装 Apache

第一步是安装 Apache web 服务器。您可以使用如下所示的 DNF 包管理器来安装它。

$ sudo dnf install -y httpd

安装完成后,启动 Apache web 服务器并使其在引导时启动。

$ sudo systemctl start httpd
$ sudo systemctl enable httpd

要验证 Apache 是否正在运行,请执行以下命令

$ sudo systemctl status httpd

注意: 如果防火墙正在运行,则允许以下 Apache 端口在防火墙中运行

$ sudo firewall-cmd --add-port=80/tcp --permanent
$ sudo firewall-cmd --add-port=443/tcp –permanent
$ sudo firewall-cmd --realod

现在,你可以转到你的浏览器,输入你的域名访问。

(2) 安装 Certbot

Certbot 是一个易于使用的开源客户端,由 EFF (Electronic Frontier Foundation) 维护。它从 Lets Encrypt 获取 TLS 证书并将其部署到 web 服务器。通过这样做,它消除了使用 TLS 证书实现 HTTPS 协议的麻烦和痛苦。

要安装 Certbot 和相关的包,首先要启用 EPEL (Extra Packages for Enterprise Linux)

$ sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

接下来,按如下方法安装 certbot 和 mod ssl 包

$ sudo dnf install certbot python3-certbot-apache mod_ssl

(3) 创建一个 Apache 虚拟主机文件

虚拟主机使得在一个 web 服务器上托管多个域成为可能。

第一步是在 Document 根目录中创建一个目录,所有的网站文件都将进入该目录

$ sudo mkdir -p /var/www/linuxtechgeek.info/html

设置目录归属为 Apache 用户

$ sudo chown -R apache:apache /var/www/linuxtechgeek.info/html

请确保按所示设置目录权限

$ sudo chmod -R 755 /var/www

接着在 /etc/httpd/conf.d 目录中创建虚拟主机文件

$ sudo vi /etc/httpd/conf.d/linuxtechgeek.info.conf

粘贴下面的内容,注意使用自己的域名

<virtualhost *:80>
ServerName linuxtechgeek.info
ServerAlias www.linuxtechgeek.info
DocumentRoot /var/www/linuxtechgeek.info/html
ErrorLog /var/log/httpd/linuxtechgeek.info-error.log
CustomLog /var/log/httpd/linuxtechgeek.info-access.log combined
</virtualhost>

保存并退出 VirtualHost 文件

为了测试虚拟主机是否正在工作,我们将在网站目录中创建一个示例 HTML 文件。

$ sudo vi /var/www/linuxtechgeek.info/html/index.html

粘贴以下示例内容,您也可以根据自己的喜好随意修改它。

<!DOCTYPE html>
<html>
 <body>
 <h1> Welcome to Linuxtechi virtualhost </h1>
 </body>
</html>

保存并退出 HTML 文件。要保存所做的所有更改,请重新启动 Apache web 服务器。

$ sudo systemctl restart httpd

再次访问您的域名,您将看到刚刚配置的定制 HTML 页面,而不是默认的 Apache 欢迎页面。

(4) 使用 Let’s Encrypt 证书保护 Apache

最后一步是获取并部署 Let’s Encrypt 证书,运行以下命令

$ sudo certbot --apache

When the command is executed, certbot will walk you through a series of prompts. You will be prompted for your email address and be required to agree to the Terms and conditions. You will also be asked whether you would like to receive periodic emails about EFF news, and campaigns about digital freedom.

当执行该命令时,certbot 将引导您完成一系列提示。系统会提示您输入您的电子邮件地址,并要求您同意本条款和条件。你还会被问到是否愿意定期收到 EFF 的新闻和活动的电子邮件。

当提示要激活 HTTPS 的名称时,只需按 ENTER 将证书应用到所有提供的域。

Certbot will proceed to fetch the TLS certificate from Let’s Encrypt and implement it on your web server. Certbot will then print the path where the certificate and key have been saved as well as the deployment path of the certificate for your domains.

Certbot 将继续从 Let s Encrypt 获取 TLS 证书,并部署在您的 web 服务器上。然后,Certbot 将打印保存证书和密钥的路径以及域证书的部署路径。

要验证 Let’s encrypt 已成功部署,请刷新浏览器。您会注意到在 URL 栏开始处出现了一个挂锁图标,表明站点已成功加密。

您可以单击挂锁图标了解更多详细信息


此外,您可以在SSL Labs 上进行 SSL 测试,以数字验证您的证书。如果一切顺利,您应该获得一个等级A的成绩。

(5) Let’s Encrypt 证书续订

Let’s Encrypt 证书有效期只有 90 天,在证书到期前几周,您通常会收到 EFF 的通知,通知您证书即将到期,需要更新您的证书。

当需要手动更新证书时,使用该命令

$ sudo certbot renew

当需要模拟证书更新时,使用该命令

$ sudo certbot renew --dry-run

这只是模拟实际的证书更新,不执行任何操作。

要自动更新证书,请打开 crontab 文件

$ crontab -e

指定下面的 cron 作业,它将在每个午夜运行。

0 0 * * * /usr/bin/certbot renew > /dev/null 2>&1

我的开源项目

作者:鸠摩智首席音效师原文地址:https://segmentfault.com/a/1190000043101995

%s 个评论

要回复文章请先登录注册