在 CentOS 8 上为Apache HTTPD 配置 HTTPS

本文假设您已经在 RHEL 8/CentOS 8 服务器上执行了 Apache 网络服务器的基本安装和配置

  1. 安装mod_ssl模块。第一步是使用dnf命令安装mod_ssl模块:
    # dnf install mod_ssl
    
  2. 启用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)

  3. 打开 TCP 端口 443以允许使用https协议的传入流量:
    # firewall-cmd --zone=public --permanent --add-service=https
    success
    # firewall-cmd --reload
    success

    注意
    此时您应该能够通过 HTTPS 协议访问您的 Apache 网络服务器。浏览您的浏览器https://your-server-iphttps://your-server-hostname确认mod_ssl配置。

  4. 生成 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

  5. 使用新的 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

  6. mod_ssl通过将 Web 浏览器导航到https://your-server-ipURLhttps://your-server-hostname来测试您的配置。
  7. 作为可选步骤,将所有 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://rhel8https://rhel8URL。有关 RHEL Linux 服务器上的 TLS/SSL 配置的更多信息,请访问我们的如何在 Red Hat 上使用 Apache httpd 设置 SSL/TLS指南。

猜你喜欢

转载自blog.csdn.net/allway2/article/details/126265716