InfraRunBook
    Back to articles

    How to Set Up a Basic Website Using Nginx in Ubuntu

    Nginx
    Published: Dec 25, 2025
    Updated: Dec 25, 2025

    Learn how to set up a basic website using Nginx on Ubuntu. This detailed guide covers server blocks, directory structure, file permissions, firewall configuration, common errors, and troubleshooting for a production-ready setup.

    How to Set Up a Basic Website Using Nginx in Ubuntu

    Setting up a basic website on a Linux server is one of the most common real-world tasks for system administrators, DevOps engineers, and developers. Among all web servers available today, Nginx is widely preferred because of its speed, stability, and ability to handle traffic efficiently—even on low-resource servers.

    In this article, we’ll walk through setting up a basic website using Nginx on Ubuntu, using infrarunbook.com as the example domain throughout. The guide is written in a practical, human way, covering proper configuration, directory structure, firewall rules, permissions, and troubleshooting steps you’re likely to face in real environments.

    This guide assumes:

    • You are using Ubuntu (20.04 / 22.04 or later)
    • You have sudo access
    • You want a clean, maintainable, and production-ready setup

    Before We Begin: Make Sure Nginx Is Installed

    If Nginx is not already installed, you should complete the installation first.

    Refer to our detailed installation guide here:
     

    That article covers installation, service management, firewall basics, and verification. Once Nginx is installed and running, come back here and continue.


    How Nginx Serves a Website (Quick Overview)

    Understanding how Nginx works internally makes configuration and troubleshooting much easier.

    At a high level:

    • Nginx listens on ports (usually 80 and 443)
    • A server block decides how requests are handled
    • Each server block points to a document root
    • Files in the document root are served to users
    • File permissions control access

    Keeping this flow in mind will help you diagnose issues quickly later.


    Step 1: Create the Website Directory Structure

    By default, Nginx serves content from:

    /var/www/html 

    For a clean setup, each website should have its own directory.

    Since we are setting up infrarunbook.com, let’s create a dedicated directory.

    sudo mkdir -p /var/www/infrarunbook.com/public_html

     

    Why This Structure Is Recommended

    • Keeps sites organized
    • Makes permission management easier
    • Prevents accidental file overlap
    • Scales well when hosting multiple sites

    Step 2: Set Correct Ownership and Permissions

    Incorrect permissions are one of the most common causes of Nginx errors.

    Set Ownership and Permissions

    Recommended permissions:

    • Directories:
      755
    • Files:
      644

    sudo chown -R www-data:www-data /var/www/infrarunbook.com
    sudo find /var/www/infrarunbook.com -type d -exec chmod 755 {} \;
    sudo find /var/www/infrarunbook.com -type f -exec chmod 644 {} \;
     

    ⚠️ Even if files look correct, missing execute permission on a parent directory can cause a 403 Forbidden error.


    Step 3: Create a Basic Website Page

    Let’s create a simple

    index.html
    file.

    sudo nano /var/www/infrarunbook.com/public_html/index.html

    Paste the following content:
     

    <!DOCTYPE html>
    <html>
    <head>
     <title>Welcome to InfraRunBook</title>
    </head>
    <body>
     <h1>InfraRunBook is Live!</h1>
     <p>This website is successfully served using Nginx on Ubuntu.</p>
    </body>
    </html>

    Save and exit. After saving the file, explicitly verify permissions:

    ls -l /var/www/infrarunbook.com/public_html/index.html

    Expected output:

    -rw-r--r-- 1 www-data www-data index.html

    If not correct, fix it manually:

    sudo chmod 644 /var/www/infrarunbook.com/public_html/index.html
    sudo chown www-data:www-data /var/www/infrarunbook.com/public_html/index.html


    Step 4: Create an Nginx Server Block for infrarunbook.com

    Server blocks tell Nginx which site to serve and how.

    Create the Configuration File

    sudo nano /etc/nginx/sites-available/infrarunbook.com

    Add the following configuration:
     

    server {
       listen 80;
       server_name infrarunbook.com www.infrarunbook.com;

       root /var/www/infrarunbook.com/public_html;
       index index.html index.htm;

       access_log /var/log/nginx/infrarunbook.com.access.log;
       error_log  /var/log/nginx/infrarunbook.com.error.log;

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

     

    What Each Directive Does

    • listen 80
      – Accepts HTTP traffic
    • server_name
      – Domain names handled by this site
    • root
      – Location of website files
    • index
      – Default files to load
    • try_files
      – Prevents incorrect routing and directory listing

    This configuration is minimal, clean, and production-safe.


    Step 5: Enable the Website

    Enable the site by creating a symbolic link:

    sudo ln -s /etc/nginx/sites-available/infrarunbook.com /etc/nginx/sites-enabled/

    Disable the default Nginx site (recommended):

    sudo rm /etc/nginx/sites-enabled/default


    Step 6: Test Nginx Configuration

    Before reloading Nginx, always test the configuration.

    sudo nginx -t

    Expected output:

    syntax is ok
    test is successful

    If there are errors, Nginx will tell you exactly where to fix them.

    Reload Nginx:

    sudo systemctl reload nginx


    Step 7: Configure Firewall (UFW)

    Even a perfect Nginx setup won’t work if the firewall blocks traffic.

    Allow Nginx Traffic

    sudo ufw allow 'Nginx Full' 

    Or if you only want HTTP for now:

    sudo ufw allow 'Nginx HTTP' 

     

    Check firewall status:

    sudo ufw status

     

    Make sure port 80 & 443 is allowed.


    Step 8: Test the Website in Browser

    Open a browser and visit:

    http://infrarunbook.com 

    or your server’s public IP.

    If everything is correct, you’ll see the InfraRunBook is Live! message.


    Common Issues and Troubleshooting

    These are real-world problems you are likely to face.


    Issue 1: 403 Forbidden Error

    Most common issue.

    Possible Causes

    • Incorrect ownership
    • Wrong permissions
    • Parent directory access blocked

    Fix Checklist

    • Files owned by
      www-data
    • Directories have
      755
    • Correct
      root
      path in config

    Check error logs:

    sudo tail -f /var/log/nginx/infrarunbook.com.error.log


    Issue 2: Default Nginx Page Still Appears

    Reasons

    • Default site still enabled
    • New site not linked properly
    • server_name
      mismatch

    Fix

    ls -l /etc/nginx/sites-enabled/

    Only

    infrarunbook.com
    should be present.


    Issue 3: Website Not Loading at All

    Checks

    sudo systemctl status nginx sudo ss -tulnp | grep :80

    Make sure:

    • Nginx is running
    • No other service is using port 80
    • Firewall allows traffic

    Issue 4: Changes Not Reflecting

    Fix

    sudo systemctl reload nginx

    Clear browser cache or use a hard refresh (

    Ctrl + F5
    ).


    Understanding Nginx Logs (Very Important)

    Logs are your best debugging tool.

    Access Log

    /var/log/nginx/infrarunbook.com.access.log 

    Error Log

    /var/log/nginx/infrarunbook.com.error.log 

    Always check logs before guessing or changing random settings.


    Best Practices for Hosting Websites with Nginx

    • One website per server block
    • Never edit
      nginx.conf
      unless required
    • Always test configs before reload
    • Keep permissions strict
    • Enable HTTPS as the next step
    • Maintain clean directory structure

    These practices prevent downtime and configuration chaos.


    What’s Next?

    Now that infrarunbook.com is live on Nginx, your next logical steps are:

    • Enable HTTPS with Let’s Encrypt
    • Add security headers
    • Configure reverse proxy
    • Optimize performance
    • Add monitoring and logging

    Each of these builds on the foundation you’ve just created.


    Final Thoughts

    Setting up a basic website using Nginx on Ubuntu is simple—but doing it properly makes all the difference. With the right directory structure, permissions, firewall rules, and clean configuration, your site will be stable, secure, and easy to maintain.

    If you haven’t installed Nginx yet, make sure to read our installation guide first:

    Frequently Asked Questions

    Do I need to install Nginx before setting up a website?

    Yes, Nginx must be installed and running before you can host a website. Without Nginx installed, server blocks and website files will not be served.

    What is a server block in Nginx?

    A server block is an Nginx configuration that defines how requests for a specific domain or IP address are handled.

    Where should website files be stored in Ubuntu?

    Website files are commonly stored under /var/www/domain-name/public_html for clarity, security, and maintainability.

    Why does Nginx show a 403 Forbidden error?

    A 403 error usually occurs due to incorrect file ownership or permissions that prevent Nginx from reading website files.

    Which user runs Nginx on Ubuntu?

    Nginx worker processes typically run as the www-data user on Ubuntu systems.

    How do I enable a website in Nginx?

    You enable a website by creating a symbolic link from sites-available to sites-enabled and then reloading Nginx.

    Why is my default Nginx page still showing?

    The default page appears if the default site is still enabled or if your custom server block is not properly configured.

    Do I need to configure a firewall for Nginx?

    Yes, the firewall must allow HTTP and HTTPS traffic; otherwise, the website will not be accessible externally.

    How do I check Nginx errors?

    Nginx errors can be checked using the error log located at /var/log/nginx/error.log or site-specific error logs.

    Is Nginx suitable for production websites?

    Yes, Nginx is widely used in production for its performance, stability, and ability to handle high traffic efficiently.

    Related Articles