跳转至

20. Web 服务配置

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


20.1 前提条件

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

💡 提示:Web 服务配置需考虑安全(如 SSL)和性能(如负载均衡)。在 RHEL 10 中,Apache 和 Nginx 支持更多模块化安装和内置容器集成,提升部署灵活性。


20.2 Web 服务器配置

20.2.1 配置 Apache

  • 安装 Apache:

    dnf install -y httpd  # 安装 Apache HTTP 服务器
    systemctl enable --now httpd  # 启用并启动 httpd 服务
    

  • 创建测试页面:

    echo "<h1>Hello from Apache on LinuxDC</h1>" > /var/www/html/index.html  # 创建测试网页
    

  • 防火墙配置:

    firewall-cmd --permanent --add-service=http
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload
    

  • SELinux 配置:

    setsebool -P httpd_can_network_connect 1  # 允许 Apache 网络连接(若需)
    restorecon -Rv /var/www/html  # 恢复 SELinux 上下文
    

🧠 知识点:Apache 使用模块化配置,支持 MPM(如 event)优化并发。验证:curl localhost 显示页面。

检查点systemctl status httpd 显示 active,浏览器访问 http://192.168.1.100 显示测试页。

20.2.2 配置 Nginx

  • 安装 Nginx:

    dnf install -y nginx  # 安装 Nginx 服务器
    systemctl enable --now nginx  # 启用并启动 nginx 服务
    

  • 创建测试页面:

    echo "<h1>Hello from Nginx on LinuxDC</h1>" > /usr/share/nginx/html/index.html  # 创建测试网页
    

  • 防火墙配置:同 Apache,使用 http/https 服务。

  • SELinux 配置:

    setsebool -P httpd_can_network_connect 1  # 允许 Nginx 网络连接(Nginx 使用 httpd 布尔值)
    restorecon -Rv /usr/share/nginx/html
    

💡 提示:Nginx 适合高并发场景。测试配置:nginx -t

检查点curl localhost 显示 Nginx 页面。


20.3 高级配置

20.3.1 配置虚拟主机(Apache 示例)

  • 配置虚拟主机:
    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
    

🧠 知识点:虚拟主机支持多站点。Nginx 类似使用 server 块。

20.3.2 配置反向代理(Nginx 示例)

  • 配置反向代理:
    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 && systemctl restart nginx
    

💡 提示:反向代理隐藏后端服务器。

20.3.3 配置负载均衡(Nginx 示例)

  • 配置 upstream:
    vim /etc/nginx/conf.d/lb.conf
    # 添加:
    upstream backend {
        server 192.168.1.101:80;
        server 192.168.1.102:80;
    }
    server {
        listen 80;
        location / {
            proxy_pass http://backend;
        }
    }
    systemctl restart nginx
    

🧠 知识点:负载均衡支持 round-robin 等算法。

20.3.4 配置 SSL/TLS(Let’s Encrypt 示例)

  • 安装 certbot:

    dnf install -y certbot certbot-apache certbot-nginx
    

  • 获取证书(Apache):

    certbot --apache -d www.example.com  # 自动配置 Apache
    

  • 获取证书(Nginx):

    certbot --nginx -d www.example.com  # 自动配置 Nginx
    

  • 配置自动续订:

    systemctl enable --now certbot-renew.timer  # 启用定时器
    

💡 提示:Let’s Encrypt 提供免费证书。验证:openssl s_client -connect www.example.com:443。在 RHEL 10 中,certbot 支持更多 ACME v2 增强,提升自动化。

检查点:浏览器访问 https://www.example.com 显示安全锁图标。


20.4 常见问题与排查

🔍 故障排查

  • 问题 1:服务启动失败
    解决journalctl -u httpdnginx -t 检查日志/语法。

  • 问题 2:端口未开放
    解决firewall-cmd --list-services 检查,添加 http/https。

  • 问题 3:SELinux 阻挡
    解决ausearch -m avc 查看拒绝,setseboolsemanage 修正。

  • 问题 4:证书无效
    解决:检查域名解析(dig www.example.com),certbot 日志(/var/log/letsencrypt)。

  • 问题 5:虚拟主机无效
    解决:确认配置文件(/etc/httpd/conf.d/vhost.conf)或 DNS 设置。


20.5 实践任务

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

检查点:服务运行,代理/均衡正常,HTTPS 安全访问。


20.6 自测问题

  • 问题 1:如何配置 Apache 虚拟主机?
    答案:编辑 /etc/httpd/conf.d/vhost.conf,添加 <VirtualHost *:80>,设置 ServerNameDocumentRoot,然后 systemctl restart httpd

  • 问题 2:如何测试 Nginx 配置有效性?
    答案nginx -t

  • 问题 3:如何获取 Let’s Encrypt 证书?
    答案certbot --apache -d www.example.comcertbot --nginx -d www.example.com


🧾 总结
本章完整介绍了 RHEL 9 Web 服务配置的流程,包括 Apache/Nginx 的安装与高级功能。这些技能连接存储到业务应用,为后续数据库和邮件服务提供 Web 基础。