Difference between revisions of "RPi Nginx Webserver"
(→Nginx Installation) |
(Added instructions for installing from wheezy-backports) |
||
(31 intermediate revisions by 3 users 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 | + | 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. | ||
− | + | == Installing Nginx Using the Nginx.org Debian Package == | |
− | + | The default Raspbian package is behind the latest stable release. So the following will enable the current version to be fetched instead, to get the latest features and bug fixes: | |
− | From your Pi's terminal, firstly we'll install the obsolete packaged version because it creates some useful | + | 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. | ||
+ | |||
+ | == Installing Nginx Using the Debian Wheezy Backports Package == | ||
+ | |||
+ | Using the Nginx.org repository may not work on the version of Raspbian you are using. The following will get the current version backported from a newer version of Debian: | ||
+ | |||
+ | cd | ||
+ | echo "deb http://ftp.debian.org/debian/ wheezy-backports main" > backports.list | ||
+ | echo "deb-src http://ftp.debian.org/debian/ wheezy-backports main" >> backports.list | ||
+ | sudo mv backports.list /etc/apt/sources.list.d | ||
+ | |||
+ | Add the backports public key so you don't get a warning when you run <code>sudo apt-get update</code>. | ||
+ | |||
+ | gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553 | ||
+ | gpg -a --export 8B48AD6246925553 | sudo apt-key add - | ||
+ | |||
+ | Update and install Nginx. | ||
+ | |||
+ | sudo apt-get update | ||
+ | sudo apt-get install -t wheezy-backports nginx | ||
+ | |||
+ | == Installing Nginx Using the Raspbian Package == | ||
+ | |||
+ | If you do not care about the latest stable release, the normal install can be used: | ||
+ | |||
+ | sudo apt-get install nginx | ||
+ | |||
+ | In the current Raspbian package, the default document root is not /var/www/ as indicated below, but in /usr/share/nginx/www | ||
+ | instead. This directory should exist with a default sample index.html page. You can verify the location using: | ||
+ | |||
+ | grep www /etc/nginx/sites-available/* | ||
+ | |||
+ | Copy your own html files to this directory, and test with curl or web browser as shown below. | ||
+ | |||
+ | == 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 104: | ||
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 114: | ||
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 76: | Line 131: | ||
For convenience, here are all the installation steps in one bunch you can copy and paste. | For convenience, here are all the installation steps in one bunch you can copy and paste. | ||
− | sudo apt-get install curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev | + | 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 | VERSION=1.2.2 | ||
curl http://nginx.org/download/nginx-$VERSION.tar.gz | tar zxvf - | curl http://nginx.org/download/nginx-$VERSION.tar.gz | tar zxvf - | ||
Line 95: | Line 153: | ||
== 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 | ||
− | sudo rm -rf /etc/nginx /etc/ | + | # 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 |
Latest revision as of 06:28, 4 February 2015
Contents
- 1 Introduction
- 2 Why Nginx?
- 3 Installing Nginx Using the Nginx.org Debian Package
- 4 Installing Nginx Using the Debian Wheezy Backports Package
- 5 Installing Nginx Using the Raspbian Package
- 6 Nginx Installation from Source Code
- 7 Using Nginx for Static Content
- 8 Nginx with PHP
- 9 Appendix 1: Install Script
- 10 Appendix 2: Uninstall
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 Nginx.org Debian Package
The default Raspbian package is behind the latest stable release. So the following will enable the current version to be fetched instead, to get the latest features and bug fixes:
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.
Installing Nginx Using the Debian Wheezy Backports Package
Using the Nginx.org repository may not work on the version of Raspbian you are using. The following will get the current version backported from a newer version of Debian:
cd echo "deb http://ftp.debian.org/debian/ wheezy-backports main" > backports.list echo "deb-src http://ftp.debian.org/debian/ wheezy-backports main" >> backports.list sudo mv backports.list /etc/apt/sources.list.d
Add the backports public key so you don't get a warning when you run sudo apt-get update
.
gpg --keyserver pgpkeys.mit.edu --recv-key 8B48AD6246925553 gpg -a --export 8B48AD6246925553 | sudo apt-key add -
Update and install Nginx.
sudo apt-get update sudo apt-get install -t wheezy-backports nginx
Installing Nginx Using the Raspbian Package
If you do not care about the latest stable release, the normal install can be used:
sudo apt-get install nginx
In the current Raspbian package, the default document root is not /var/www/ as indicated below, but in /usr/share/nginx/www instead. This directory should exist with a default sample index.html page. You can verify the location using:
grep www /etc/nginx/sites-available/*
Copy your own html files to this directory, and test with curl or web browser as shown below.
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