Raspberry Pi NGINX Header In this tutorial we will be showing you how to setup the NGINX Web Server on your Raspberry Pi. It is a better alternative to Apache for the Raspberry Pi due to a few different reasons.

Some of the reasons NGINX is better on the Raspberry Pi is that it has a much lower memory usage and generally a lower CPU usage meaning you can get more out of your Raspberry Pi’s limited resources.

We will also show you how to configure NGINX to utilize PHP-FPM, this requires a slight bit of extra configuration to get up and running unlike PHP for Apache.

While NGINX has a bit more leg room then Apache, still don’t expect it to run any serious amount of traffic or perform any serious amount of PHP work.

Equipment List

Below are all the bits and pieces that I used for this Raspberry Pi NGINX tutorial.

Recommended:

Raspberry Pi 2 or 3

Micro SD Card

Ethernet Cord or Wifi dongle (The Pi 3 has WiFi inbuilt)

Optional:

Raspberry Pi Case

Setting up NGINX on the Raspberry Pi

1. Before we get started with setting up the NGINX web server on the Raspberry Pi we must first make sure our Raspberry Pi is up to date by running the following two commands on it:

sudo apt-get update
sudo apt-get upgrade

2. We should also run the following command to uninstall Apache2 since there is a chance that it is pre-installed on your system. Failing to do so can cause the installation to fail since it automatically starts up and utilizes port 80, since we intend on using NGINX as a web server we choose to just remove it off the system. You can skip this step if you are certain Apache2 isn’t already installed on your Raspberry Pi.

sudo apt-get remove apache2

3. With the packages now up to date and Apache 2 removed we can proceed on with the tutorial. Finally lets install NGINX onto our Raspberry Pi by running the following command on your Raspberry Pi:

sudo apt-get install nginx

4. Now with NGINX installed we can now start up the software, just type the following command into terminal to startup the web server on your Raspberry Pi:

sudo systemctl start nginx

5. With the NGINX web server now started up we should now grab our local ip address. We do this so we can test whether our web server is working from a separate web browser. Utilize the following command to grab your Raspberry Pi’s local IP address.

hostname -I

6. Now that we have our Raspberry Pi’s local IP address handy, lets open the address within any web browser. Just go to your local IP Address that you grabbed using hostname -I. In my case I would go to http://192.168.0.143.

Once you browse to the address you should see something like below, don’t be worried if this shows an Apache page instead as sometimes NGINX doesn’t overwrite the Apache default index page.

NGINX Default Screen

Configuring NGINX for PHP

1. Unlike Apache, NGINX won’t be automatically setup for use with PHP. We instead must make changes to its configuration files to get it to load in. We will also have to utilize PHP-FPM and not normal PHP due to the way NGINX works.

2. Before we get started with configuring PHP for NGINX we need to actually install it. You have the choice between PHP 5, or PHP 7. We personally recommend PHP 7 as PHP 5 is getting close to end of life.

PHP 5

2a. If you have decided to just use PHP 5 then you can simply type the following command into your Raspberry Pi’s terminal.

sudo apt-get install php5-fpm

Installing PHP 7

2b. PHP 7 is a bit trickier to install than PHP 5 for those are on Raspbian Jessie and earlier. Those who have updated to Raspbian Stretch will have it easy, in fact we recommend people upgrade to Raspbian Stretch, you can follow our guide on upgrading from Raspbian Jessie to Stretch.

Raspbian Stretch and later

sudo apt-get install php7.0-fpm

Raspbian Jessie and earlier

For Raspbian Jessie and earlier we need to grab the php7.0 packages from the Stretch branch, to be able to do this we will be adding Stretch as a possible repository, but setting it up so it will only grab a package from it when we specifically specify it.

a. Begin by editing the apt sources list file by running the following command.

sudo nano /etc/apt/sources.list

b. Add the following line to the bottom of this file.

deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi

Once done, we can save & exit by pressing CTRL + X and then pressing Y and lastlyEnter.

c. Now we need to update our preferences file to ensure jessie will be selected over Stretch. Run the following command to begin editing the file.

sudo nano /etc/apt/preferences

d. To this, add the following lines, this basically sets the packages to effect and the priority.

Package: *
Pin: release n=jessie
Pin-Priority: 600

Once you’re finished, save & exit by pressing CTRL + X then Y and finally Enter.

e. With the preference file now changed and now that we have added the new repository to our sources list, we need to run the apt-get update command to grab the new repository.

sudo apt-get update

f. Finally we can install PHP7.0-FPM by using the following command on your Raspberry Pi.

sudo apt-get install -t stretch php7.0-fpm

Modifications to the NGINX Config File

3. With PHP-FPM now installed we can make the required modifications to the default NGINX configuration file. To begin editing this file run the following command on your Raspberry Pi:

sudo nano /etc/nginx/sites-enabled/default

4. Within this file, find and replace/add the following lines.

Find

index index.html index.htm;

Replace With

index index.php index.html index.htm;

Here we need to add index.php to the index line, this basically tells NGINX to recognise the index.php file as a possible index, adding it first in the list means it will be selected over a index.html file.

Find

#location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #}

Replace With for PHP 5

location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               # With php5-fpm:
               fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

Replace With for PHP 7

location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

This basically sets up NGINX to process .php files by passing them through to PHP-FPM.
Once done, you’re able to save & exit by pressing CTRL +X and then pressing y and then enter.

5. Next in this Raspberry Pi Nginx server tutorial we will need to tell NGINX to reload its configuration by running the following command.

sudo systemctl reload nginx

6. Finally let’s test the PHP setup by writing a very simple index.php file in our /var/www/html directory. Run the following command to create and begin editing our index.php file.

sudo nano /var/www/html/index.php

7. To this file, add the following line of code.

<?php phpinfo(); ?>

Once that is all done, we can save & exit by pressing CTRL + X then Y and lastly Enter.

8. Now like earlier in the tutorial you can go to your Raspberry Pi’s IP address, this time however you should see a page showing all the information about your version of PHP and what modules are currently active. It is a good indication that your PHP installation is up and running correctly.

If you want to go one step further then you can look at setting up a MYSQL database to go with it. It’s pretty straight forward and perfect for anyone who needs a good database software.

Hopefully by now you have successfully setup your Raspberry Pi with a fully functional NGINX web server with PHP working correctly. If you have ran into any issues with this Raspberry Pi Nginx tutorial or just want to post some feedback feel free to drop a comment below.

The Raspberry Pi Crash Course

The Raspberry Pi Crash Course

Enter your email address below to get the

Raspberry Pi crash course delivered straight to your inbox

Please check your inbox for a confirmation email!