Ubuntu Intrepid - Nginx from source layout

Following from the main installing Nginx from source and creating an init script articles we can now move onto looking at the Nginx layout.

The idea of this article is to change the default layout to one more in keeping with the 'Debian' style. Installing Nginx via the package manager creates this layout automatically — we need to do it by hand.


You do, of course, have the option of leaving the layout exactly as it is.

However, any further Nginx articles will use the 'Debian' layout which includes sites-available and sites-enabled folders.

This gives common ground to those who installed Nginx via the 'aptitude' package manager and those who installed Nginx via source.

As an aside, I also find it a very convenient and easy way of organising my virtual hosts.


As alluded to, the layout we want to create is the same as if we installed Nginx via the package manager.

The main difference being that any any virtual host files are kept in a 'sites-available' folder — each vhost has its own file and configurations.

This makes for easy administration as they are not all placed in one unwieldy monolithic file.

One thing to note is that any vhosts in the 'sites-available' folder are not 'live' — to enable a vhost, we simply place a symlink in a 'sites-enabled' folder pointing to the vhost file.


Let's start by creating the two main folders (remember Nginx was installed into '/usr/local/nginx'):

sudo mkdir /usr/local/nginx/sites-available
sudo mkdir /usr/local/nginx/sites-enabled



Now we need to adjust the main Nginx configuration file to look in the sites-enabled folder for our vhosts:

sudo nano /usr/local/nginx/conf/nginx.conf

I won't list the contents of the file here but note that the vast majority of the contents consists of two virtual hosts — a default for port 80 and a default for port 443 (which is actually commented out).

This is a great example of an unwieldy file — it contains just two virtual hosts and is already difficult to follow in a terminal.

The easiest thing is to delete the contents of the file (you can make a copy if you wish) and replace it with this:

user www-data;
worker_processes  1;

events {
    worker_connections  1024;

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    include /usr/local/nginx/sites-enabled/*;


The contents are very similar to those created when installing Nginx via the package manager.

Let's save the file and move onto creating a default vhost.

Default vhost

Create a default vhost file in the sites-available folder:

sudo nano /usr/local/nginx/sites-available/default

The contents are very simple and come from the original configuration:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;


To enable the vhost all we need to do is create a symlink:

sudo ln -s /usr/local/nginx/sites-available/default /usr/local/nginx/sites-enabled/default


Restart Nginx:

sudo /etc/init.d/nginx restart

Navigating to your slice's IP will show the default "Welcome to nginx!' page.


To create any further virtual hosts all you need to do is create the main file in the 'sites-available' folder, create a symlink in the 'sites-enabled' folder and restart Nginx.

All very simple and very organised.


Mirroring the layout used when installing Nginx via the aptitude package manager allows for much easier administration of your virtual hosts — each vhost has its own file and can be switched on and off by a symlink to the sites-enabled folder.

The next article will look at configuring and optimising Nginx for use on your Slice.


Article Comments:

Andrea commented Mon Mar 30 23:56:12 UTC 2009:

In the article you are using the www-data user. Usually it's a user/group created by apache. Here we are dealing with nginx and the only user we created so far following your articles is "demo". Do I have to create the user/group www-data? How to setup it and permissions on the public_html folder and vhost subfolders?

Thanks, Andrea

Want to comment?

(not made public)


(use plain text or Markdown syntax)