Ubuntu Hardy - 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 Hardy Slice.


Versions

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

Do note that Nginx is also available from Hardy's package manager (aptitude) and is at version 0.5.33.

Using the inbuilt 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 onto 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.

Not many are needed and include pcre, ssl and zlib packages:

sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Download

Now we can download the source code.

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

mkdir ~/sources

Now move into the folder:

cd ~/sources/

And then download the source code:

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

Unpack

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

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

Options

There are quite a few compile time options that are available to use.

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 file: "/usr/local/nginx/conf/nginx.conf"
...

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

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

and navigating to our Slice IP address (assuming you have no other server running):

http://123.45.67.890

You will be greeted with the standard welcome page:

Nginx Welcome Screen

Stop

If you have used Apache or some other web server (or even installing Nginx via a package manager), you may be used to using 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 the next article or two).

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 backslashes (`) 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 deal with learning how to create an init script so we have more control over Nginx and in mirroring the 'Debian' style layout to include sites-available and sites-enabled folders and so on.

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

PickledOnion.

Article Comments:

Max commented Mon Jul 07 15:25:02 UTC 2008:

Cool article. Also note that you said backslashes when you meant backquotes.

cdaaawg commented Wed Jul 16 22:44:25 UTC 2008:

Great article; clear,concise and accurate. Why not use checkinstall instead of make install?

GPS commented Wed Jul 23 21:21:13 UTC 2008:

looks like nginx is up to 0.6.32

Zviki commented Wed Oct 29 14:14:08 UTC 2008:

I would suggest the flag:

--with-httpstubstatus_module

If you want to get status, this is a must. Is there any serious overhead here?

jake commented Thu Dec 04 03:28:28 UTC 2008:

I tried following this with the latest ubuntu distro 8.10. First I ran into not having make installed which I corrected by:

sudo apt-get update

sudo aptitude install build-essential

Then I could not "make" because of this error: :~/src/nginx-0.6.31$ make make -f objs/Makefile make[1]: Entering directory /home/user/src/nginx-0.6.31' gcc -c -O -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Wno-unused-function -Wunused-variable -Wunused-value -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc1: warnings being treated as errors src/core/nginx.c: In function ‘main’: src/core/nginx.c:244: error: ignoring return value of ‘write’, declared with attribute warn_unused_result src/core/nginx.c:249: error: ignoring return value of ‘write’, declared with attribute warn_unused_result src/core/nginx.c:257: error: ignoring return value of ‘write’, declared with attribute warn_unused_result make[1]: *** [objs/src/core/nginx.o] Error 1 make[1]: Leaving directory/home/user/src/nginx-0.6.31' make: * [build] Error 2 Which I tried to circumvent with - NOWARNINGCHECKS=yes ./configure --sbin-path=/usr/local/sbin/ --with-httpsslmodule but make still has build error 2 - any words of wisdom?

Gil Creque commented Tue Oct 06 22:31:24 UTC 2009:

Worked just fine. I also added some aliases to stop and start nginx:

  1. run "nano ~/.bashrc"

  2. Add the folowing to the end of the file, write out, and exit:

alias startnginx="sudo /usr/local/sbin/nginx" alias stopnginx="sudo kill cat /usr/local/nginx/logs/nginx.pid"

  1. run "source ~/.bashrc"

Mirco Veltri commented Sat Oct 31 14:00:43 UTC 2009:

Stop Server Alias in ~/.bashrc

alias stopnginx="/usr/local/sbin/nginx -s stop"

+1 for Zviki advice

Igor commented Fri Oct 21 17:26:44 UTC 2011:

I just want to add that I was able to stop nginx with this >nginx -s stop

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)