本文假设您已经在 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模块,请重新启动您的Apache httpd网络服务器:
- # systemctl restart httpd
- # apachectl -M | grep ssl
-
- # httpd -M
- ssl_module (shared)
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配置。
例如,让我们为主机生成一个新的自签名证书,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
/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
https://your-server-ip或https://your-server-hostname来测试您的mod_ssl配置。/etc/httpd/conf.d/redirect_http.conf包含以下内容的新文件:
- <VirtualHost _default_:80>
- Servername rhel8
- Redirect permanent / https://rhel8/
- VirtualHost>
httpd守护程序:
# systemctl reload httpd 上述配置会将任何传入流量重定向http://rhel8到https://rhel8URL。有关 RHEL Linux 服务器上的 TLS/SSL 配置的更多信息,请访问我们的如何在 Red Hat 上使用 Apache httpd 设置 SSL/TLS指南。