第 2 章 设置和配置 NGINX
NGINX 是一个高性能和模块化的服务器,可作为:
- Web 服务器
- 反向代理服务器
- 负载均衡器
这部分论述了如何在这些场景中使用 NGINX。
2.1. 安装并准备 NGINX
在 Red Hat Enterprise Linux 9 中,Application Streams 提供了不同版本的 NGINX 。通过使用默认配置,NGINX 在端口 80
上作为 Web 服务器运行,并提供 /usr/share/nginx/html/
目录中的内容。
先决条件
- 已安装 RHEL 9。
- 主机订阅了红帽客户门户网站。
-
firewalld
服务已经启用并启动。
流程
安装
nginx
软件包:要将 NGINX 1.20 作为 RPM 软件包的此应用程序流的初始版本安装:
# dnf install nginx
注意如果您之前启用了 NGINX 模块流,这个命令会从启用的流中安装 NGINX 版本。
要从模块流安装替代的 NGINX 后期版本:
显示可用的 NGINX 模块流:
# dnf module list nginx ... rhel-AppStream Name Stream Profiles Summary nginx 1.22 common [d] nginx webserver ... Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
启用所选流:
# dnf module enable nginx:stream_version
安装 nginx 软件包:
# dnf install nginx
打开 NGINX 应该在其防火墙中提供其服务的端口。例如,要在
firewalld
中为HTTP(端口 80)和 HTTPS(端口 443)开放默认端口,请输入:# firewall-cmd --permanent --add-port={80/tcp,443/tcp} # firewall-cmd --reload
设置
nginx
服务在系统启动时自动启动:# systemctl enable nginx
另外,也可启动
nginx
服务:# systemctl start nginx
如果您不想使用默认配置,请跳过这一步,并在启动该服务前相应地配置 NGINX。
验证步骤
使用
dnf
工具验证是否已安装nginx
软件包。如果是 NGINX 1.20 RPM 软件包:
# dnf list installed nginx Installed Packages nginx.x86_64 1:1.20.1-4.el9 @rhel-AppStream
如果是所选的是 NGINX 模块流:
# dnf list installed nginx Installed Packages nginx.x86_64 1:1.22.1-3.module+el9.2.0+17617+2f289c6c @rhel-AppStream
确保在 firewalld 中打开了 NGINX 需要的端口:
# firewall-cmd --list-ports 80/tcp 443/tcp
验证
nginx
服务是否已启用:# systemctl is-enabled nginx enabled