Skip to content

How to install NGINX on raspberry pi

The Raspberry PI is great little mini computer the size of credit card. Despite it's size it is extremely versatile and for developers it can be an extremely useful tool.

In this tutorial we will investigate how we can use the Raspberry Pi to become a lightweight web server, by installing Nginx on it.

What is Nginx?

NGINX (pronounced engine x) is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web pages.

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.

Why Install NgInx on Raspberry Pi

I've mentioned previously that developers could make great use of a raspberry pi, because at it's core the Raspberry Pi is nothing more than a Linux based computer. Therefore Linux is an operating system that is extremely useful and every developer should have at least a rudimentary understanding of Linux.

One use case I particularly have found quite useful for the Raspberry while working from my home-based office, is to make use of them as lightweight web servers to enable the test of web applications.

I have also developed a lightweight DevOps environments to enable testing of Continuous Integration / Continuous Development (CI/CD).

Equipment used in this tutorial

Refresh available packages

It is standard practice when working with Linux servers and installing new software to update your software repositories to ensure you get the latest or most update version of the software you want to install.

We can do this by running the following command:

sudo apt-get update

Install NGINX

We can install the NGINX web server application using the following command

sudo apt-get install nginx

Once the installation has completed we can start the Nginx service

sudo /etc/init.d/nginx start

We can now test that every is working as expected and fire up a web browser on the computer on your network and browse to the IP address of your Raspberry Pi .ie http://192.168.0.7

You should see the default nginx test page.

By default, NGINX creates a test HTML file in the web folder. This default web page is served when you browse to http://localhost/ on the Pi itself, or http://192.168.1.7 (whatever the Pi's IP address is) from another computer on the network.

To find the Pi's IP address, type hostname -I at the command line (or read more about finding your IP address).

You should be able to browse to the default web page either on the Pi or from another computer on the network and you should see the following:

nginx test page on the raspberry pi

This will confirm that Nginx is now installed on your raspberry pi. You can now easily edit the default page to provide an example of how you can use Nginx to host static web pages.

Change the default nginx default web page

NGINX defaults its web page location to /var/www/html on Raspbian.

We can use nano to create a simple web page, to test this.

sudo nano /var/www/html/index.html

We can now add this simple HTML to create a very simple web page

<html>
    <body>
        <h1>Boo!</h1>
    </body>
</html>

simply save and close this file by using ctrl x

We can now browse to the website on the Raspberry Pi and instantly see our changes.

Raspberry Pi Boo page

This is made simple because the default configuration in the Nginx enabled sites enables any file named as index.html to be the default page. The nginx test page we seen earlier is actually named index.nginx-debian.html

if we list the files in /var/www/html we will now see we have two files

cd /var/www/html
ls
index.html  index.nginx-debian.html

You can add and edit any more files as you choose as required.

Summary

We have now successfully configured our Raspberry Pi to be able to serve static web pages. Which can be great fun to develop small house hold web sites etc.

In forthcoming posts we will explore how we can further expand on this and actually configure how Raspberry PI to serve dynamic websites which have been developed .net core, PHP or any JavaScript framework of choice.

Gary Woodfine
Latest posts by Gary Woodfine (see all)