RPi Nginx Webserver
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.
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.
From your Pi's terminal, firstly we'll install the obsolete packaged version because it creates some useful starup scripts for us, then we'll remove it again.
sudo apt-get -y install nginx sudo apt-get -y remove nginx sudo rm -rf /etc/nginx
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 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, give the server a poke using
sudo service nginx reload
Nginx with PHP
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 rm -rf /etc/nginx 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 /usr/sbin/nginx* /etc/logrotate.d/nginx /var/lib/update-rc.d/nginx