Skip to content

22. Web 服务配置

本章节指导在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 配置 Web 服务,配合 SecureFX 传输配置文件。内容涵盖 Apache 和 Nginx 的安装、虚拟主机、反向代理、负载均衡及 SSL/TLS 配置,以测试页面和 HTTPS 访问为例,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 ZSLinux 环境中测试,确保实验一致性。

22.1 前提条件

  • RHEL 8.10 已安装(参考第 1 章),主机名设置为 ZSLinux.
  • 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 6 章).
  • 使用 SecureFX 传输文件(SFTP 协议).
  • 具有 root 或 sudo 权限.
  • 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 4 和 7 章).
  • 防火墙和 SELinux 启用(参考第 13 章).
  • 网络连接正常,时间同步配置完成(参考第 3 章).

22.2 Web 服务器配置

22.2.1 配置 Apache

  • 安装 Apache:
    bash
    dnf install -y httpd  # 安装 Apache HTTP 服务器
    systemctl enable --now httpd  # 启用并启动 httpd 服务
  • 配置防火墙和 SELinux:
    bash
    firewall-cmd --permanent --add-service=http --add-service=https  # 允许 HTTP 和 HTTPS 端口(80, 443)
    firewall-cmd --reload  # 应用防火墙规则
    setsebool -P httpd_enable_homedirs 1  # 允许 Apache 访问用户目录
  • 创建测试页面:
    bash
    echo "<h1>Welcome to ZSLinux</h1>" > /var/www/html/index.html  # 创建测试页面
  • 配置虚拟主机:
    bash
    vim /etc/httpd/conf.d/vhost.conf  # 创建虚拟主机配置文件
    # 添加:
    <VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /var/www/vhost
        <Directory /var/www/vhost>
            Require all granted
        </Directory>
    </VirtualHost>
    mkdir -p /var/www/vhost  # 创建虚拟主机目录
    echo "<h1>Virtual Host Page</h1>" > /var/www/vhost/index.html  # 创建虚拟主机页面
    systemctl restart httpd  # 重启 Apache 服务

22.2.2 配置 Nginx

  • 安装 Nginx:
    bash
    dnf install -y nginx  # 安装 Nginx Web 服务器
    systemctl enable --now nginx  # 启用并启动 Nginx 服务
  • 配置防火墙和 SELinux:
    bash
    firewall-cmd --permanent --add-service=http --add-service=https  # 允许 HTTP 和 HTTPS 端口(80, 443)
    firewall-cmd --reload  # 应用防火墙规则
    setsebool -P http_can_network_connect 1  # 允许 Nginx 网络连接
  • 配置反向代理:
    bash
    vim /etc/nginx/conf.d/proxy.conf  # 创建反向代理配置文件
    # 添加:
    server {
        listen 80;
        server_name proxy.example.com;
        location / {
            proxy_pass http://192.168.1.101:8080;
            proxy_set_header Host $host;
        }
    }
    nginx -t  # 验证 Nginx 配置
    systemctl restart nginx  # 重启 Nginx 服务
  • 配置负载均衡:
    bash
    vim /etc/nginx/conf.d/lb.conf  # 创建负载均衡配置文件
    # 添加:
    upstream backend {
        server 192.168.1.101:8080;
        server 192.168.1.102:8080;
    }
    server {
        listen 80;
        server_name lb.example.com;
        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
        }
    }
    nginx -t  # 验证 Nginx 配置
    systemctl restart nginx  # 重启 Nginx 服务

22.3 SSL/TLS 配置

22.3.1 配置自签名证书

  • 创建自签名证书:
    bash
    mkdir /etc/ssl/private  # 创建证书目录
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/server.key -out /etc/ssl/private/server.crt -subj "/CN=ZSLinux"  # 生成自签名证书
  • 配置 Apache SSL:
    bash
    dnf install -y mod_ssl  # 安装 Apache SSL 模块
    vim /etc/httpd/conf.d/ssl.conf  # 编辑 SSL 配置文件
    # 修改:
    SSLCertificateFile /etc/ssl/private/server.crt
    SSLCertificateKeyFile /etc/ssl/private/server.key
    systemctl restart httpd  # 重启 Apache 服务
  • 配置 Nginx SSL:
    bash
    vim /etc/nginx/conf.d/ssl.conf  # 创建 Nginx SSL 配置文件
    # 添加:
    server {
        listen 443 ssl;
        server_name www.example.com;
        ssl_certificate /etc/ssl/private/server.crt;
        ssl_certificate_key /etc/ssl/private/server.key;
        location / {
            root /var/www/vhost;
        }
    }
    nginx -t  # 验证 Nginx 配置
    systemctl restart nginx  # 重启 Nginx 服务

22.3.2 配置 Let’s Encrypt 证书

  • 安装 Certbot:
    bash
    dnf install -y certbot python3-certbot-apache python3-certbot-nginx  # 安装 Certbot 工具
  • 获取 Let’s Encrypt 证书:
    bash
    certbot --apache -d www.example.com  # 为 Apache 获取证书
    certbot --nginx -d www.example.com  # 为 Nginx 获取证书
  • 自动续订证书:
    bash
    crontab -e  # 编辑 crontab
    # 添加:
    0 3 * * * /usr/bin/certbot renew --quiet  # 每天 03:00 检查续订

22.4 验证

  • 验证 Apache:
    bash
    curl http://localhost  # 测试 Apache 默认页面
    curl http://www.example.com  # 测试虚拟主机页面
    systemctl status httpd  # 检查 Apache 服务状态
  • 验证 Nginx:
    bash
    curl http://proxy.example.com  # 测试反向代理
    curl http://lb.example.com  # 测试负载均衡
    nginx -t  # 验证 Nginx 配置
    systemctl status nginx  # 检查 Nginx 服务状态
  • 验证 SSL/TLS:
    bash
    curl -k https://localhost  # 测试 Apache 自签名 HTTPS 页面
    curl https://www.example.com  # 测试 Let’s Encrypt HTTPS 页面
    openssl s_client -connect localhost:443 -brief  # 检查 SSL 证书信息
  • 测试 SSH 连接:
    bash
    ssh -p 2222 testuser@ZSLinux  # 测试 SSH 连接(参考第 6 章)
  • 常见问题:
    • Web 服务不可访问:检查服务状态(systemctl status httpdnginx)或防火墙(firewall-cmd --list-services)。
    • SSL 连接失败:验证证书路径(/etc/ssl/private) 或 SELinux(ausearch -m avc)。
    • 虚拟主机无效:确认配置文件(/etc/httpd/conf.d/vhost.conf) 或 DNS 设置。

22.5 实践任务

  1. 使用 SecureCRT 登录 ZSLinux,安装 Apache 并创建测试页面。
  2. 配置 Nginx 反向代理,指向 192.168.1.101:8080。
  3. 配置 Apache 虚拟主机并验证访问。
  4. 设置 Let’s Encrypt 证书并配置自动续订。

22.6 自测问题

  • 问题:如何配置 Apache 虚拟主机?
    • 答案:编辑 /etc/httpd/conf.d/vhost.conf,添加 <VirtualHost *:80>,设置 ServerNameDocumentRoot,然后 systemctl restart httpd
  • 问题:如何测试 Nginx 配置有效性?
    • 答案nginx -t
  • 问题:如何获取 Let’s Encrypt 证书?
    • 答案certbot --apache -d www.example.comcertbot --nginx -d www.example.com

Released under the MIT License.