Difference between revisions of "RPi Nginx Webserver"

From eLinux.org
Jump to: navigation, search
(Why Nginx?)
m (Installing the Debian Package)
Line 14: Line 14:
 
== Installing the Debian Package ==
 
== Installing the Debian Package ==
  
The default Debian package is the wrong one - it's v0.7, way behind the latest stable release. So this will enable the latest (currently v1.2.2) version to be fetched instead:
+
The default Debian package is the wrong one - it's v0.7, way behind the latest stable release. So the following will enable the latest (currently v1.2.2) version to be fetched instead:
  
 
  sudo -s
 
  sudo -s
  cd /etc/apt/sources.d
+
  cd /etc/apt/sources.list.d
 
  echo "deb http://nginx.org/packages/debian/ squeeze nginx" >nginx.list
 
  echo "deb http://nginx.org/packages/debian/ squeeze nginx" >nginx.list
 
  echo "deb-src http://nginx.org/packages/debian/ squeeze nginx" >> nginx.list
 
  echo "deb-src http://nginx.org/packages/debian/ squeeze nginx" >> nginx.list

Revision as of 14:57, 4 July 2012

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.

Installing the Debian Package

The default Debian package is the wrong one - it's v0.7, way behind the latest stable release. So the following will enable the latest (currently v1.2.2) version to be fetched instead:

sudo -s
cd /etc/apt/sources.list.d
echo "deb http://nginx.org/packages/debian/ squeeze nginx" >nginx.list
echo "deb-src http://nginx.org/packages/debian/ squeeze nginx" >> nginx.list
sudo apt-get update
sudo apt-get install nginx

This raises a problem with un-authenticated packages - you can either accept this warning, or if you prefer install from the source code instead, as described next.

Nginx Installation

From your Pi's terminal, firstly we'll install the obsolete packaged version because it creates some useful startup scripts for us, then we'll remove it again.

sudo apt-get -y install nginx
sudo apt-get -y remove nginx
sudo apt-get clean

Now grab the necessary Debian compilation utils and libraries.

sudo apt-get -y 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 took about ten minutes on my Pi.

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, which is unusual and might actually make it slower, so 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
sudo mkdir -p /var/www

and you'll need some sample content perhaps:

sudo cp html/* /var/www

Finally, start the server:

sudo service nginx start

If this succeeds, everything went according to plan. You can test it using

curl -s http://localhost/

or 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

The 'standard' place for your website content is in /var/www. This is the default place that was set up for you by the steps given above. If you want to serve static web content only (no PHP or whatever) then you're good to go.

You might like to go further and create virtual hosts or whatever - the configuration files you will need to edit are in /etc/nginx and how to write them is described here. After editing the config files, always give the server a poke using

sudo service nginx reload

Nginx with PHP

This is another download of ten minutes or so.

sudo apt-get install mysql-server php5-cgi php5-mysql

Note: During the install you will be asked for a password for accessing MySql with. This will be for root access so choose something you will not forget: you will need it again at some point!

Configure Nginx and PHP to work together as described here.

Appendix 1: Install Script

For convenience, here are all the installation steps in one bunch you can copy and paste.

sudo apt-get -y install nginx
sudo apt-get -y remove nginx
sudo apt-get clean
sudo apt-get -y install curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev
VERSION=1.2.2
curl http://nginx.org/download/nginx-$VERSION.tar.gz | tar zxvf -
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
sudo make install
sudo mkdir -p /var/www
sudo cp html/* /var/www
sudo service nginx start

Appendix 2: Uninstall

Beware! This will brutally delete Nginx, all the configuration files and all the static content.

sudo service nginx stop
# Our compiled-from-source files:
sudo rm -rf /etc/nginx /usr/sbin/nginx* /usr/local/nginx /var/run/nginx.pid /var/log/nginx
# The Debian scripts that came with the apt-get version:
sudo rm -rf /etc/init.d/nginx /etc/logrotate.d/nginx /var/lib/update-rc.d/nginx
# Your static content:
sudo rm -rf /var/www