Skip to content

Nginx Setup

Shreyansh Singh edited this page Apr 19, 2021 · 1 revision

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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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.
* http://YOUR PI's IP ADDRESS
  1. 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 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 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
  1. 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
  1. Within this file, delete and paste the contents from nginx-sites-enabled-default.

This code sets up NGINX to process .php files by passing them through to PHP-FPM and connection with C++ code.

Once done, you are able to save and exit by pressing CTRL + X and then pressing Y and then ENTER.

  1. 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
  1. 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
  1. 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.

Clone this wiki locally