Monday, 2 May 2016

How to add module to Nginx

How to add module to Nginx

Adding third party module to Nginx

Compiling third party module with Nginx 

How to add Redis module to Nginx 

How to install modules in Nginx


Adding a third party module to Nginx is really simple as long as you know the correct way. Adding a module, needs the Nginx to be compiled, hence should be installed from the source.
Dis-advantage of installing Nginx from source is that it doesn't provide monitoring and start/stop of Nginx on system boot (upstart) and you don't get logrotate setup for Nginx.
If you want to get rid of  the above disadvantages and want the same functionality which you get by installing it from Debian packages than follow the steps below.

Get the required module

Download the required module and un-tar it. Here we are considering the redis module but the process would be same for all the modules.

cd /opt/
sudo wget https://github.com/openresty/redis2-nginx-module/archive/v0.12.tar.gz
tar -xvzf v0.12.tar.gz

 After getting this you will have the redis2-nginx-module-0.12 directory in /opt/

Get the Nginx source and dependencies

Add the Nginx PPA repository

sudo add-apt-repository -y ppa:nginx/stable
sudo apt-get update

Now Open the PPA source file

sudo vi /etc/apt/sources.list.d/nginx-stable-trusty.list

Add the following line to the file and save it

deb http://ppa.launchpad.net/nginx/stable/ubuntu trusty main
deb-src http://ppa.launchpad.net/nginx/stable/ubuntu trusty main

Run the update

sudo apt-get update

Now we can get Nginx source package, modify it, build it and than install it.

# Install package creation tools
sudo apt-get install -y dpkg-dev

sudo mkdir /opt/rebuildnginx
cd /opt/rebuildnginx

# Get Nginx (ppa:nginx/stable) source files
sudo apt-get source nginx

# Install the build dependencies
sudo apt-get build-dep nginx

To add the module to Nginx open the file /opt/rebuildnginx/nginx-1.8.1/debian/rules and add the module in full_configure_flags as follow

full_configure_flags := \
   $(common_configure_flags) \
   --with-http_addition_module \
   --with-http_dav_module \
   --with-http_geoip_module \
   --with-http_gunzip_module \
   --with-http_gzip_static_module \
   --with-http_image_filter_module \
   --with-http_spdy_module \
   --with-http_sub_module \
   --with-http_xslt_module \
   --with-mail \
   --with-mail_ssl_module \
   --add-module=$(MODULESDIR)/nginx-auth-pam \
   --add-module=$(MODULESDIR)/nginx-dav-ext-module \
   --add-module=$(MODULESDIR)/nginx-echo \
   --add-module=$(MODULESDIR)/nginx-upstream-fair \
   --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module \
                        --add-module=/opt/redis2-nginx-module-0.12 

Save it, now we can build the Nginx.

cd /opt/rebuildnginx/nginx-1.8.1/
sudo dpkg-buildpackage -uc -b 

Install Nginx

Till now there would be a bunch of .deb files in /opt/rebuildnginx/

ls
nginx-1.8.1         nginx-doc_1.8.1-1+trusty0_all.deb
nginx_1.8.1-1+trusty0_all.deb       nginx-extras_1.8.1-1+trusty0_amd64.deb
nginx_1.8.1-1+trusty0_amd64.changes   nginx-extras-dbg_1.8.1-1+trusty0_amd64.deb
nginx_1.8.1-1+trusty0.debian.tar.gz   nginx-full_1.8.1-1+trusty0_amd64.deb
nginx_1.8.1-1+trusty0.dsc       nginx-full-dbg_1.8.1-1+trusty0_amd64.deb
nginx_1.8.1.orig.tar.gz        nginx-light_1.8.1-1+trusty0_amd64.deb
nginx-common_1.8.1-1+trusty0_all.deb  nginx-light-dbg_1.8.1-1+trusty0_amd64.deb

We can use these .deb file to install the Nginx. Install using nginx-full_1.8.1-1+trusty0_amd64.deb file and in case you get an error complaining about the nginx-common_1.8.1-1+trusty0_all.deb should be installed first than install nginx-common_1.8.1-1+trusty0_all.deb first than install nginx-full_1.8.1-1+trusty0_amd64.deb.

cd /opt/rebuildnginx/
sudo dpkg -i nginx-common_1.8.1-1+trusty0_all.deb
sudo dpkg -i nginx-full_1.8.1-1+trusty0_amd64.deb

Congratulations, we have installed the Nginx with the required module. To verify it the module has installed correctly or not run the following command and you will see the new module at the end of the output.


nginx -V
nginx version: nginx/1.8.1
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/opt/rebuildnginx/nginx-1.8.1/debian/modules/nginx-auth-pam --add-module=/opt/rebuildnginx/nginx-1.8.1/debian/modules/nginx-dav-ext-module --add-module=/opt/rebuildnginx/nginx-1.8.1/debian/modules/nginx-echo --add-module=/opt/rebuildnginx/nginx-1.8.1/debian/modules/nginx-upstream-fair --add-module=/opt/rebuildnginx/nginx-1.8.1/debian/modules/ngx_http_substitutions_filter_module --add-module=/opt/redis2-nginx-module/redis2-nginx-module-0.12


Now the Nginx should work as it was suppose to when installed by Debian package. All commands like sudo service nginx start/stop/restart will work fine and everything would be same as it is being installed by Debian package.

3 comments:

  1. Hey, great article! I installed as supposed and it works. But today I ran "apt-get dist-upgrade" and somehow the nginX were replaced and my modules are not installed anymore. I will try to reinstall but how can I avoid this in future?

    ReplyDelete
    Replies
    1. You can put the package on hold to ignore udpate of a particular package. For example:
      $ echo "foo hold" | dpkg --set-selections
      This will put the package foo on hold while updating.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel