How to Set Up An nginx Server on a Raspberry Pi
14 Aug 2016First Off.
This tutorial is an update to an earlier article on how to install the latest nginx server on a Raspberry Piv3.
Install dependencies
Install the PCRE libraries to enable nginx’s rewrite modules. Also install SSL library to support HTTPS configurations:
Download source and compile
Always check the latest source version by going to the nginx download page. I’m grabbing the latest Mainline version which includes HTTP2 support. This will be fun to play around later.
Let’s pass along a fairly basic nginx configuration:
Then issue a make and make install:
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!