企业级Nginx安装部署

1. Nginx 安装方式

  • 编译安装:使用源码,根据企业业务需求,需要什么功能,编译时添加什么模块,但是安装过程较为繁琐
  • 自动化安装:配置好安装源,直接通过自动化工具安装即可
  • 二进制安装:解压即用,直接下载已经编译好的可执行文件

2. Nginx 编译安装

2.1 下载源码包

https://nginx.org/en/download.html

本文使用 nginx-1.26.3.tar.gz

2.2 编译安装

# 安装依赖包
$ sudo yum install -y gcc pcre pcre-devel openssl openssl-devel zlib zlib-devel

$ tar -zxvf nginx-1.26.3.tar.gz
$ cd nginx-1.26.3

# 编译
$ ./configure \
	--prefix=/usr/local/nginx \ # 设置 nginx 安装目录
	--sbin-path=/bin/ \ # nginx 命令存放目录
	--user=nginx \ # 设置运行 nginx 服务的用户
	--group=nginx \ # 设置运行 nginx 服务的组
	--with-http_ssl_module \ # 使用 --with 添加了 6 个模块
	--with-http_stub_status_module \
	--with-http_gzip_static_module \
	--with-http_v2_module \
	--with-stream \
	--with-stream_ssl_module

$ make
$ sudo make install

# 测试
$ nginx -v
nginx version: nginx/1.26.3
$ nginx -V
nginx version: nginx/1.26.3
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC) 
built with OpenSSL 3.5.1 1 Jul 2025
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/bin/ --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_v2_module --with-stream --with-stream_ssl_module

# 创建 nginx 用户
$ sudo useradd nginx -M -s /sbin/nologin

# 启动 Nginx 服务
$ nginx

# 验证
$ netstat -lntup | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28787/nginx: master

$ curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

2.3 编译常见报错

# 报错 1 ./configure: error:C compiler cc is not found 
# 报错 1 解决办法
$ sudo yum install -y gcc

# 报错 2 ./configure: error: the HTTP rewrite module requires the PCRE library.
# You can either disable the module by using --without-http_rewrite_module option, 
# or install the PCRE library into the system, 
# or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 
# 报错 2 解决办法
$ sudo yum install -y pcre pcre-devel

# 报错 3 ./configure: error: SSL modules require the OpenSSL library.
# You can either do not enable the modules, or install the OpenSSL libraryinto the system,
# or build the OpenSSL library statically from the sourcewith nginx by using --with-openssl=<path> option.
# 报错 3 解决办法
$ sudo yum install -y openssl openssl-devel

# 报错 4 ./configure: error: the HTTP gzip module requires the zlib library.
# You can either disable the module by using --without-http_gzip_module
# option, or install the zlib library into the system, or build the zlib library
# statically from the source with nginx by using --with-zlib=<path> option.
# 报错 4 解决办法
$ sudo yum install -y zlib zlib-devel

3. YUM 安装 Nginx

https://nginx.org/en/linux_packages.html#RHEL

$ sudo yum install yum-utils

$ sudo tee /etc/yum.repos.d/nginx.repo > /dev/null << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

# 如果只想安装稳定版本,则不要执行这条命令
$ sudo yum-config-manager --enable nginx-mainline

# 关闭 mainline 源
$ sudo yum-config-manager --disable nginx-mainline

$ sudo yum install nginx -y

$ systemctl enable nginx --now

$ sudo ln -s /usr/sbin/nginx /usr/bin/nginx
posted on 2025-10-26 15:07  老地瓜大数据  阅读(0)  评论(0)    收藏  举报