Recompile Nginx Installed With Apt

—————
Update, August 2016: This article refers to Nginx 1.5.x. As of version 1.9.11, Nginx supports loading modules dynamically at runtime, so I’d suggest that route instead. If you still want to recompile Nginx, an updated tutorial for recompiling Nginx 1.9+ installed with Apt on Ubuntu 16.04 is available here. Meanwhile, an updated tutorial for compiling Nginx 1.9+ from source on Ubuntu 16.04, without Apt, can be found here.

—————

One minor inconvenience with Nginx, as compared to Apache, is that Nginx has no hook for dynamically adding new modules. Instead you have to recompile Nginx to add them, and the problem there is that recompiling packages by hand can begin to cause problems with your Linux package manager.

To add a new module, you could apt-get purge your existing Nginx installation, then download the source from Nginx.org and recompile Nginx, but you don’t want to blow away your existing configuration files and directories. Ideally you’d just need to add a couple of compiler switches to the binary created by the Ubuntu packaging system while leaving everything else intact.

Grab the Source

One way to do this is to recompile Nginx into a .deb package with the modules you want and install that instead. So download the Nginx source from the ppa repository:

$ cd /opt
$ apt-get build-dep nginx
$ apt-get source nginx

Download one or more modules you want to compile in (we’ll use the push-stream module).

$ cd /opt/nginx-1.5.10/debian/modules
$ git clone https://github.com/wandenberg/nginx-push-stream-module.git

Edit /opt/nginx-1.5.10/debian/rules, find the list of compiler arguments and additional modules, and under ‘full’, add your new modules:

            [...] 
            --with-http_sub_module 
            --with-http_xslt_module 
            --with-ipv6 
            --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 your new modules
            --add-module=$(MODULESDIR)/nginx-push-stream-module 

Recompile Nginx

$ cd /opt/nginx-1.5.10
$ dpkg-buildpackage -uc -b

After everything has compiled, you’ll see the new *.deb packages in the parent directory:

$ ls -al /opt

-rw-r--r--  1 root root   69282 Mar  2 06:24 nginx_1.5.10-1~precise0_all.deb
-rw-r--r--  1 root root    4956 Mar  2 06:26 nginx_1.5.10-1~precise0_amd64.changes
-rw-r--r--  1 root root   83054 Mar  2 06:24 nginx-common_1.5.10-1~precise0_all.deb
-rw-r--r--  1 root root   81786 Mar  2 06:24 nginx-doc_1.5.10-1~precise0_all.deb
-rw-r--r--  1 root root  685964 Mar  2 06:26 nginx-extras_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root 4833698 Mar  2 06:26 nginx-extras-dbg_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root  521218 Mar  2 06:26 nginx-full_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root 3237026 Mar  2 06:26 nginx-full-dbg_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root  348926 Mar  2 06:26 nginx-light_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root 2086840 Mar  2 06:26 nginx-light-dbg_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root  391920 Mar  2 06:26 nginx-naxsi_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root 2241926 Mar  2 06:26 nginx-naxsi-dbg_1.5.10-1~precise0_amd64.deb
-rw-r--r--  1 root root  357934 Mar  2 06:24 nginx-naxsi-ui_1.5.10-1~precise0_all.deb

Remove Nginx:

$ apt-get remove nginx

Make sure you don’t do apt-get purge, or you may lose all your config files.

Since you updated nginx-full, install it:

$ dpkg --install /opt/nginx-full_1.5.10-1~precise0_amd64.deb

Run nginx -V to ensure the Nginx binary shows your new module:

[...] --add-module=/opt/nginx-1.5.10/debian/modules/nginx-push-stream-module

Finally, you want to block further updates from apt-get, or else it’ll just overwrite your custom Nginx packages.

$ apt-mark hold nginx-full

If you want to update Nginx again with apt-get, just run apt-mark unhold nginx-full.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *