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.
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:
1 2 sudo apt-get update sudo apt-get install nginx
Once the installation has completed, you can start the Nginx web service with:
1 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
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
1 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.
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.
1 sudo nano /etc/nginx/sites-enabled/default
Find the following line:
1 index index.html index.htm;
index.php to the list:
1 index index.php index.html index.htm;
Then look for a section which is commented out (with
Reload Nginx to pick up the recent configuration changes:
1 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.
1 2 cd /var/www/html/ sudo mv index.nginx-debian.html index.php
Now open the
index.php file with
1 sudo nano index.php
<body> tag in the page, but before the closing
</body> tag, add the following:
1 2 3 <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.
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.