WEB服务与NGINX(15)-NGINX安装第三方模块
1.nginx安装第三方模块
nginx安装第三方模块需要进行编译安装,安装方法如下:
./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 ...
注意:编译第三方模块时需要把原来编译的选项全部加上,否则新编译的功能不回包含之前的编译功能,查看当前nginx的编译选项的方法为:
[root@xuzhichao ~]# nginx -V
注意:编译第三方模块时只需要执行make,不需要make install,否则会完全覆盖之前的nginx。
下面以nginx的第三方echo模块安装为例进行演示:
-
第一步:测试nginx的配置文件
[root@xuzhichao ~]# cat /etc/nginx/conf.d/vhost.conf [root@xuzhichao nginx-1.20.1]# cat /etc/nginx/conf.d/vhost.conf server { listen 80; server_name www.module.com; root /data/nginx/html/web01; location / { index index.html; } location /test/ { echo "this is a test dir!"; } location /main { index index.html; echo "hello world,main-->"; echo_reset_timer; echo_location /sub1; echo_location /sub2; echo "took $echo_timer_elapsed sec for total."; } location /sub1 { echo_sleep 1; echo sub1; } location /sub2 { echo_sleep 1; echo sub2; } } #nginx不识别echo语句。 [root@xuzhichao ~]# nginx -t nginx: [emerg] unknown directive "echo" in /etc/nginx/conf.d/vhost.conf:11 nginx: configuration file /etc/nginx/nginx.conf test failed
-
进行安装echo模块,使用wget或git下载echo模块
#下载echo模块 [root@xuzhichao ~]# yum install git -y [root@xuzhichao ~]# git clone https://github.com/openresty/echo-nginx-module.git #新建third_moule目录专门用于存放第三方模块 [root@xuzhichao ~]# cd nginx-1.20.1/ [root@xuzhichao nginx-1.20.1]# mkdir third_moule [root@xuzhichao nginx-1.20.1]# cp -a /root/echo-nginx-module/ third_moule/
-
重新编译
#首先查看当前nginx的编译选项 [root@xuzhichao nginx-1.20.1]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_dav_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-file-aio #编译第三方模块 [root@xuzhichao nginx-1.20.1]# ./configure --prefix=/apps/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_dav_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-file-aio --add-module=/root/nginx-1.20.1/third_moule/echo-nginx-module #执行make操作 [root@xuzhichao nginx-1.20.1]# make ......
-
替换nginx二进制文件
#查看当前nginx二进制文件路径 [root@xuzhichao ~]# which nginx /usr/sbin/nginx #复制时报错,因为nginx正在运行 [root@xuzhichao nginx-1.20.1]# cp objs/nginx /usr/sbin/nginx cp: overwrite ‘/usr/sbin/nginx’? y cp: cannot create regular file ‘/usr/sbin/nginx’: Text file busy #强行复制,成功。 [root@xuzhichao nginx-1.20.1]# cp objs/nginx /usr/sbin/nginx -f cp: overwrite ‘/usr/sbin/nginx’? y
-
重启nginx服务
#检测语法无报错 [root@xuzhichao nginx-1.20.1]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #重启nginx服务,restart而不是reload,新模块需要重启生效 [root@xuzhichao nginx-1.20.1]# systemctl restart nginx #查看nginx编译选项 [root@xuzhichao nginx-1.20.1]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/apps/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_dav_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-file-aio --add-module=/root/nginx-1.20.1/third_moule/echo-nginx-module
-
客户端测试访问
[root@nginx01 ~]# curl http://www.module.com/test/ this is a test dir! [root@nginx01 ~]# curl http://www.module.com/main/ hello world,main--> sub1 sub2 took 2.019 sec for total.