Difference between revisions of "RPi Nginx Webserver"

From eLinux.org
Jump to: navigation, search
(Appendix 2: Uninstall)
(Why Nginx?: Apache is no longer web server software, but an organization. Clarify/correct.)
(27 intermediate revisions by one other user not shown)
Line 8: Line 8:
 
== Why Nginx? ==
 
== Why Nginx? ==
  
By far the most of the world's websites are hosted on [http://httpd.apache.org/ 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.
+
By far the most of the world's websites are hosted on [http://httpd.apache.org/ Apache's] httpd. If your Pi runs Debian, you ''could'' install httpd 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 httpd 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 [http://www.lighttpd.net/ Lighttpd], but a far more widely-used server is '''[http://nginx.com/ Nginx]''' ([http://nginx.org/ 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.
 
However, there are several good light-weight alternatives: [[RPi webserver]] covers installing [http://www.lighttpd.net/ Lighttpd], but a far more widely-used server is '''[http://nginx.com/ Nginx]''' ([http://nginx.org/ 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.
+
== Installing Nginx Using the Debian Package ==
  
== Nginx Installation ==
+
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:
  
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.
+
cd
 +
echo "deb http://nginx.org/packages/debian/ squeeze nginx" >nginx.list
 +
echo "deb-src http://nginx.org/packages/debian/ squeeze nginx" >> nginx.list
 +
sudo mv nginx.list /etc/apt/sources.list.d
 +
sudo apt-get update
 +
sudo apt-get install nginx
 +
 
 +
This raises a warning about 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 Source Code ==
 +
 
 +
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. (If you did the package installation in the preceding section, you won't need to do this again. Just remove /etc/apt/sources.list.d/nginx.list to clean up, then <code>sudo apt-get update</code>.)
  
 
  sudo apt-get -y install nginx
 
  sudo apt-get -y install nginx
 
  sudo apt-get -y remove nginx
 
  sudo apt-get -y remove nginx
 +
sudo apt-get clean
  
 
Now grab the necessary Debian compilation utils and libraries.
 
Now grab the necessary Debian compilation utils and libraries.
Line 60: Line 72:
 
  sudo service nginx start
 
  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).
+
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 ==
 
== Using Nginx for Static Content ==
Line 66: Line 82:
 
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.
 
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 [http://wiki.nginx.org/Configuration described here]. After editing the config files, give the server a poke using
+
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 [http://wiki.nginx.org/Configuration described here]. After editing the config files, always give the server a poke using
  
 
  sudo service nginx reload
 
  sudo service nginx reload
  
 
== Nginx with PHP ==
 
== 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 [http://wiki.nginx.org/PHPFcgiExample described here].
  
 
== Appendix 1: Install Script ==
 
== Appendix 1: Install Script ==
Line 78: Line 101:
 
  sudo apt-get -y install nginx
 
  sudo apt-get -y install nginx
 
  sudo apt-get -y remove 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
 
  sudo apt-get -y install curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev
 
  VERSION=1.2.2
 
  VERSION=1.2.2
Line 97: Line 121:
 
== Appendix 2: Uninstall ==
 
== Appendix 2: Uninstall ==
  
Beware! This will brutally delete Nginx, all the configuration files and all the static content.
+
'''Beware!''' This will brutally delete Nginx, all the configuration files and all the static content.
  
 
  sudo service nginx stop
 
  sudo service nginx stop
 
+
# Our compiled-from-source files:
Our compiled-from-source files:
+
  sudo rm -rf /etc/nginx /etc/default/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/nginx /usr/sbin/nginx* /usr/local/nginx /var/run/nginx.pid /var/log/nginx
+
  sudo rm -rf /etc/init.d/nginx /etc/logrotate.d/nginx /var/lib/update-rc.d/nginx
 
+
# Your static content:
The Debian scripts that came with the apt-get version:
+
sudo rm -rf /var/www
 
 
  sudo rm -rf /etc/init.d/nginx /usr/sbin/nginx* /etc/logrotate.d/nginx /var/lib/update-rc.d/nginx
 

Revision as of 20:11, 1 February 2013

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's httpd. If your Pi runs Debian, you could install httpd 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 httpd 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 Nginx Using 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:

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

This raises a warning about 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 Source Code

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. (If you did the package installation in the preceding section, you won't need to do this again. Just remove /etc/apt/sources.list.d/nginx.list to clean up, then sudo apt-get update.)

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 /etc/default/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