Red Hat Training

A Red Hat training course is available for RHEL 8

第 2 章 设置和配置 NGINX

NGINX 是一个高性能和模块化的服务器,可作为:

  • Web 服务器
  • 反向代理服务器
  • 负载均衡器

这部分论述了如何在这些场景中使用 NGINX。

2.1. 安装并准备 NGINX

红帽使用 Application Streams 来提供不同的 NGINX 版本。您可以执行以下操作:

  • 选择流并安装 NGINX
  • 在防火墙中打开所需端口
  • 启用并启动nginx服务

使用默认配置,NGINX 作为 Web 服务器在端口80上运行,并提供/usr/share/nginx/html/目录中的内容。

先决条件

  • 已安装了 RHEL 8。
  • 主机订阅了红帽客户门户网站。
  • firewalld服务已经启用并启动。

流程

  1. 显示可用的 NGINX 模块流:

    # yum module list nginx
    Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs)
    Name        Stream        Profiles        Summary
    nginx       1.14 [d]      common [d]      nginx webserver
    nginx       1.16          common [d]      nginx webserver
    ...
    
    Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
  2. 如果要安装与默认流不同的流,请选择相关的流:

    # yum module enable nginx:stream_version
  3. 安装nginx软件包:

    # yum install nginx
  4. 打开 NGINX 应该在其防火墙中提供其服务的端口。例如,要在firewalld中为HTTP(端口 80)和 HTTPS(端口 443)开放默认端口,请输入:

    # firewall-cmd --permanent --add-port={80/tcp,443/tcp}
    # firewall-cmd --reload
  5. 设置nginx服务在系统启动时自动启动:

    # systemctl enable nginx
  6. 另外,也可启动nginx服务:

    # systemctl start nginx

    如果您不想使用默认配置,请跳过这一步,并在启动该服务前相应地配置 NGINX。

验证步骤

  1. 使用yum工具来验证是否已安装了nginx软件包:

    # yum list installed nginx
    Installed Packages
    nginx.x86_64    1:1.14.1-9.module+el8.0.0+4108+af250afe    @rhel-8-for-x86_64-appstream-rpms
  2. 确保在 firewalld 中打开了 NGINX 需要的端口:

    # firewall-cmd --list-ports
    80/tcp 443/tcp
  3. 验证nginx服务是否已启用:

    # systemctl is-enabled nginx
    enabled

其它资源