Ubuntu Intrepid - Installing Nginx from source

Nginx is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.

This article will look at installing Nginx from source on an Ubuntu Intrepid Slice.


Versions

At the time of writing, the latest stable version of Nginx is 0.6.35. You can check the latest versions and change logs at the main nginx site.

Do note that Nginx is also available from Intrepid's 'aptitude' package manager as version 0.6.32, at the time of writing.

Using the package manager will ensure any dependencies are taken care of and any security issue (if any arise) are taken care of with a simple update. Please see this article if you want to install Nginx with the package manager.

Layout

One final difference that is worth considering is the layout differences between using the packaged version and installing from source.

Ubuntu's packaged version of Nginx uses a 'sites-available' and 'sites-enabled' layout in the same manner as a Debian based Apache install.

Although installing from source does not automatically create this layout, I will show how to adjust the folders and config files to do so.

Mirroring the layout used in the packed version of Nginx gives us 'common ground' when moving on to creating virtual hosts and further configuration articles.

Dependencies

As we are not using the package manager to install nginx, we need to take care of some dependencies. The dependencies list can be generated with aptitude:

aptitude show nginx
...
Depends: libc6 (>= 2.4), libpcre3 (>= 7.4), libssl0.9.8 (>= 0.9.8f-5), zlib1g
         (>= 1:1.1.4), lsb-base (>= 3.2-14)
...

It turns out that a few additional, related packages are needed to successfully install nginx from source — one of the hazards of source installs, and a sign that package managers aren't always 100% accurate in their dependency tracking. But altogether not too many packages are required:

sudo aptitude install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base

Download

Now we can download the source code.

Create a 'sources' directory (you can download it to wherever you want):

mkdir ~/sources

Now move into the folder:

cd ~/sources/

And then download the source code:

wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz

Unpack

Unpack the downloaded tar file and move into the newly created directory:

tar -zxvf nginx-0.6.35.tar.gz
...
cd nginx-0.6.35/

Options

There are quite a few compile time options available.

Have a look at the Install Options page of the nginx wiki for full details.

We're going to use just two options to customise the install. The first of which is:

--sbin-path=/usr/local/sbin

By default, nginx will be installed in /usr/local/nginx which, although a good place, does mean the main nginx binary will be found in /usr/local/nginx/sbin/nginx.

Not a location we are likely to find in our default search paths. So instead of adding new directories to our path (which may cause errors later on) we simply define where to put the binary.

The second option is:

--with-http_ssl_module

Probably self explanatory, but this will enable the ssl module to be compiled so we can parse HTTPS requests.

Compile

Let's go ahead and compile nginx using those two options:

./configure --sbin-path=/usr/local/sbin --with-http_ssl_module

There will be a nice summary at the end of the compile which includes such items as:

...
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/sbin"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
...

Keep a note of the output as it does contain some useful paths and locations.

Make

Ok, let's go ahead and 'make' and then 'make install':

make
...
sudo make install

Defaults

Done. We can test the install by starting nginx:

sudo /usr/local/sbin/nginx

Navigating to your Slice's IP address (assuming you have no other server listening on port 80), you will be greeted with the standard welcome page:

Nginx Welcome Screen

Stop

If you have been running Apache or some other web server (or have previously installed Nginx via a package manager), you may be used to having a script to start and stop it, such as you might find in '/etc/init.d/'.

Unfortunately, compiling from source does not create a script like that (we will create an init script in a subsequent article).

You may have noticed that one of the paths noted in the compile summary was:

nginx pid file: "/usr/local/nginx/logs/nginx.pid"

Let's make use of that to stop nginx:

sudo kill `cat /usr/local/nginx/logs/nginx.pid`

That will use the output of the pid file to kill the nginx process — note the use of back ticks ( ` ` ) and not single quotes ( ' ' ) in the command.

Summary

Although quite detailed, we have installed the latest version of nginx from source and compiled it with some custom options to make life slightly easier.

We also know how to start and stop nginx by hand.

As mentioned, the next couple of articles will cover creation of an init script so we have more control over Nginx, and also mirroring the 'Debian' style layout to include 'sites-available' and 'sites-enabled' folders and so on.

Once finished, we will then look at configuring nginx, setting up vhosts and proxying to a backend (such as a mongrel cluster).

Mike

Article Comments:

Dave commented Sat Mar 07 20:36:50 UTC 2009:

The dependencies you installed before trying to compile nginx are wrong and won't allow you to compile. It will end up complaining about missing libraries. This is the correct command to run prior to trying to compile nginx: sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Mike commented Wed Mar 11 19:19:32 UTC 2009:

Dave, thanks for the tip. You are correct, and the article has been updated. The slice I'm using for the Intrepid tutorials already had those packages installed, i.e. the ones not specified in the 'aptitude show nginx' output, so I didn't note those dependencies on my first pass.

Tony commented Mon Mar 16 10:38:54 UTC 2009:

Thank you for the detailed article.

Since Nginx is installed from source, how does one go about upgrading when a newer version comes out?

Andrew commented Fri May 01 22:38:19 UTC 2009:

^^ good question that i'm interested in as well

pablo commented Tue Jun 02 17:19:41 UTC 2009:

why do you install at --sbin-path=/usr/local/sbin

I thought that in ubuntu the common place is /etc/nginx

For example, apache is installed under etc

nicolas block commented Mon Jan 18 23:53:33 UTC 2010:

you might want to add nginx to your path

PATH=$PATH:/usr/local/sbin/

nartz commented Tue Mar 16 05:24:59 UTC 2010:

Yo this has some errors, it is /usr/local/sbin/nginx NOT /usr/local/nginx, this problem is propogated into the next article and into the /etc/init.d/nginx script, where the paths need to be changed to /usr/local/sbin/nginx/logs, and then the script will work, also changing "PATH = /usr/local/sbing/nginx/sbin/nginx"

Cheri Jones commented Thu Jun 23 03:17:52 UTC 2011:

Just would like to know why "404 Not Found" nginx comes up every time I try to open Yahoo mail ever since I updated Mozilla to 4.0. I don't understand what your website is talking about, or what I need to do to get my email to open without the error message. Please help me...do I need to download something? What?!!!

Jered commented Thu Jun 23 15:50:22 UTC 2011:

Cheri, we can't actually help you with your Yahoo email, I'm afraid. The article, and this site, focuses on helping people host their own web servers.

I would suggest going to Yahoo's web site and go to the "Help/Contact us" link in the bottom right of the site. They should be able to assist you with your email from there.

Thiyagarajan Veluchamy commented Fri Jul 27 05:20:29 UTC 2012:

Thanks, its helped me setup nginx.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)