1.首先使用nginx -V,查看当前nginx的信息,包括版本号和configure编译配置信息
[root@datuanzi ~]# nginx -V
nginx version: nginx/1.21.3
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --with-http_geoip_module
[root@datuanzi ~]# whereis nginx
nginx: /etc/nginx /usr/local/nginx /usr/share/nginx /usr/local/nginx/sbin/nginx
2.下载相同版本号的源码
cd /tmp
wget http://nginx.org/download/nginx-1.21.3.tar.gz
3.解压,cd到源码目录
[root@datuanzi tmp]# tar -zxvf nginx-1.21.3.tar.gz
[root@datuanzi tmp]# ll
total 1044
drwxr-xr-x 8 webop2 webop2 158 Sep 7 23:21 nginx-1.21.3
-rw-r--r-- 1 root root 1066609 Sep 7 23:25 nginx-1.21.3.tar.gz
[root@datuanzi tmp]# cd nginx-1.21.3/
下载解压要编译的模块源码和nginx源码目录同一目录,本次ssl模块不需要下载,ngnix默认有。
在nginx源码目录下执行configure,configure参数包括已安装的参数和你要添加的模块
例如
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
./configure --with-http_geoip_module --with-http_ssl_module
–with-http_ssl_module 就是我要添加的模块
4.configure完成后执行make编译
make
记住不要make install
编译后的nginx在当前目录的objs文件夹内。
[root@datuanzi nginx-1.21.3]# ll objs/
total 5960
-rw-r--r-- 1 root root 18643 Nov 27 10:04 autoconf.err
-rw-r--r-- 1 root root 41559 Nov 27 10:04 Makefile
-rwxr-xr-x 1 root root 5950984 Nov 27 10:05 nginx
-rw-r--r-- 1 root root 5537 Nov 27 10:05 nginx.8
-rw-r--r-- 1 root root 7351 Nov 27 10:04 ngx_auto_config.h
-rw-r--r-- 1 root root 657 Nov 27 10:03 ngx_auto_headers.h
-rw-r--r-- 1 root root 6144 Nov 27 10:04 ngx_modules.c
-rw-r--r-- 1 root root 55352 Nov 27 10:05 ngx_modules.o
drwxr-xr-x 9 root root 91 Nov 27 10:04 src
[root@datuanzi nginx-1.21.3]#
5.备份 /usr/local/nginx/sbin目录下的nginx文件
[root@datuanzi ~]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@datuanzi ~]# ll /usr/local/nginx/sbin
total 10240
-rwxr-xr-x 1 root root 5242776 Nov 26 23:30 nginx
-rwxr-xr-x 1 root root 5242776 Nov 27 10:22 nginx.bak
6.停止nginx进程,替换nginx二进制文件。
停止nginx进程
/usr/local/nginx/sbin/nginx -s stop
把编译后的objs文件夹内的nginx文件移动到 /usr/local/nginx/sbin 文件夹内
[root@datuanzi objs]# cp /tmp/nginx-1.21.3/objs/nginx /usr/local/nginx/sbin/nginx
cp: overwrite '/usr/local/nginx/sbin/nginx'? y
[root@datuanzi objs]# ll /usr/local/nginx/sbin/nginx
-rwxr-xr-x 1 root root 5950984 Nov 27 10:31 /usr/local/nginx/sbin/nginx
7.运行nginx -V发现已经包含需要的模块了
[root@datuanzi objs]# /usr/local/nginx/sbin/nginx
[root@datuanzi objs]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.3
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --with-http_geoip_module --with-http_ssl_module
总结:不要make install 而是直接把目录下objs/nginx文件覆盖旧的nginx文件.如果你需要安装多个nginx第三方模块,只需指定多个–add-module即可.