跳至内容

mod_ssl 在 Apache Web 服务器环境中的 Rocky Linux 上

Apache Web 服务器已经存在很多年了。mod_ssl 为 Web 服务器提供更高的安全性,几乎可以在任何版本的 Linux 上安装。

此过程将帮助您在 Apache Web 服务器环境中使用 Rocky Linux 和 mod_ssl 运行。

先决条件

  • 工作站或服务器,最好已经安装了 Rocky Linux。
  • 能够以rootsudo身份运行命令以提升权限。

安装 Rocky Linux Minimal

安装 Rocky Linux 时,我们使用以下软件包集

  • Minimal
  • Standard

运行更新

首先,运行系统更新命令以让服务器重建存储库缓存以识别可用的软件包。

dnf update

启用存储库

使用传统的 Rocky Linux 服务器安装,所有必需的存储库都将到位。

检查可用的存储库

为了确保,请使用以下命令检查您的存储库列表

dnf repolist

您将获得以下内容

appstream                                                        Rocky Linux 8 - AppStream
baseos                                                           Rocky Linux 8 - BaseOS
extras                                                           Rocky Linux 8 - Extras
powertools                                                       Rocky Linux 8 - PowerTools

安装软件包

要安装mod_ssl,请运行

dnf install mod_ssl

要启用mod_ssl模块,请运行

apachectl restart httpd apachectl -M | grep ssl

您将看到

ssl_module (shared)

打开 TCP 端口 443

要允许传入 HTTPS 流量,请运行

firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload

确保您的目标是让网站对全世界开放,然后再添加此规则!如果不是,请更改区域或配置防火墙以进行修正。

此时,您应该能够通过 HTTPS 访问 Apache Web 服务器。输入https://your-server-iphttps://your-server-hostname以确认mod_ssl配置。

生成 SSL/TLS 证书

要为主机 rocky8 生成自签名证书,有效期为 365 天,请运行

openssl req -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/httpd.key -x509 -days 365 -out /etc/pki/tls/certs/httpd.crt

您将看到以下输出

Generating a RSA private key
................+++++
..........+++++
writing new private key to '/etc/pki/tls/private/httpd.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:AU
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:LinuxConfig.org
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:rocky8
Email Address []:

此命令完成后,将存在以下两个 SSL/TLS 文件

ls -l /etc/pki/tls/private/httpd.key /etc/pki/tls/certs/httpd.crt

-rw-r--r--. 1 root root 1269 Jan 29 16:05 /etc/pki/tls/certs/httpd.crt
-rw-------. 1 root root 1704 Jan 29 16:05 /etc/pki/tls/private/httpd.key

使用 SSL/TLS 证书配置 Apache Web 服务器

要将新创建的 SSL/TLS 证书包含到 Apache Web 服务器配置中,请通过运行以下命令打开ssl.conf文件

nano /etc/httpd/conf.d/ssl.conf

更改以下行

FROM

SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

TO

SSLCertificateFile /etc/pki/tls/certs/httpd.crt
SSLCertificateKeyFile /etc/pki/tls/private/httpd.key

通过运行以下命令重新加载 Apache Web 服务器

systemctl reload httpd

测试mod_ssl配置

在 Web 浏览器中输入以下内容

https://your-server-iphttps://your-server-hostname

将所有 HTTP 流量重定向到 HTTPS

通过运行以下命令创建一个新文件

nano /etc/httpd/conf.d/redirect_http.conf

插入以下内容并保存文件,将“your-server-hostname”替换为您的主机名。

<VirtualHost _default_:80>

        Servername rocky8
        Redirect permanent / https://your-server-hostname/

</VirtualHost>

通过运行以下命令应用更改

systemctl reload httpd

Apache Web 服务器将从http://your-server-hostnamehttps://your-server-hostname URL 的任何传入流量重定向。

最后一步

您已经了解如何安装和配置mod_ssl,以及如何创建新的 SSL/TLS 证书以在 HTTPS 服务下运行 Web 服务器。

结论

本教程介绍了mod_ssl的基本安装和使用。

作者:Garthus

贡献者:Steven Spencer、David Hensley、Ganna Zhyrnova