Erik Dahlstrand

Writing on web development and server management.

Install Nginx on Ubuntu

You can get the latest stable version of Nginx from the Nginx PPA on Launchpad. Just change lucid for the version you are using.

1
2
echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu lucid main" > /etc/apt/sources.list.d/nginx-stable-lucid.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C

Then run the following commands:

1
2
apt-get update
apt-get install nginx

Don’t forget to open firewall:

1
ufw allow 'Nginx HTTP'

Add Virtual Hosts

Create a config file in /etc/nginx/sites-available for each virtual host. In it’s most simple form:

/etc/nginx/sites-available/example_com
1
2
3
4
5
server {
  server_name     example.com www.example.com;    # domain(s) to match
  root            /home/deployer/example_com;     # web site root path
  access_log      off;                            # log path or off
}

Create a symbolic link in /etc/nginx/sites-enabled for each virtual host you want to enable.

1
2
cd /etc/nginx/sites-enabled
ln -s ../sites-available/example_com

Then restart Nginx service nginx restart.

Notes

Check config file(s) for errors

1
2
service nginx configtest             # or
nginx -t -c /etc/nginx/nginx.conf

Generate SSL certificate request

1
openssl req -new -newkey rsa:2048 -nodes -keyout domain.com.key -out domain.com.csr

Check if gzip files are served

1
2
3
4
5
# First, you need to detect PID of nginx worker process(es):
ps ax | grep nginx

# Now, we trace it:
strace -p 12345 2>&1 | grep gz

Comments