1.首先查看目前nginx编译的时候都带了哪些模块,重新编译的时候都要带上。
nginx -V
[root@10-10-10-10 sbin]# ./nginx -V
nginx version: nginx/1.15.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=service --group=service --prefix=/home/service/app/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_v2_module --with-pcre
2.1 安装依赖
如果没有相关依赖,编译的时候会报错:GeoIP module requires the GeoIP library
yum install GeoIP-devel.x86_64 -y
2.2 找到安装nginx的源码目录。如果没有的话可以直接重新下载一个相同版本的包。
我需要添加--with-http_geoip_module,所以将上面nginx -V查出来的configure arguments的编译参数和要新增的模块参数一起加上。
#进入nginx源码目录
cd nginx-1.15.5
#编译:./configure&make ##千万不要make install !!!
./configure --user=service --group=service --prefix=/home/service/app/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_v2_module --with-pcre --with-http_geoip_module
make
千万不要make install!!!
3.替换编译的新的nginx命令文件
新编译的命令文件在源码的objs目录下
#备份旧nginx命令文件
mv nginx/sbin/nginx nginx/sbin/nginx.bak-20220119
#替换新编译的nginx命令文件
cp nginx-1.15.5/objs/nginx nginx/sbin/
4.验证&热重载
#验证模块编译成功
./nginx/sbin/nginx -V
#验证新nginx命令和配置文件是否适配
./nginx/sbin/nginx -t
#热重载
./nginx/sbin/nginx -s reload