How to set up an nginx server on a Raspberry Pi
04 Aug 2015** This article has been updated new article **
Why nginx?
Using a Raspberry Pi as webserver is a no brainer. On that list of no brainers is using the lighter-weight webserver nginx solution. The binaries circulating through apt-get though are often out of date. So we will compile our own from source.
Download the PCRE library
One of the requirements for nginx needed for http_rewrites is the PCRE library. So we will need that dependency installed first:
sudo apt-get update;
sudo apt-get install libpcre3 libpcre3-dev;
Download the SSL library
The other requirement will be OpenSSL. In the future I plan on configuring my Raspberry Pi nginx server to use HTTPS:
sudo apt-get update;
sudo apt-get install libssl-dev;
Get the source code
We will need to download the nginx source code. At the time of this writing the latest release is 1.9.7. You can grab the latest by navigating to the nginx download page.
cd ~;
wget http://nginx.org/download/nginx-1.9.7.tar.gz;
tar -xvf nginx-1.9.*.tar.gz;
cd nginx*;
We just downloaded the nginx source code, untar and changed into the directory.
nginx configuration
We should issue the ./configure command with a certain set of configuration options. You can issue a ./configure –help command to see a list of available options. We will setting up bare essential here with the following ./configure command:
./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module
Then we compile the source and move the files to their final locations:
make;
sudo make install;
We then add an nginx user for the binary to run under if we ever decide to run it by it’s lonesome:
sudo useradd -r nginx;
Set up init scripts
Since we compiled from source, we don’t have the luxury of some awesome upstart scripts. We will need to scrounge around for our own. I modified the upstart script I found on nginx wiki page:
cd /etc/init.d/
sudo wget https://gist.githubusercontent.com/spfaffly/0470343bc8d5e884392a/raw/c4a60c998059b866d923fbeff20cd0d82dcc15fa/nginx -O /etc/init.d/nginx;
sudo chmod +x /etc/init.d/nginx;
We just downloaded our modified upstart script and made it executable. We should now be able to issue service commands.
Automatically start nginx on boot
This is an easy one. We just issue the following command to make sure the nginx webserver is always started on bootup:
sudo update-rc.d -f nginx defaults;
Our first webroot
Made it this far? Well now we’re at the more confusing part: setting up our web root. By default nginx puts the webroot in a weird spot. We will change this by making our own webroot and index file:
sudo mkdir /var/www/;
sudo wget https://gist.githubusercontent.com/spfaffly/d774f87b8cf9a1837d05/raw/2223babec34cb3e1732031556549eb307024572f/index.html -O /var/www/index.html
sudo chmod -R 775 /var/www/;
sudo chgrp -R nginx /var/www/;
We added a more standard webroot directory, added a kickass Raspberry Pi index page and applied the appropriate permissions. Even though the daemon runs as root
Adding the webroot to the default server
We will now modify the nginx configuration file to use our new webroot directory.
sudo nano /etc/nginx/nginx.conf;
Look for the following configuration under the http { } block:
location / {
root html;
index index.html index.htm;
}
Now change that to:
location / {
root /var/www/;
index index.html index.htm;
}
We will need to reload nginx for the changes to take effect:
sudo service nginx reload;
Viewing our webpage
So how do we access our webpage on our webserver? We will need to navigate to the IP of our Raspberry Pi. I run a quick ifconfig eth0 to find out my IP address and then navigate to it from another computer on the same network. What do I see?
Something awesome:
Have any questions?
If you run into any issues - post below and leave a question and I’ll make sure to update the article for any specific use-cases!