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:
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:
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.
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:
Then we compile the source and move the files to their final locations:
We then add an nginx user for the binary to run under if we ever decide to run it by it’s lonesome:
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:
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:
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:
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.
Look for the following configuration under the http { } block:
Now change that to:
We will need to reload nginx for the changes to take effect:
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!