In this tutorial, we will be showing you how to set up the NGINX Web Server on your Raspberry Pi.

Raspberry Pi NGINX Header

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 significant 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, 3, or 4

Micro SD Card

Power Supply

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 remove it from 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, let’s 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, type the following command into terminal to start up 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, let’s open the address within any web browser. Just go to the 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 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 set up 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 standard PHP due to the way NGINX works.

2. Before we get started with configuring PHP for NGINX, we need to go ahead and install PHP 7.3 it and some recommended PHP modules that will make it easier when dealing with more extensive PHP scripts.

Before you run the command below, make sure that you are running Raspbian Buster or newer. You can learn how to upgrade from Raspbian Stretch to Buster in our guide.

You can install everything by running the command below.

sudo apt-get install php7.3-fpm php7.3-mbstring php7.3-mysql php7.3-curl php7.3-gd php7.3-curl php7.3-zip php7.3-xml -y

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 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, and this tells NGINX to recognize the index.php file as a possible index, adding it first in the list means it will be selected over an 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

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

This code 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 an MYSQL database to go with it. It’s pretty straightforward and perfect for anyone who needs a good database software.

Hopefully, by now you have successfully set up your Raspberry Pi with a fully functional NGINX web server with PHP working correctly.

If you have run into any issues with this Raspberry Pi Nginx tutorial or just want to post some feedback, then feel free to drop a comment below.

Ultimate Books of Pi Bottom
Get projects delivered to your inbox weekly.