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

Optional

This tutorial was last tested using the latest version of Raspberry Pi OS Bullseye running on a Pi 400.

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 update
sudo apt 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 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 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 hostname 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 obtained using hostname -I. In my case, I would go to http://192.168.0.143.

http://YOUR PI's IP ADDRESS

7. Once you browse to the address you should see something like below.

Do not be worried if this shows an Apache page as sometimes NGINX does not 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 it and some recommended PHP modules that will make it easier when dealing with more extensive PHP scripts.

You can install everything by running the command below.

sudo apt install php8.2-fpm php8.2-mbstring php8.2-mysql php8.2-curl php8.2-gd php8.2-curl php8.2-zip php8.2-xml -y

If you run into a “package not found” error, then this version of PHP isn’t available on your current version of Raspberry Pi OS. However, you can work around this error by adding a third-party PHP repository to your operating system.

3. With PHP-FPM now installed we can make the required modifications to the default NGINX configuration file.

To begin editing the default configuration 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/php8.2-fpm.sock;
        }

This code sets up NGINX to process .php files by passing them through to PHP-FPM.

Once done, you are able to save and 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 and 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 is 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.

29 Comments

  1. Avatar for Martin
    Martin on

    Thank you so much, great guide.

  2. Avatar for JM
    JM on

    Thanks for the tutorial! (2/26/23) For others using this, may find lighttpd daemon on your pi rather than apache, which is already bound to port 0.0.0.0:80. `sudo systemctl stop lighttpd`

  3. Avatar for Lukas
    Lukas on

    Hi. I have followed the tutorial and love it! However, I can’t log in to Bitwarden as it says failed to fetch? I use self signed certificate if that is any help. Cheers

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Lukas,

      Is this through the web interface or through one of the many apps?

      Cheers,
      Emmet

    2. Avatar for Lukas
      Lukas on

      This is through the official Bitwarden widows app.

    3. Avatar for Emmet
      Emmet on
      Editor

      Hi Lukas

      It is highly likely that the Bitwarden windows app is enforcing SSL cert verification.

      The only ways to work around this is to add the custom cert to Windows, or by getting a domain name and getting a free signed SSL cert using letsencrypt.

      Cheers,
      Emmet

  4. Avatar for DaveP
    DaveP on

    Excellent, worked flawlessly for me to install nginx and php on my raspberry pi 400. After experimenting, I’ve found php commands are interpretted correctly from .php files but not from .html files. Previously I’ve had a configuration that sent .html files for php preprocessing – would I need to edit something in the php configuration files or in nginx file to get .html files preprocessed with php? Thanks again, very clear and thorough.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi DaveP,

      Typically PHP should only really be configured to handle processing of “.php” files. HTML can be written within these .php files and will be handled accordingly.

      However, if you have a particular reason why you would want your “.html” files to also be processed by PHP then it requires modification of your NGINX config file.

      Within the config file find the following configuration block.

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

      Replace that with the following.

      location ~ \.(php|html)$ {
                     include snippets/fastcgi-php.conf;
                     fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
              }

      Basically, what we are changing here is the regex that selected “.php” so that it will pick both “.php” and “.html“.

      Hopefully this changes NGINX to process .html files through PHP. Please let me know if you are still having issues getting it to work.

      Cheers,
      Emmet

  5. Avatar for Patrick
    Patrick on

    Thank you very much for this very good tutorial. Everything explained and everything worked directly. Keep up the excellecnt work!

  6. Avatar for jim Taylor
    jim Taylor on

    This is a very thorough tutorial, and, except for the missing index,php file on my Pi, worked flawlessly. I previously wasted half a day re-imaging my Pi because a previous tutorial purporting to do the same thing sent the whole OS off into parts unknown. Good job!

  7. Avatar for Gabriel
    Gabriel on

    Is PHP 7.3 still ok to use? If not can you please update commands?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Gabriel,

      It is perfectly fine to use PHP 7.3! In fact it is the latest version that is available through the standard package repository on Raspberry Pi OS Buster.

      PHP 7.3 will continue to have security updates until later this year.

      If you do want to use newer versions of PHP we can create an additional guide showing the best way of doing this.

      Cheers,
      Emmet

    2. Avatar for Stephen Lovatt
      Stephen Lovatt on

      It is necessary to change the php install command to:

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

      The command given in this tutorial has ONE instance of “7.3” remaining.

    3. Avatar for Emmet
      Emmet on
      Editor

      Hi Stephen,

      Thank you for pointing that out. I have fixed up the command. I had not realized I missed that last one.

      Cheers,
      Emmet

  8. Avatar for Rizal M
    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. Avatar for Emmet
      Emmet on
      Editor

      Hi Rizal,

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

      Cheers,
      Emmet

    2. Avatar for Matt KJ7CNL
      Matt KJ7CNL on

      I had this exact problem. try typing ‘sudo apt-get update’. when that’s done, re-install php via the step 3 above. Worked for me.

  9. Avatar for Dave Ingram
    Dave Ingram on

    hostname -I gives me 127.0.0.1
    This is not correct!

    1. Avatar for Gus
      Gus on
      Editor

      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.

  10. Avatar for Tim B
    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. Avatar for Gus
      Gus on
      Editor

      Hey Tim,

      Yes it should be possible.

      Cheers,
      Gus

  11. Avatar for Erik van Elten
    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. Avatar for karan Sharma
      karan Sharma on

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

    2. Avatar for Gus
      Gus on
      Editor

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

      sudo systemctl status nginx
  12. Avatar for nuno
    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.

  13. Avatar for Humberto
    Humberto on

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

    1. Avatar for Humberto
      Humberto on

      I add Nextcloud to my desire

  14. Avatar for Bob Deloyd
    Bob Deloyd on

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

  15. Avatar for Jack
    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 *