NGINX (pronounced engine x) is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web content.
Like Apache, NGINX can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP, .net core, JavaScript or any other popular web development programming language.
In this tutorial we will investigate how we can use the Raspberry Pi to become a lightweight web server, by installing Nginx on it.
Installing the Essentials
In an open Terminal session window, type the following command to install Nginx:
sudo apt-get update
sudo apt-get install nginx
Once the installation has completed, you can start the Nginx web service with:
sudo /etc/init.d/nginx start
You can now test that the Nginx web server is working as expected by starting up a local Chromium browser instance on your Raspberry Pi.
You should see the default nginx test page when you open http://localhost
Installing PHP
Now that Nginx is running, it’s just another trivial package install to enable PHP support! In an open Terminal session
window, just type the following to install the latest php-fpm
connector.
sudo apt-get install php-fpm
Like most web servers, Nginx allows for a set of “default” supported web documents. In other words, when you make a browser
request to the root of a folder on your web server without any specific page, eg. https://yoursite.com/
vs. https://yoursite.com/page1.html
,
Nginx will check it’s list of supported default documents. If your folder has such a named default document file, then
it will return that automatically to the browser.
Let’s add support for PHP.
sudo nano /etc/nginx/sites-enabled/default
Find the following line:
index index.html index.htm;
and add index.php
to the list:
index index.php index.html index.htm;
Then look for a section which is commented out (with #
)
Then update
Reload Nginx to pick up the recent configuration changes:
sudo /etc/init.d/nginx reload
Create a test default PHP page
To help test (and demonstrate) our newly added PHP support, let’s create a default PHP page.
cd /var/www/html/
sudo mv index.nginx-debian.html index.php
Now open the index.php
file with nano
sudo nano index.php
After the <body>
tag in the page, but before the closing </body>
tag, add the following:
<body>
<?php echo phpinfo(); ?>
</body>
Save and close up nano
. Now open Chromium browser and navigate to http://localhost
. You should
see the familiar PHP info output.
Conclusion
Hopefully this was a simple walkthrough to show you how to setup the latest version of Nginx on the Raspberry Pi. As an additional bonus, I also included some easy steps to enable a fast and lightweight PHP connector for Nginx.