Install Nginx from source

Written by Christophe Limpalair on 08/29/2015

Why install from source?
Installing from source is not hard and doesn't take long, but it definitely has more steps than just
apt-get install nginx


So why bother?

Because installing from package doesn't give you the latest version. It installs version 1.4.6 when the latest stable version is 1.8.0, and mainline (development) version is 1.9.3.

This means you miss out on new features and improvements. No worries, I'll walk you through every step of the way.

Installing from source
First, download the newest version. I'm using the stable version for this, but feel free to use the mainline version.

wget "http://nginx.org/download/nginx-1.8.0.tar.gz"


If you do an
ls
, you'll see nginx-1.8.0.tar.gz. Let's extract it.

tar -zxvf nginx-1.8.0.tar.gz


Side note:
-z tells tar to use gzip
-x tells tar to extract files from an archive
-v do it verbosely
-f tells tar the next argument is the name of the archive

Let's go in our new directory:

cd nginx-1.8.0


Depending on your machine, you may already have build tools. If not:

sudo apt-get update

sudo apt-get install build-essential


Nginx doesn't need much to work, but it does have a few dependencies. Let's grab those now.

sudo apt-get install libpcre3-dev zlib1g-dev libssl-dev


Now, let's configure Nginx with a few modules that don't work by default. I'll explain those right after.

./configure --with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module


--with-http_ssl_module Enables SSL.
--with-http_realip_module Forwards the real user IP. Useful in a load balancer or CDN situation, for example.
--width-http_stub_status_module Enables Nginx statistics, like number of active connections.

Let's make and make install.

make


sudo make install


Now check to make sure it installed properly.

/usr/local/nginx/sbin/nginx -V


Great! It's installed. Only problem is that it's not set to boot on startup. Let's set that up now.

wget -O init-deb.sh https://www.linode.com/docs/assets/660-init-deb.sh


This will download the init-deb.sh script from Linode. Take a look at it here if you're curious.

Alternatively, there are scripts straight from Nginx for various distros.

Now let's move this script to init.d and make it executable.

sudo mv init-deb.sh /etc/init.d/nginx


sudochmod +x /etc/init.d/nginx


And finally, add Nginx to system startup. (For Debian)

sudo /usr/sbin/update-rc.d -f nginx defaults


That's it! You should be able to
sudo service nginx start


What now?
If you are running a PHP application, take a look at Nginx + PHP-FPM Installation and Configuration.

I didn't get to cover every setting in that blog post, just ones that matter the most to scalability. If you are curious about the other settings, sit tight. I'll have more on that soon.