2 years ago by: matt

More of a reference for myself but may come in useful for someone else too.

Start off by downloading the latest version you need. In my case im going for 1.9.12. I usually throw all of this into /usr/local/src.
Keep in mind, I am running all of this as sudo, if you dont know what your doing do some reading on the linux superuser.

cd /usr/local/src
wget http://nginx.org/download/nginx-{VERSION}.tar.gz
gunzip nginx-{VERSION}.tar.gz
tar -xvf nginx-{VERSION}.tar
cd nginx-{VERSION}

A former colleague of mine showed me this neat little way of keeping track of your compile history (I am aware there is the compile log if needed) by simply creating a "configure.sh" script that you run your configure from. That way down the line you can just open it up to see the current compile state or simply recompile without having to enter all the options again.

nano configure.sh

This file contains all of the configure options. In my case the guys im currently work for need to be able to use nginx as a TCP proxy. So we will be using the stream module which previously needed to be downloaded separately through the projects github page. Since nginx 1.9.0 this module comes as part of the nginx core. Along with that we will be using the pcre and zlib libraries. You can read more about the options available here.

So I have the following in my configure.sh file:

sudo ./configure \
        --prefix=/usr/local/nginx-1.9.12 \
        --with-http_ssl_module \
        --with-stream \
        --with-pcre=/usr/local/src/pcre-8.21 \
        --with-zlib=/usr/local/src/zlib-1.2.5 \

Make sure the libraries you are passing to it are actually supported. You can check the correct version on the nginx documentation. I like to prefix all of my source builds with their own versions into /usr/local to just keep everything separate. That way I could potentially run multiple versions with their own dedicated config files etc.. Next set the configure.sh script as executable and run it:

chmod +x configure.sh

Just double check the output of the configuration summary to make sure it is what your expecting. If you all happy you can go ahead and build/install.

make install

And there you have it. If you cd into the prefix you specified you should see nginx installed there. I am not going to go into configuration as there is a billion resources for whatever you are working with. 

So if you need to make changes in a few months time you can simply open up configure.sh and reconfigure as needed.