本文假设您已经在 RHEL 8/CentOS 8 服务器上执行了 Apache 网络服务器的基本安装和配置。
- 安装
mod_ssl
模块。第一步是使用dnf
命令安装mod_ssl
模块:# dnf install mod_ssl
- 启用
mod_ssl
模块。如果您刚刚安装mod_ssl
,该模块可能尚未启用。要测试是否mod_ssl
启用执行:# apachectl -M | grep ssl # httpd -M
如果您没有看到上述命令的输出,则您
mod_ssl
未启用。要启用该mod_ssl
模块,请重新启动您的Apachehttpd
网络服务器:# systemctl restart httpd # apachectl -M | grep ssl # httpd -M ssl_module (shared)
- 打开 TCP 端口 443以允许使用
https
协议的传入流量:# firewall-cmd --zone=public --permanent --add-service=https success # firewall-cmd --reload success
注意
此时您应该能够通过 HTTPS 协议访问您的 Apache 网络服务器。浏览您的浏览器https://your-server-ip
以https://your-server-hostname
确认mod_ssl
配置。 - 生成 SSL 证书。如果您的服务器还没有正确的 SSL 证书,请使用以下命令生成新的自签名证书。
例如,让我们为主机生成一个新的自签名证书,
rhel8
有效期为 9999天:openssl req -newkey rsa:4096 -nodes -keyout /etc/pki/tls/private/localhost.key -x509 -days 9999 -out /etc/pki/tls/certs/localhost.crt ..+......+....+..+.........+......+....+.........+.....+...+..........+..+.......+...+.....+.+...........+.+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+.+........+.+.....+....+..+...............+.+........+....+...+..+.......+.....+....+.........+........+.........+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.....+.........+...+.........+.+............+......+..+...+..........+..+....+...+............+........+.+..+...+...............+.......+........+................+...........+...+....+...............+...........+................+...+.....+................+..+....+...+.....+...+.......+..+....+.........+......+...........+......+...+.........+.+...............+......+.........+.........+..+.+.....+...+....+..+.+...............+.........+..................+...+.....+.......+..+...+......+.........+..................+...+..........+..........................+......+..........+........+....+..+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ..+............+................+..+...+.....................+..........+........+.......+......+..+.+...........+.........+....+..+.+..+...+..........+..............+...+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*..+.....+...+...+.+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*...+................+.....+.+.................+.......+...+........+...+.........+.........................+.....+...+....+...+..+....+.....+...............+....+...........+.+...+........+...+......+....+...............+..............+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ----- 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]: State or Province Name (full name) []: Locality Name (eg, city) [Default City]: Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []: Email Address []:
成功执行上述命令后,将创建以下两个 SSL 文件:
# ls -l /etc/pki/tls/private/localhost.key /etc/pki/tls/certs/localhost.crt -rw-r--r--. 1 root root 1931 8月 10 14:27 /etc/pki/tls/certs/localhost.crt -rw-------. 1 root root 3272 8月 10 14:27 /etc/pki/tls/private/localhost.key
- 使用新的 SSL 证书配置 Apache Web 服务器。要将新创建的 SSL 证书包含到 Apache Web 服务器配置中,请
/etc/httpd/conf.d/ssl.conf
使用管理权限打开文件并更改以下行(如果文件名未使用localhost):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
httpd
网络服务器:# systemctl reload httpd
mod_ssl
通过将 Web 浏览器导航到https://your-server-ip
URLhttps://your-server-hostname
来测试您的配置。- 作为可选步骤,将所有 HTTP 流量重定向到 HTTPS.T,创建一个
/etc/httpd/conf.d/redirect_http.conf
包含以下内容的新文件:<VirtualHost _default_:80> Servername rhel8 Redirect permanent / https://rhel8/ </VirtualHost>
复制要应用更改重新加载
httpd
守护程序:# systemctl reload httpd
上述配置会将任何传入流量重定向
http://rhel8
到https://rhel8
URL。有关 RHEL Linux 服务器上的 TLS/SSL 配置的更多信息,请访问我们的如何在 Red Hat 上使用 Apache httpd 设置 SSL/TLS指南。