总体思路:下载和当前系统相同版本的nginx,将要添加的模块在编译时添加编译,然后将编译好的nginx可执行文件替换原来/usr/sbin/nginx文件
1. 查看nginx版本信息
root@www:~# nginx -V
nginx version: nginx/1.14.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-15) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug
2. 对现有的nignx进行备份
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp -r /etc/nginx /etc/nginx.bak
3. 下载和此版本相同的nginx源码包并解压
wget http://nginx.org/download/nginx-1.XX.X.tar.gz
tar -xvzf nginx-1.XX.X.tar.gz
4. 更新一下相关依赖包
yum -y install libxml2 libxml2-devel libxslt-devel gd gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
yum -y install --skip-broken gcc gcc-c++ autoconf automake gperftools
yum -y install --skip-broken zlib zlib-devel openssl-devel pcre-devel
yum -y install pcre*
5. 停止nginx服务
systemctl stop nginx
6. 进入新下载的nginx目录,进行编译,添加模块为 with-http_geoip_module
cd ngxin-1.XX.X
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-http_geoip_module
make #千万不要make install
7. 测试是否编译成功nginx -t
8. 替换使用新的nginxcp /usr/sbin/nginx /usr/sbin/nginx-bak
cp ./objs/nginx /usr/sbin/nginx
9. 重启nginxsystemctl start nginx
其间可能遇到的问题及相应的解决方案:
nginx安装error: perl module ExtUtils::Embed is required-IDC资讯中心编译安装nginx的时候–with-http_perl_module 这个参数,编译时出现如下错误:error: perl module ExtUtils::Embed is required解决办法安装依赖:yum -y install perl-devel perl-ExtUtils-Embedhttp://www.west999.com/www/info/88675-1.htm/configure: error: the HTTP XSLT module requires the libxml2/libxslt_小白正在飞的博客-优快云博客安装nginx 报错/configure: error: the HTTP XSLT module requires the libxml2/libxsltyum -y install libxml2 libxml2-devyum -y install libxslt-devel
https://blog.youkuaiyun.com/ukgikgb/article/details/106923724安装nginx报错,make命令error: ‘struct crypt_data’ has no member named ‘current_salt’ cd.current_salt[0_Jchgen的博客-优快云博客被坑坏了!第一步:vim src/os/unix/ngx_user.c把cd这一行注释掉。第二步:如果还不行的话 vim objs/Makefile把这句中的 - -Werror删掉。然后再进行make就可以了。
https://blog.youkuaiyun.com/mercycpp/article/details/121629994