How to Set Up An nginx Server on a Raspberry Pi

First 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:

sudo apt-get update;
sudo apt-get install libpcre3 libpcre3-dev libssl-dev;

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.

cd ~;
wget https://nginx.org/download/nginx-1.11.3.tar.gz;
tar -xvf nginx-1.*.tar.gz;
cd nginx-1.*;

Let’s pass along a fairly basic nginx configuration:

./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\
    --with-http_gunzip_module \
    --with-http_v2_module;

Then issue a make and make install:

make;
sudo make install;
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:

New nginx screen

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!