Setting up nginx on Debian Lenny

In this article i will i will focus on how to install, configure and setup Nginx. Nginx is a popular web server, load balancer and reverse caching proxy for many high traffic sites. Personally I prefer to use Apache2 with proper configuration and Varnish as frontend. In some cases you might want to use Nginx as frontend for compressing data after it has been received from the backend server, either to ease the load on the backend servers or because the backeds does not support this feature, as is the case with Varnish.

Please note that this is my first encounter with Nginx, so bare with me if I do some weird stuff. I try to document whatever I am doing, mostly to educate my self. You should not use any configuration found in this article in a production environment if you are not one hundred percent clear what it does and why it does it.

In this article we’ll install nginx from source as the latest stable release in Debian Lenny is very old. Please note that this might not be the case with Squeeze/SID, but in my articles I focus on using the latest stable release of Debian.

apt-get install build-essential libpcre3-dev libpcre3 libssl-dev zlib1g-dev

This will install all essential build utilities that you will need to build a program from source. In addition to this we install some libraries to support SSL and PECR to support rewrites.

cd /root
wget http://nginx.org/download/nginx-0.7.65.tar.gz
tar -zxvf nginx-0.7.65.tar.gz
cd nginx-0.7.65

Please go to nginx.org and fetch the latest stable version of Nginx. As of writing this 0.7.65 is the latest stable release. We download it, untar it and move in to the folder. You probably knew this ;-) (4)

./configure --prefix=/usr/local /
--sbin-path=/usr/local/sbin /
--conf-path=/etc/nginx/nginx.conf /
--pid-path=/var/run/nginx.pid /
--error-log-path=/var/log/nginx/error.log /
--http-log-path=/var/log/nginx/access.log /
--with-http_ssl_module /
--with-debug

This tells the compiler where to put stuff and what modules we want to configure it with or without. Please refer to nginx wiki if you want more prefixes. If you encounter any errors, read them and install whatever it is missing. Use some good old fashion Google-Fu! And please let me know in a comment if I made a Boo-Boo. (3)

make; make install

This uses the information given to it when we configured it and installs nginx. Normally this should go by without any errors as the step above should have found any short comings.

cd /etc/init.d/
wget http://static.vvvegard.net/files/nginx/nginx
chmod +x  nginx

This is a modified version of the startup script to comply with the special configuration we use. The original script can be found at articles.slicehost.com. With this startup script you should be able to start, stop, restart or just reload it. I recommend you to visit the site that i snagged this startup script from, they have a lot of good articles on nginx and hosting in general! (2)

/etc/init.d/nginx start

This starts nginx and you should now be able to access it via your favorite browser.

Server: nginx/0.7.65

This is the header that nginx puts out, and this verifies that nginx is actually serving us pages. I prefer to use Live HTTP Headers add-on for Firefox to inspect HTTP Headers.

cd /etc/nginx
mv nginx.conf old_nginx.conf
wget http://static.vvvegard.net/files/nginx/nginx.conf

This replaces the standard configuration to a modified version that comply with the standards of Debian. This is fairly basic and should work in most cases. (1)

mkdir sites-enabled/
cd sites-enabled/
wget http://static.vvvegard.net/files/nginx/000-default

Again, to comply with Debian standards we store all of our virtual hosts in it’s own folder and instead of including everyone manually we include everything in that folder with wildcard. (1)

/etc/init.d/nginx restart

Now this won’t do much, it will basically just serve static files located in “/var/www”. If we’d want PHP you’d have to either do it via fastcgi or use nginx as a frontend for Apache2, primarily for caching and compressing and let Apache2 handle all dynamic data.

Resources:
(1) http://www.ubuntugeek.com/using-nginx-as-a-reverse-proxy-to-get-the-most-out-of-your-vps.html
(2) http://articles.slicehost.com/2007/10/19/debian-etch-installing-nginx
(3) http://wiki.nginx.org/Main
(4) http://nginx.org/

Leave a Reply