【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

哈哈哈,直接跳到nginx这来了,前面学的tomcat,以及tomcat+nginx还没讲,我装完nginx,在学习nginx配置文件中的变量的使用中,看到有使用到echo这个命令将变量输出,我也写在我的配置文件里,一测试,然后就把报错, 一查才知道这个命令是第三方模块的,然后就开始去百度怎么添加第三方模块,这个问题也是我弄了我一天,看了很多博客才解决的,就赶紧记录下来。

提示:所有操作都是在root用户下完成的。

第一步,下载echo-nginx-module。

下载地址:https://github.com/openresty/echo-nginx-module/releases

第二步,输入命令 nginx -V 查看yum安装的nginx版本信息,会输出对应版本号以及编译信息

【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

然后下载对应版本nginx的tar.gz的压缩包

下载地址:http://nginx.org/en/download.html

第三步,将下载好的 nginx 和 echo-nginx-module 模块通过工具上传到虚拟机上,然后cp到 /usr/local/src 目录下,解压

解压完成以后进入nginx的解压目录

【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

再进行下一步之前,最好把你 /usr/sbin/nginx 备份一下

【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

第四步,配置参数

输入命令:./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module-0.61再在后面加上配置参数

就是nginx -V输出的configure arguments后面的,添加到后面,最终命令就是下面所示:

./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module-0.61 --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'

其中标红的是要改的,这是我虚拟机上的数据,下面是报错解决,你可以在配置参数之前就把下面报错的库都安装了,当然,如果你想一个一个错解决直到安装成功,当然也可以的。

报错解决

1、缺少libxml2/libxslt:

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

解决方案:
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel

2、缺少gd-devel

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

解决方案:
yum -y install gd-devel

3、缺少ExtUtils

./configure: error: perl module ExtUtils::Embed is required

解决方案:
yum -y install perl-devel perl-ExtUtils-Embed

4、缺少GeoIP

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

解决方案:
yum -y install GeoIP GeoIP-devel GeoIP-data

5、缺少Google perftools

./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.

翻译过来就是:Google perftools模快需要Google perftools库,你可以选择不启动或者安装库
我这里就不启用,将参数中的--with-google_perftools_module去掉就可以了

第五,编译

输入:make -j2

编译好以后会在nginx的解压目录objs目录下生成一个nginx文件,先测试一下这个文件,输入 objs/nginx -t,然后看结果。

【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

显示成功,然后用这个文件代替 /usr/sbin/nginx 这个文件,再测试一下,还是成功的,哈哈哈,到这就基本上弄好了。

【CentOS7】yum安装下Nginx添加第三方模块 echo-nginx-module

然后启动nginx,如果出现下面这种情况,可能nginx服务进程卡死了,导致80端口被占用。

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

 解决:

nginx -s stop 或者

先查看进程信息:ps -aux | grep nginx  然后再杀进程 kill 进程号,再重新启动nginx。

到此,这个第三方模块就算是弄好了。