Part 3: Installing Nginx and PHP

January 3, 2020 - Reading time: 92 minutes

Having configured Raspbian and UFW, we are now ready to install software which will host our content.  The two main options are Apache and Nginx.  Apache is more prevalent, with more than 60% market share.  However, Nginx is considered to be more resource efficient.  This is particularly relevant for our underpowered Raspberry Pi.

We can use apt-get to download and install Nginx.  We will simultaneously install a variant of PHP called PHP-fpm.  This is a scripting language which can be embedded in HTML.  Scripts are executed on the web server, and the resulting HTML is passed to the client to be rendered.  Our Content Management System (or CMS), which we will install later on, uses PHP to display content. 

You can think of Nginx as a waiter at a restaurant who takes down your order, passes this to the kitchen, and then returns with your food once ready.

To install Nginx and php-fpm as follows:

sudo apt-get install nginx php-fpm

Once done, we need to configure Nginx, and test to make sure it is functional.  We will modify this configuration later on to work correctly with our CMS.  However, let’s first establish a functional baseline configuration. 

Nginx is capable of hosting multiple sites/domains on a single server.  Each site configuration is stored in a separate text file.  We will only be hosting a single site and can make do with the default config file.  We can edit this file as follows:

sudo nano /etc/nginx/sites-available/default

This command is executed as root using sudo for impersonation.  It uses a text editor called nano.  The location of the pre-existing default file is in the /etc/nginx/sites-available folder.  Once in the file, we first need to tell Nginx to consider ‘index.php’ a default file.  This means that you can use a browser to type in a domain name, for example https://makerflare.com, and nginx will automatically present index.php if it exists.  This avoids having to type the fill URL (https://makerflare.com/index.php). 

Look for a line like this:

index index.html index.htm index.nginx-debian.html;

and replace with this (adding index.php, and removing other file names that aren’t needed):

index index.html index.htm index.php;

Note that our root site folder will not have any other index file other than index.php.  Technically you can remove the other file references as well, but there isn’t really any need to do so.

We now need to tell nginx to use php-fpm.  Scroll down to the section on php.  It should look like this:

#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/php/php7.0-fpm.sock;

#}

 Let’s uncomment (remove the #) on the following lines:

include snippets/fastcgi-php.conf;

and…

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

You can now save and exit nano (Ctrl + X, and follow the prompt to type “y” to save the file).

Nginx stores its website files at /var/www/html by default.  This folder is also locked down by default, making it difficult to add/modify/delete files within.  Let’s now change permissions on this folder to make it easier to change things in the future. 

These commands change both permissions (chmod) and ownership (chown) for /var/www/html:

sudo chown -R www-data:pi /var/www/html/
sudo chmod -R 770 /var/www/html/

You can read more about chown and chmod here: https://www.unixtutorial.org/difference-between-chmod-and-chown, and here: https://www.webservertalk.com/how-to-use-chmod-and-chown-command-in-linux/.

Finally, let’s test our configuration to make sure that both nginx and php are working.  We can do this by creating an index.php file in /var/www/html, which runs a small script to output default configuration info.  The function we will call is phpinfo().  We use echo to create the file as follows:

echo "<?php phpinfo(); ?>" > /var/www/html/index.php

We can now restart nginx as follows:

sudo systemctl stop nginx

sudo systemctl start nginx

Now check to make sure nginx is running properly:

systemctl status nginx

Now, open a browser and navigate to http://localhost.  This tells the browser to open a URL on the local machine (itself), on a default port of 80 (used by http).  Nginx will respond with the default index.php (which we configured in the default file located at /etc/nginx/sites-available).  The file itself will call the php function phpinfo(), which is executed and subsequently presented to the browser by nginx.

Recap:

We have successfully installed, configured, and tested nginx (a web server), and php (scripting engine).  Our Raspberry Pi now has the base foundation to start publishing content to the Internet!

About

A playground of creativity that combines Cloudflare technology with hobbyists.