插件窝 干货文章 centos7编译安装nginx1.16.0的方法是什么

centos7编译安装nginx1.16.0的方法是什么

Nginx sudo bash 编译 501    来源:    2025-04-27

CentOS 7 编译安装 Nginx 1.16.0 方法

以下是详细的编译安装 Nginx 1.16.0 的步骤:

1. 安装编译依赖

sudo yum install -y gcc gcc-c++ make automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

2. 创建系统用户和组

sudo groupadd www
sudo useradd -g www www -s /sbin/nologin

3. 下载 Nginx 1.16.0 源代码

wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0

4. 配置编译选项

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-threads \
--with-stream

5. 编译并安装

make && sudo make install

6. 创建软链接到系统路径

sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

7. 创建 systemd 服务文件

创建 /usr/lib/systemd/system/nginx.service 文件:

sudo vi /usr/lib/systemd/system/nginx.service

内容如下:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8. 启动 Nginx 并设置开机自启

sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

9. 验证安装

nginx -v

应该显示:nginx version: nginx/1.16.0

10. 防火墙设置(如果需要)

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

可选配置

如果需要添加更多模块,可以在 configure 步骤中添加相应的选项,例如:

--with-http_v2_module \
--with-http_sub_module \
--with-stream_ssl_module

编译完成后,可以通过 nginx -V 查看已安装的模块列表。