在CentOS 7上编译安装Nginx
# nginx编译安装
- 安装编译所需的依赖包:
sudo yum install -y gcc-c++ pcre-devel zlib-devel openssl-devel wget
1
- 下载 Nginx 1.20.2:
cd /opt
sudo wget http://nginx.org/download/nginx-1.20.2.tar.gz
sudo tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
1
2
3
4
2
3
4
- 配置编译选项:
./configure --user=root --group=root --prefix=/data/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-pcre --with-stream
1
安装多模块
./configure --prefix=/data/nginx --pid-path=/data/nginx/run/nginx.pid --user=root --group=root --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
1
- 编译并安装 Nginx:
sudo make
sudo make install
1
2
2
- 创建 Nginx 系统服务单元文件:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/data/nginx/run/nginx.pid
ExecStartPre=/data/nginx/sbin/nginx -t
ExecStart=/data/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
保存并退出。
- 重新加载 systemctl 守护进程:
sudo systemctl daemon-reload
1
- 启动并设置 Nginx 在开机时自动启动:
sudo systemctl start nginx
sudo systemctl enable nginx
1
2
2
- 检查 Nginx 版本和运行状态:
nginx -v
sudo systemctl status nginx
1
2
2
# nginx编译后添加第三方模块
已经安装好的nginx,需要添加一个未被编译安装的模块,需要如何操作呢?
nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so。这里以安装第三方ngx_http_google_filter_module模块为例:
- 下载第三方扩展模块ngx_http_google_filter_module
# cd /data/software/
# git clone https://github.com/cuber/ngx_http_google_filter_module
1
2
2
- 查看nginx编译安装时安装了哪些模块
# nginx -V nginx version: nginx/1.15.3
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f 25 May 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module
1
2
3
4
5
2
3
4
5
可以看出编译安装使用了--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module这些参数。--add-module=/data/software/ngx_http_substitutions_filter_module是之前编译添加ngx_http_substitutions_filter_module模块时添加
1
- 加入需要安装的模块,重新编译,如这里添加–add-module=/data/software/ngx_http_google_filter_module
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/data/software/ngx_http_substitutions_filter_module --add-module=/data/software/ngx_http_google_filter_module
# make //千万不要make install,不然就真的覆盖了
1
2
2
- 替换nginx二进制文件:
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# cp ./objs/nginx /usr/local/nginx/sbin/
1
2
2
上次更新: 2023/07/10, 21:25:36