logo

How to Set Up Nginx on Raspberry Pi with Ubuntu

O

Ohidur Rahman Bappy

MAR 22, 2025

Step 1 – Installing Nginx

To install Nginx, update your package list and install using the following commands:

sudo apt update
sudo apt install nginx

Step 2 – Adjusting the Firewall

Check available application profiles by typing:

sudo ufw app list

You should see:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

Enable the profile that allows HTTP traffic:

sudo ufw allow 'Nginx HTTP'

Verify the firewall changes:

sudo ufw status

Step 3 – Checking your Web Server

Ubuntu automatically starts Nginx post-installation. Check the service status:

systemctl status nginx

Step 4 – Managing the Nginx Process

Learn management commands for the Nginx service:

  • Stop the server: sudo systemctl stop nginx
  • Start the server: sudo systemctl start nginx
  • Restart the server: sudo systemctl restart nginx
  • Reload without dropping connections: sudo systemctl reload nginx
  • Disable automatic start at boot: sudo systemctl disable nginx
  • Enable automatic start at boot: sudo systemctl enable nginx

Step 5 – Setting Up Server Blocks

Create custom server blocks for multiple domains. Prepare your directory:

sudo mkdir -p /var/www/your_domain/html
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain

Create a sample index page:

nano /var/www/your_domain/html/index.html

Add:

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain server block is working!</h1>
    </body>
</html>

Configure Nginx:

sudo nano /etc/nginx/sites-available/your_domain

Insert:

server {
        listen 80;
        listen [::]:80;

        root /var/www/your_domain/html;
        index index.html index.htm;

        server_name your_domain www.your_domain;

        location / {
                try_files $uri $uri/ =404;
        }
}

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Update nginx.conf for additional server names:

sudo nano /etc/nginx/nginx.conf

Uncomment server_names_hash_bucket_size:

http {
    server_names_hash_bucket_size 64;
}

Test for syntax errors:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Step 6 – Familiarize with Important Nginx Files and Directories

Content

  • /var/www/html: Default web content directory.

Server Configuration

  • /etc/nginx: Nginx configuration directory.
  • /etc/nginx/nginx.conf: Main configuration file.
  • /etc/nginx/sites-available/: Store server block configurations.
  • /etc/nginx/sites-enabled/: Link enabled configurations.
  • /etc/nginx/snippets: Configuration fragments.

Server Logs

  • /var/log/nginx/access.log: Access log.
  • /var/log/nginx/error.log: Error log.

Add Reverse Proxy on Different Ports

Edit /etc/nginx/nginx.conf to add reverse proxies:

http {
    server {
        location /some/path/ {
            proxy_pass http://192.168.0.1:7000;
        }
        location /another/path/ {
            proxy_pass http://192.168.0.2:8000;
        }
    }
}