What is NGINX ?
NGINX is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web pages. In its initial release, it functioned for HTTP web serving. It is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. It is an alternative for Apache.
Setting up NGINX on the Raspberry Pi
- 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. With the packages now up to date we can proceed on with the installation of NGINX. Finally, let’s install NGINX onto our Raspberry Pi by running the following command on your Raspberry Pi.
sudo apt-get install nginx
3. Now with NGINX installed, we can now start up the software. So let’s type the following command into terminal to start up the web server on your Raspberry Pi.
sudo systemctl start nginx
4. NGINX places an HTML file under the web folder. As such, you can test if NGINX installed properly by navigating to http://localhost/
or http://[YOUR RASPBERRY PI'S IP ADDRESS]/
.
5. To view your IP address, you may run the command hostname -I.
hostname -I
6.If installing NGINX on the Raspberry Pi went as planned, you’ll see a friendly “Welcome to nginx” message when you navigate to http://localhost/ in a browser.
Configuring NGINX for PHP
- 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.
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 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/php7.3-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.Hopefully, by now you have successfully set up your Raspberry Pi with a fully functional NGINX web server with PHP working correctly.