Docs:02.1 NginX

This page was last edited on 2 March 2025, at 20:21.

NGINX

We will install nginx on our pi, this is the software that will make our pi into our server. To do this we need to be working on the pi, so we will use SSH to access the pi from our personal machine.

[!REMINDER] Reminder: Working on the pi Go to section 01.3 SSH Connecting from a client to recap how to login to the server via SSH

Once you are logged in to your user on the pi, we will install nginx from the command line. We will use apt , which is a command-line utility that uses the Advanced Package Tool to download and install packages onto your device.

In the terminal, run the command:

apt install nginx

File locations

Packages are saved by default in your user’s home directory. The NginX software will also be saved inside the etc directory. We will work in here soon when we setup the config file of the server, for now just take note of where it is. The directory path is printed below:

cd /etc/nginx

Once NGINX is installed, it will create a few default HTML files that display a default webpage. This is a very simple static website. NGINX will save the website files in the var directory path below. We will work in here to edit our HTML index page.

cd /var/www/html

Take a note of these two locations as we will be jumping between them in the coming steps. ## Viewing the static site

Inside the /var/www/html the default html document will be named index.nginx-debian.html which you can see if you run the ls command while in the directory.

This website can be viewing locally by any machine that is on the same network as the server (previous referred to as the pi). To do this, paste the servers IP address in the browser of a local machine (like the laptop you are using to do this setup). ## Configuring the static site

The default configuration file for this simple static site can be found here (remember this is where nginx is saved) inside teh sites-enabled folder:

cd /etc/nginx/sites-enabled

Here you will see that you have a configuration for the default server, you can open that up to look at it by running the nano command:

nano default

[!important] DO NOT simply edit this default configuration file. Instead set up your own folder within the sites-enabled directory to start serving your custom static site.

For now, have a look at the contents of the default config file. Here is a shortened version of what you should see with some added comments:

server {
        # 80 the default port for listening for http traffic
        listen 80 default_server;
        listen [::]:80 default_server;

        # the root folder that your website will be served out of
        root /var/www/html;

        # Allowed file formats for the index / home page
        index index.html index.htm index.nginx-debian.html;

        # The server name that you would set
        server_name _;

        # A location directive for any incoming URI requests
        location / {

                # First attempt to serve request as file, then as directory,
                # then fall back to displaying a 404 not found error
                try_files $uri $uri/ =404;
        }
        
 }

This server block is where we will define our server set up in our own config file. For reference. ### Make your own .conf

To make a new file use the touch command in terminal. Make sure you are in the right folder before you do this:

cd /etc/nginx/sites-enabled
touch yoursite.conf

The following steps will go through how to edit the config file to serve your own static website.

      1. NginX debug

This Pitfalls and Common Mistakes guide should serve as a good resource to double check any changes you make. this nginx beginners guide does a good job of introducing the fundamentals.