Build your own Raspberry Pi NGINX Web Server

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.

14 Comments

  1. Rizal M on

    thanks for sharing. I have trouble when configuring nginx. when installing php 7 with code:
    sudo apt-get install php7.0-fpm

    it returns this:
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.0-fpm
    E: Couldn’t find any package by glob ‘php7.0-fpm’
    E: Couldn’t find any package by regex ‘php7.0-fpm’

    and when i continue following your tutorial, until configuring index.php and try to open IP in browser, i got this error:
    502 bad gateway

    i use latest rapbian, version 10 (buster).

    1. Emmet on

      Hi Rizal,

      We have now updated the Nginx tutorial so it will now work with Raspbian Buster.

      Cheers,
      Emmet

  2. Dave Ingram on

    hostname -I gives me 127.0.0.1
    This is not correct!

    1. Gus on

      Hi Dave,

      Make sure you are using a capital ‘I’ when you use the command hostname -I.

      It may also indicate your Raspberry Pi isn’t connected to a network or isn’t being designated an IP address.

  3. Tim B on

    So once this is all setup, am I able to clone the MicroSD card to replicate the whole setup on another RPi?

    1. Gus on

      Hey Tim,

      Yes it should be possible.

      Cheers,
      Gus

  4. Erik van Elten on

    I installed nginx and everything worked fine. Built myself a website and was happy, until I needed some php code.

    So I installed php5 like described above. After restarting the webserver I can’t browse to the site anymore. Somehow my browser gets redirected to another internal IP, where nothing happens, of course.

    This is the message: 192.168.178.38 is unreachable.

    Where can I correct this?

    1. karan Sharma on

      Same problem occurs with me dude, i need an urgent solution …

    2. Gus on

      Running the following command should give you an insight into why Nginx is failing to start.

      sudo systemctl status nginx
  5. nuno on

    Hello,
    first of all thanks for your good tutorial.
    My comment is more of a request because I am no expert.
    If I am right, seems to me that the tutorial to install mysql and phpmyadmin is with apache and not nginx. Hope some day that would be inclueded.
    Thanks.

  6. Humberto on

    Good tutorial. I wait a tutorial with NGINX and Owncloud or Pydio, in order to replace Apache for personal Cloud task.

    1. Humberto on

      I add Nextcloud to my desire

  7. Bob Deloyd on

    I’m going to try this and then learn what to do with it.

  8. Jack on

    I like what you’ve done, but I do want to point out MySQL is not what it used to be. Since the transfer of Sun to Oracle, MySQL is no longer fully free. I would suggest using PostgeSQL as it doesn’t have the same licensing issues.

Leave a Reply

Your email address will not be published. Required fields are marked *