Nginx之RPM安装后添加第三方skicky模块
1. 场景简述
实际的生产环境或测试环境中,大家可能图简便会直接yum安装Nginx,来达到快速部署及上线的目的,但这样会带来一个问题,如果业务量增大,并且Nginx此时的配置无法满足性能上的要求,需要添加第三方模块来升级Nginx。但是yum方式安装的Nginx是无法直接添加第三方模块的,安装路径也是默认的。必须使用编译方式安装。
我们使用nginx -V命令可以查看yum方式安装的Nginx默认使用了哪些模块及功能,然后直接将如下图红框部分的命令copy出来,因为我们需要使用其重新编译Nginx。
注意:需要从Nginx官方站点下载一个同版本的可编译安装的包;我这里是Nginx/1.12.2。
2. 前期准备
- 安装编译必备组件及解决依赖关系
[root@lbserver ~]# yum -y install gcc pcre-devel openssl-devel libxslt-devel libxml2 gd-devel geoip-devel perl-ExtUtils-Embed
- 备份nginx配置文件及nginx程序等
[root@lbserver ~]# cp -r /etc/nginx{,_$(date +%F)bak}
[root@lbserver ~]# cp /usr/sbin/nginx{,_$(date +%F)bak}
- 下载sticky和nginx-1.12.2的编译安装包
## 下载sticky包
[root@lbserver ~]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
## 下载nginx包
[root@lbserver ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@lbserver ~]# ll nginx-*
-rw-r--r-- 1 root root 981687 Jan 5 22:41 nginx-1.12.2.tar.gz
-rw-r--r-- 1 root root 120603 Jan 5 22:31 nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz
3. 编译安装nginx
添加第三方模块只能通过编译安装的方式,我们按照1中所描述的,将nginx -V等到的configure结果复制出来。
## 解压
[root@lbserver ~]# tar xf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz
[root@lbserver ~]# tar xf nginx-1.12.2.tar.gz
[root@lbserver ~]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky
## 查看不支持--with-sticky
[root@lbserver ~]# cd nginx-1.12.2
[root@lbserver nginx-1.12.2]# ./configure --help|grep sticky
## 编译升级安装,注意,这里去掉了--with-google_perftools_module
[root@lbserver nginx-1.12.2]# ./configure --add-module=/root/nginx-sticky/ --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-file-aio --with-ipv6 --with-http_auth_request_module --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_geoip_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-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
.....
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/share/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/lib64/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"
./configure: warning: the "--with-ipv6" option is deprecated
## 注意,这里不要make install,不然会覆盖原来的安装,直接make即可;
[root@lbserver nginx-1.12.2]# make -j2
## 编译完成后,会在objs目录中生成一个nginx文件,先验证
[root@lbserver nginx-1.12.2]# objs/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lbserver nginx-1.12.2]# objs/nginx -V
## 停止nginx
[root@lbserver nginx-1.12.2]# systemctl stop nginx.service
## 替换nginx
[root@lbserver nginx-1.12.2]# cp objs/nginx /usr/sbin/nginx
cp: overwrite ‘/usr/sbin/nginx’? y
## 替换完成后重新启动nginx
[root@lbserver nginx-1.12.2]# systemctl start nginx.service
[root@lbserver nginx-1.12.2]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2019-01-05 23:44:03 CST; 3s ago
Process: 13990 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 13986 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 13984 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 13992 (nginx)
CGroup: /system.slice/nginx.service
├─13992 nginx: master process /usr/sbin/nginx
├─13993 nginx: worker process
├─13994 nginx: cache manager process
└─13995 nginx: cache loader process
Jan 05 23:44:03 lbserver systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 05 23:44:03 lbserver nginx[13986]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 05 23:44:03 lbserver nginx[13986]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 05 23:44:03 lbserver systemd[1]: Started The nginx HTTP and reverse proxy server.
由此,即使是yum方式安装nginx,我们也可以通过编译的方式添加第三方模块。