概述
本章,您将学习到 Nginx 的多种安装方式,包括常见的:
- 存储库
- 源代码编译安装
- Docker 容器技术
存储库
应用程序流:在 Rocky Linux 8.x 和 9.x 中使用了一种新的模块化技术,允许存储库托管多个版本的应用程序及其依赖项。
由于 RL 8.x 和 RL 9.x 使用了应用程序流这一技术,因此系统管理员可以根据需要选择特定的流(版本)以及模块配置集(规定了为特定使用场景而绑定在一起的一组软件包列表)。
默认情况下,若要在 RL 8.x 和 RL 9.x 中安装 Nginx,直接键入以下命令即可:
Shell > dnf module list nginx
Last metadata expiration check: 0:01:04 ago on Mon 17 Aug 2026 01:01:16 PM CST.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
nginx 1.14 [d] common [d] nginx webserver
nginx 1.16 common [d] nginx webserver
nginx 1.18 common [d] nginx webserver
nginx 1.20 common [d] nginx webserver
nginx 1.22 common [d] nginx webserver
nginx 1.24 common [d] nginx webserver
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
# 此时安装的 Nginx 版本为 1.14
Shell > dnf -y install nginx
若需要在 RL 8.x 和 RL 9.x 中安装其他版本的 Nginx,请参考以下命令:
# 启用对应的流
Shell > dnf -y module enable nginx:1.24
# 查询 Nginx 模块流信息
Shell > dnf module list nginx
Last metadata expiration check: 0:10:05 ago on Mon 17 Aug 2026 01:01:16 PM CST.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
nginx 1.14 [d] common [d] nginx webserver
nginx 1.16 common [d] nginx webserver
nginx 1.18 common [d] nginx webserver
nginx 1.20 common [d] nginx webserver
nginx 1.22 common [d] nginx webserver
nginx 1.24 [e] common [d] nginx webserver
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
# 安装 Nginx 模块的特定流以及模块配置集
Shell > dnf module install nginx:1.24/common
若您使用的 RL 10.x 操作系统,安装 Nginx 需要键入以下命令即可:
Shell > dnf5 -y install nginx
源代码编译安装
若部署 Nginx 的机器数量本来就少(少于 10 台机器)且对性能有极致的要求,则推荐使用源代码编译安装。后续的文档都会围绕这种安装方式进行说明。
相关步骤如下:
# 添加系统组(伪用户组) nginx
Shell > groupadd -r nginx
# 添加系统用户(伪用户) nginx
Shell > useradd -r -g nginx -s /sbin/nologin nginx
Shell > id nginx
uid=993(nginx) gid=988(nginx) groups=988(nginx)
# 下载源码压缩包
Shell > wget -c https://nginx.org/download/nginx-1.30.4.tar.gz
# 解压并提取文件
Shell > tar -xvf nginx-1.30.4.tar.gz -C /usr/local/src/
# 变更工作目录
Shell > cd /usr/local/src/nginx-1.30.4/
# 查看编译选项
Shell > ./configure --help
Nginx 的编译选项主要分为四大块:
- 核心选项
- 按需启用的功能模块选项
- 性能与安全增强选项
- 第三方模块选项
核心选项如下表所示:
| 选项 | 说明 | 示例 |
|---|---|---|
--prefix=PATH |
指定 Nginx 安装的根目录 | --prefix=/usr/local/nginx |
--sbin-path=PATH |
指定 Nginx 可执行文件的路径 | --sbin-path=/usr/local/nginx/sbin/nginx |
--modules-path=PATH |
指定 Nginx 模块的存放目录路径 | --modules-path=/usr/local/nginx/modules |
--user=NAME |
指定 Nginx 工作进程所使用的用户,若不指定,则默认使用 nobody 使用 | --user=nginx |
--group=NAME |
指定 Nginx 工作进程所使用的用户组 | --group=nginx |
按需启用的功能模块选项如下表所示:
| 选项 | 说明 |
|---|---|
--with-http_ssl_module |
为 Nginx 提供 HTTPS (SSL/TLS) 支持。必须开启 |
--with-http_v2_module |
启用 HTTP/2 协议支持 |
--with-http_v3_module |
启用 HTTP/3 协议支持 |
--with-http_realip_module |
用于获取经过代理或 CDN 后的客户端真实 IP 地址 |
--with-http_gzip_static_module |
启用 Nginx 的静态预压缩功能,适用于那些静态资源较多的 Web 站点 |
--with-http_stub_status_module |
提供一个简单的状态页,用于监控 Nginx 的基本运行指标 |
--with-http_dav_module |
增加对 WebDAV 协议的支持 |
--with-http_sub_module |
用于修改网站响应内容中的字符串 |
--with-stream |
启用 Stream 模块,用来处理第四层(传输层)的流量,即所有使用 TCP 或 UDP 协议的流量都能被该模块转发,相当于是一个 "四层负载均衡器" |
性能与安全增强选项如下表所示:
| 选项 | 说明 |
|---|---|
--with-threads |
启用线程池功能,特别适用于那些高 I/O 的场景,如需要传输大量的小文件 |
--with-file-aio |
启用 GNU/Linux 的 AIO (asynchronous file I/O,异步文件 I/O),能提升大文件或静态资源的服务性能 |
--with-http_secure_link_module |
启用防盗链功能。简单来说,对应模块会通过 "加密签名 + 过期时间" 的这种机制为每个资源请求生成一个有时效性的、独一无二的访问令牌。只有携带正确令牌且在有效期内的请求,才会被 Nginx 放行,否则直接拒绝访问 |
第三方模块选项如下表所示:
| 选项 | 说明 |
|---|---|
--add-module=PATH |
指定第三方模块的源码路径,将其静态编译进 Nginx。多个模块可重复使用该选项 |
开始编译源代码并进行安装:
Shell > ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_dav_module \
--with-stream \
--with-threads \
--with-file-aio \
&& make && make install
执行后的输出的文本如下所示:
...
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
|| mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/usr/local/src/nginx-1.30.4'
若编译失败,则是一些依赖的环境没有安装,如 gcc、gcc-c++、openssl、openssl-devel、make、pcre、pcre-devel、gzip 等。
安装成功后的信息:
Shell > /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.30.4
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-28) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --modules-path=/usr/local/nginx/modules --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_realip_module --with-http_gzip_static_module --with-http_dav_module --with-stream --with-threads --with-file-aio
Docker 容器技术
您也可以使用 Docker 容器技术来安装 Nginx,步骤如下:
# 拉取 Nginx 镜像
Shell > docker pull nginx:latest
# 查询镜像信息
Shell > docker inspect nginx:latest
# 提前在宿主机上创建所需的目录,并将已经配置好的 nginx.conf 放入到该目录中
## 生成容器实例
Shell > docker run -d --name=web-nginx -v /web-nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-p 80:80 -p 443:443 nginx:latest
# 查看容器实例的状态与信息
Shell > docker ps
# 查询容器实例的日志信息
Shell > docker logs web-nginx
版权声明:「自由转载-保持署名-非商业性使用-禁止演绎 3.0 国际」(CC BY-NC-ND 3.0)
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论










