RPi Nginx Webserver

From eLinux.org
Revision as of 12:37, 4 July 2012 by Rickb (talk | contribs) (Nginx Installation)
Jump to: navigation, search

Introduction

Using the Pi as a webserver is easy. There is more than enough grunt to run a small web server supported by PHP and mysql.

This HOWTO is based on the Debian build.

Why Nginx?

By far the most of the world's websites are hosted on Apache. If your Pi runs Debian, you could install Apache just using the normal "sudo apt-get install apache2", and the hard work would be done for you. However, the result may be less than satisfactory because Apache is intended to run on full-sized servers and is a bit of a hog on resources.

However, there are several good light-weight alternatives: RPi webserver covers installing Lighttpd, but a far more widely-used server is Nginx (additional resources). You can find plenty of resources comparing these two and others so I won't repeat the factors here to help you decide.

But there is a small hitch with installing Nginx: unfortunately, the Debian repositories only hold an old version of it. Fortunately, this isn't a problem because building it from source is easy. And here's how.

Nginx Installation

From your Pi's terminal, firstly grab the necessary Debian utils and libraries.

sudo apt-get install curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev

Now get the latest Nginx source code. This was v1.2.2 at the time of writing, but there will be more versions along later.

VERSION=1.2.2
curl http://nginx.org/download/nginx-$VERSION.tar.gz | tar zxvf -

Here's the bit where we build it using the traditional "./configure && make" procedure. It takes a few minutes.

cd nginx-$VERSION
./configure --sbin-path=/usr/sbin/nginx \
            --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 \
            --without-http_proxy_module
make

Note that those end of line back-slashes are necessary and mustn't be followed by any space characters. The first five options make Nginx play nicely with Debian. The other two options give you a typical build, including encryption (ssl) support but not proxying. The complete set of options for ./configure are here. You could save some Pi resources by

  • missing off '--with-http_ssl_module' if you don't want to support https encryption;
  • adding '--without-http_gzip_module' if you prefer to serve uncompressed text files - this might make it faster or slower and you'd have to experiment.

Now it's built, you have to be root to install your shiny new webserver, like this:

sudo make install

and you'll need some sample content perhaps:

sudo mkdir -p /var/www
sudo cp html/* /var/www

Finally, start the server:

sudo service nginx start

If this succeeds, everything went according to plan. You can point your web browser at your Pi (http://mypi.local/ or http://192.168.1.10/ or whatever its name is).

Using Nginx for Static Content

Nginx with PHP