.

Set Up Unmanaged VPS (4 Newbies) – Part 11: Nginx (better than Apache) Web Server

English   English (change)

Set Up Unmanaged VPS (4 Newbies) – Part 11: Nginx (better than Apache) Web Server

Install Nginx - Nginx logo

Installing & configuring Nginx web server, tweaking the default file structure, then setting up a vhost file with symlink is the subject of this copy/paste how-to. So open up that CLI and I’ll explain.

So we’re ready to install a star attraction, Nginx (pronounced “engine-x”). But why? Isn’t Apache the best web server?

Apache has served me well for years, both locally and on remote hosts, and is still a desirable choice for many large sites.

But, kinda like Ubuntu, FireFox and jQuery, there’s a new kid in town, and it’s leaner, simpler to configure and use, and faster to serve. Enter the Nginx web server.

[sniplet guvSellBox]
[sniplet vpsIndexSell]
[sniplet video]

Why Choose Nginx? Why not Apache or Lighttpd?

I don’t profess to be an expert, just someone who has researched ultra-thoroughly. I concentrated primarily on the two most popular, best established web servers, Apache and Lighttpd, and the young Turk alternative, Nginx, about which geekdom is all aflutter. Here’s a summary of my key findings:-

  • Apache is bloatware, loading unused modules that waste resources
  • Lighttpd leaks RAM badly
  • Nginx benchmarks the fastest, using the least resources

I’ve been using this light-weight server for my resource-heavy WordPress blog, guvnr.com, and am impressed with its solid performance. Also importantly, it doesn’t have that Windows-like tendency, an Apache affliction too, of wasting resources by running a bunch of services that I just don’t need.

Don’t take my word for it. You shouldn’t, because I haven’t performed any benchmark tests. Google something like Apache vs Nginx” or Nginx vs Lighttpd and have a read. And here’s the Nginx wiki.

Getting the Latest Nginx Version

There are two ways to install software on Linux; using the integrated installation tools or from sourcecode.

Normally, we install internally, using something like the Linux installer ‘aptitude’, but because this is such an important component of our VPS, I’m going to show you how to install from source. This method takes a little longer, but it’s worth it because we’ll have a much more up-to-date version.

.. With a web server, we shouldn’t cut corners. That would be like buying a yellow Ferrari.

First up, we need some dependency files:-

[text]sudo aptitude -y install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev[/text]

And to create a directory in which to store the Nginx package:-

[text]mkdir ~/sources[/text]

Change to that directory:-

[text]cd ~/sources/[/text]

Now we get the latest stable Nginx release, nginx-0.7.64 *.

* As of 20th January 2010, edited by the_guv. You should still check it is still the latest, here, and amend the filename accordingly:-

[text]wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz[/text]

Unzip it:-

[text]tar -zxvf nginx-0.7.64.tar.gz[/text]

Go into the new unzipped folder:-

[text]cd nginx-0.7.64[/text]

Installing and Testing Nginx

Compile with two options; where to install it, and including ‘ssl’ (to enable ‘https’ for secure connections, ie shopping and stuff):-

[text]./configure –sbin-path=/usr/local/sbin –with-http_ssl_module[/text]

Install this baby:-

[text]make
sudo make install[/text]

Kick it up:-

[text]sudo /usr/local/sbin/nginx[/text]

And test it by popping your IP address in a web browser. You should see “Welcome to nginx!”

Now stop it:-

[text]sudo kill `cat /usr/local/nginx/logs/nginx.pid`[/text]

Have Nginx Start, Restart or Stop When Required

This is important, for example, upon reboot. We need a script for this. Create a file:-

[text]sudo nano /etc/init.d/nginx[/text]

And paste this within:-

[text]
#! /bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

case “$1″ in
start)
echo -n “Starting $DESC: ”
start-stop-daemon –start –quiet –pidfile /usr/local/nginx/logs/$NAME.pid
–exec $DAEMON — $DAEMON_OPTS
echo “$NAME.”
;;
stop)
echo -n “Stopping $DESC: ”
start-stop-daemon –stop –quiet –pidfile /usr/local/nginx/logs/$NAME.pid
–exec $DAEMON
echo “$NAME.”
;;

restart|force-reload)
echo -n “Restarting $DESC: ”
start-stop-daemon –stop –quiet –pidfile
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON
sleep 1
start-stop-daemon –start –quiet –pidfile
/usr/local/nginx/logs/$NAME.pid –exec $DAEMON — $DAEMON_OPTS
echo “$NAME.”
;;
reload)
echo -n “Reloading $DESC configuration: ”
start-stop-daemon –stop –signal HUP –quiet –pidfile /usr/local/nginx/logs/$NAME.pid
–exec $DAEMON
echo “$NAME.”
;;
*)
N=/etc/init.d/$NAME
echo “Usage: $N {start|stop|restart|reload|force-reload}” >&2
exit 1
;;
esac

exit 0
[/text]

Then give the file permissions and make the script run on reboot, else start/stop/restart when required:-

[text]sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults[/text]

nginx.conf – Configuring Nginx

Now open the Nginx configuration file:-

[text]sudo nano /usr/local/nginx/conf/nginx.conf[/text]

…and strip out all the content, delete the lot. CTRL-K is the easy way to do that, if you were wondering.

And replace with this:-

[text]
user www-data www-data;
worker_processes 4;

events {
worker_connections 1024;
}

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

sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 5;

gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

include /usr/local/nginx/sites-enabled/*;
}
[/text]

Creating the Virtual Host File Structure & Symlinks

The Nginx file structure is pretty messy for multiple sites, so we’ll sort that.

First, layout some new folders:-

[text]sudo mkdir /usr/local/nginx/sites-available
sudo mkdir /usr/local/nginx/sites-enabled[/text]

…the first is for our virtual host (vhost) files, the second for their corresponding symlinks which will be referenced by Nginx’ config file.

What are vhosts & symlinks?

You have one of each per domain, and one of each for the default settings.

The symlink, or symbolic link, references the web server to the virtual host file.

The vhost file is a configuration file. It tells the web server, for example, things like where the web files live or the kind of URI structure you want.

For now, we need a default vhost file, and that goes in the sites-available folder. So:-

[text]sudo nano /usr/local/nginx/sites-available/default[/text]

Now paste this:-

[text]
server {
listen 80;
server_name localhost;

location / {
root html;
index index.php 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;
}
}
[/text]

And enable it with this symlink:-

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

Boot it up again:-

[text]sudo /etc/init.d/nginx start[/text]

…and check for that “Welcome…” page again, using your IP in a web browser.

Splendid. All pretty. And pretty well organised.

Moving Along

So that’s Nginx up and running.

In Part 12 of this series Set Up an Unmanaged VPS (4 Newbies) I’m taking a quick detour, setting up FileZilla so we’ve got a Secure FTP (SFTP) connection. That’ll be handy to help demonstrate Part 13, when we create another folder structure, this time for our sites and blogs, and pop up a couple of test pages.

Then, in Part 13, I’ll show you how to use Subversion to more easily install and upgrade platforms and their modules/plugins. I’ll example the popular WordPress scenario – and while we’re about it we’ll sort out WordPress caching and friendly-URLs.

And then, this, that, the other. Cue index ..

[sniplet vpsIndex]

.