Nginx is an awesome highly customable and super fast (web)server, unfortunately configuring and setting him up is not that easy compared to Apache.

Modules could not be loaded later via a config file like Apache modules, instead they must be compiled into the Nginx binary. This often leads to the necessity to compile Nginx from source for a deployment.

Here how I compiled, installed and deployed Nginx on a Ubuntu 12.04 system:

Prepare

Install required packages apart from the usual stuff like make and gcc:

sudo apt-get install libpcre3-dev libssl-dev

Explanation:

  • libpcre3-dev is required for the rewrite modules, it is quite common so I assume you will use it
  • libssl-dev is required for the SSL module. If you do not plan to use it you could skip this.

Set up a system user, which will run the Nginx:

sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx

Compile and install Nginx

Get the sources (check Nginx page before downloading, there might be a newer version available):

wget http://nginx.org/download/nginx-1.2.6.tar.gz

Extract the archive:

tar xvzf nginx-1.2.6.tar.gz

Now change into the directory where the sources where extracted. Use the configure command to configure which modules you want to use later. My confiuration looked like:

./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module

Explanation:

  • --prefix is where Nginx will be installed
  • --user should be the user, that will Nginx. If you want to structure this more like an Apache setup you could choose another username
  • --with-http_ssl_module: enable SSL module
  • --without-XXX: disable the module XXX

An overview of the most common compile time flags for Nginx could be found in the Nginx wiki.

Finally compile and install:

make && sudo make install

Set up Upstart to start and stop Nginx with your system

Most newer Ubuntu system use Upstart to start and stop background services.

This is the Upstart config file that I use. It will start Nginx on start up of your system and shut it down, when the system halts. Please this or a similar file under: /etc/init/nginx.conf.

# nginx

description "nginx http daemon"
author "Philipp Klose <me@'thisdomain'.de>"

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env DAEMON=/opt/nginx/sbin/nginx
env PID=/opt/nginx/logs/nginx.pid

expect fork
respawn
respawn limit 10 5
#oom never

pre-start script
 $DAEMON -t
 if [ $? -ne 0 ]
 then exit $?
 fi
end script

exec $DAEMON

If you installed Nginx in a different directory you need to adjust the paths in the file.

This also gives you the following commands:

  • sudo service nginx start – Start Nginx
  • sudo service nginx stop – Stop Nginx
  • sudo service nginx restart – Restart Nginx. This is usefull if you are changing some configuration

The Upstart configuration files could also be found as a Gist on Github (Download).

Configure Nginx

Configuring Nginx is another complex topic and strongly depends on what you are planing to do with your Nginx instance, therefore there will be no further explanation. The steps above should produce a running Nginx which serves the Nginx default pages on port 80.

The Nginx wiki is always a good place to start reading. For some details on configuration and which modules are available:

A list of example configurations for common use cases you be found here: Nginx configuration examples.