How to Install Nginx on Raspberry Pi

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to install an Nginx web server on a Raspberry Pi. I am using a Raspberry Pi 4, but any model should work.

By the end of this tutorial, you will have a basic Nginx website running on your Raspberry Pi. Let’s get started.

1. Update Your System

First things first. Login to your Raspberry Pi via SSH and update your system.

sudo apt update
sudo apt upgrade

2. Install the Nginx Web Server

Use the apt package manger to install the Nginx web server.

sudo apt install nginx

3. Create a Basic HTML Website

Note that the default Nginx configuration file at /etc/nginx/sites-available/default has a web root directory of /var/www/html/. Create an index.html file in this directory with the following content.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to my Raspberry Pi!!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to my Raspberry Pi!!</h1>
<p>Hello world. This website is being served from Nginx on my Raspberry Pi.</a>
<img src="Raspi-PGB001.png" width="500px"/>
</body>
</html>

Download the Raspberry Pi logo into the root directory of your website.

cd /var/www/html/
wget https://www.raspberrypi.org/app/uploads/2011/10/Raspi-PGB001.png

4. View the Website in a Browser

Don’t forget to change ownership of your website directory to the www-data user and group.

sudo chown www-data:www-data /var/www/html

To apply your changes, restart Nginx with this command.

sudo systemctl restart nginx

Now you can open a web bowser, navigate to the IP address of your Raspberry Pi, and view your website that’s being served by Nginx.

YouTube video

Next Steps: Host a Website

You have a lot of options for hosting a website on your Raspberry Pi including WordPress.


Meet Tony

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.