Installation Guide

How to Install and Configure Nginx on Ubuntu 24.04 LTS

A beginner-friendly Nginx guide for Ubuntu 24.04 LTS that covers installation, service checks, firewall rules, a simple server block, and the most common setup mistakes.

Back to tutorials

This guide shows a safe, practical way to install Nginx on Ubuntu 24.04 LTS and configure a simple website without jumping straight into SSL, reverse proxy rules, or advanced performance tuning. That narrower scope is intentional. If you are publishing your first site, learning Linux basics, or setting up a lightweight developer server, the fastest win is to get Nginx installed correctly, confirm that traffic can reach port 80, and understand how a basic server block works.

The commands and flow below are grounded in official Ubuntu Server and NGINX documentation, but the wording and guidance here are written for practical setup work. By the end, you will have a running Nginx service, a reachable test page, a custom web root, and a server block you can extend later. If you are deploying this as part of a public website, it is also worth reviewing the deployment readiness audit and security header planner after the core setup is working.

Why Nginx is useful

Nginx is a web server that is commonly used to serve static websites, act as a reverse proxy for app servers, and terminate HTTP or HTTPS traffic before a backend service receives requests. Developers often choose it because it is fast, widely documented, and works well for both small personal sites and larger production systems.

For a personal resource hub or tutorial site, Nginx is useful because it can serve static files directly, handle multiple sites with separate server blocks, and grow with your setup later if you decide to add SSL, proxy an app, or split traffic across services. The key thing to understand is that Nginx is not just a package you install. It is a set of configuration files plus a running service. A good first setup means treating both parts with care.

What you need before you start

  • An Ubuntu 24.04 LTS machine or virtual server.
  • A user account with `sudo` privileges.
  • Terminal access over SSH or local shell access.
  • An IP address you can open in a browser.
  • An optional domain name if you want to move beyond IP-based testing later.

This tutorial assumes you are starting from a normal Ubuntu server state. If you already have Apache or another service listening on port 80, you may need to stop or remove that service first. If this server is exposed to the internet, make sure you know your firewall posture before enabling web access.

Step 1: Update package information

Before installing anything, refresh your package information so Ubuntu knows about the latest available packages in your configured repositories.

command
sudo apt update

This command does not upgrade every package on the system. It refreshes the local package index so subsequent install commands use current repository metadata. If `apt update` reports connectivity issues, repository errors, or lock files in use, fix those first instead of continuing. Package problems early in the process tend to cause confusing errors later.

Step 2: Install Nginx

Once the package index is current, install Nginx using `apt`. Ubuntu's server documentation uses the standard package available in the Ubuntu repositories.

command
sudo apt install nginx

During installation, Ubuntu will place the Nginx binaries and configuration files in their expected locations and start the service automatically. On Ubuntu 24.04, the package installs cleanly for a standard setup and creates the systemd service unit for `nginx`. If the install process warns that port 80 is already in use, stop and resolve that conflict before continuing.

Step 3: Verify the service

After installation, check that the service is active. Ubuntu Server docs show `systemctl status nginx` as the basic verification step.

command
sudo systemctl status nginx

In a healthy setup, you should see an `active (running)` state. That tells you the package installed and the daemon started. If you prefer a short check instead of the full status screen, this is also useful:

command
sudo systemctl is-active nginx

If Nginx is not active, do not guess. Read the status output carefully. A failure at this stage often means one of three things: another service is already using port 80, Nginx has a configuration issue from an earlier manual edit, or the installation was interrupted.

Step 4: Allow traffic with UFW

If you are using Ubuntu's uncomplicated firewall, you need to allow HTTP traffic so browsers can reach the server. A common profile-based approach is:

command
sudo ufw allow 'Nginx Full'

If you only want HTTP for now, you can also allow port 80 specifically, but the `Nginx Full` profile is useful because it covers both HTTP and HTTPS for future expansion. Check the firewall status afterward:

command
sudo ufw status

If `ufw` is not enabled on your server, this command may show an inactive firewall. That is not automatically wrong, but it does mean you should understand how access is being controlled elsewhere. On cloud servers, firewall rules may also exist at the provider level.

Step 5: Check the default page

At this point, Nginx should be serving the default page. Open your server's IP address in a browser:

address
http://your_server_ip

You should see the default Nginx welcome page. That confirms three things at once: Nginx is running, port 80 is reachable, and the default site configuration is serving content from the expected location. If you are testing locally on the server itself, `curl` is also a quick check:

command
curl http://localhost

Do not worry if this is not your final site content. The default page is just a signal that the basic pipeline is alive. We will replace it with a simple custom site next.

Step 6: Create a basic site directory

Instead of editing the default Ubuntu web root directly, create a dedicated directory for your site. This makes the configuration easier to reason about and matches how multi-site setups usually grow.

command
sudo mkdir -p /var/www/tutorial-site
sudo chown -R $USER:$USER /var/www/tutorial-site

Now create a minimal HTML file:

code
cat > /var/www/tutorial-site/index.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Nginx Test Page</title>
  </head>
  <body>
    <h1>Nginx is serving this site correctly.</h1>
    <p>This page comes from /var/www/tutorial-site.</p>
  </body>
</html>
EOF

The content can be simple. The goal is to confirm that your custom server block points to the directory you expect, not just that Nginx is working in the abstract.

Step 7: Create a server block

Ubuntu's Nginx configuration commonly uses `sites-available` and `sites-enabled`. Create a new server block file:

command
sudo nano /etc/nginx/sites-available/tutorial-site

Add this basic configuration:

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

    server_name _;
    root /var/www/tutorial-site;
    index index.html;

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

This is a deliberately simple starting point. The `server_name _;` pattern is acceptable for generic testing when you are not binding to a real domain yet. If you already have a domain, replace `_` with that hostname.

Enable the site by creating a symlink:

command
sudo ln -s /etc/nginx/sites-available/tutorial-site /etc/nginx/sites-enabled/tutorial-site

If you want this site to be the primary response instead of the default welcome page, remove the default site link:

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

Step 8: Test the configuration

Before reloading Nginx, validate the configuration file syntax. This is one of the most important habits in Nginx administration:

command
sudo nginx -t

A successful result should say the syntax is okay and the configuration test is successful. If it fails, do not reload the service yet. Read the file path and line number in the error carefully. Common mistakes include a missing semicolon, a typo in the file path, or a malformed directive block.

Step 9: Reload Nginx safely

Once the configuration test passes, reload the service:

command
sudo systemctl reload nginx

Reloading is safer than restarting for configuration changes because it keeps the service running while the new configuration is applied. After reload, visit your server IP in a browser again. You should now see the custom HTML page from `/var/www/tutorial-site` instead of the default Nginx page.

Common fixes

Port 80 does not load

First check that Nginx is active with `systemctl status nginx`. Then check your firewall and any cloud-level security groups. If the service is running but the page does not open, the issue is often outside Nginx itself.

Firewall blocks traffic

Run `sudo ufw status` and confirm that HTTP or `Nginx Full` is allowed. If `ufw` is inactive, make sure another firewall layer is not silently blocking traffic.

Configuration test fails

Always rerun `sudo nginx -t` after edits. The error message usually points to the exact file and line. Nginx configuration is strict, so one missing semicolon can invalidate the whole file.

The default page still appears

That usually means the default site is still enabled or your new server block does not match the request the way you expect. Removing the default symlink and reloading Nginx is the most common fix for first-time setups.

Permission issues in the web root

If files exist but Nginx cannot serve them, check ownership and directory permissions under `/var/www`. The user running Nginx must be able to read the files and traverse the directory path.

Useful Nginx commands cheat sheet

commands
sudo apt update
sudo apt install nginx
sudo systemctl status nginx
sudo systemctl reload nginx
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo nginx -t
sudo ufw allow 'Nginx Full'
sudo ufw status
curl http://localhost

What to do next

Once the basic configuration works, the next useful step is deciding what kind of site or app this server will host. If it is a public website, review deployment quality with the deployment readiness audit. If you are preparing for a public launch, also think about browser protections and headers using the security header planner.

You can also return to the tutorials hub to explore the rest of the planned content. If you want a follow-up article, the most natural next topics are enabling HTTPS with Certbot, pointing a real domain at the server, or using Nginx as a reverse proxy for a Node.js app. If you get stuck while adapting these steps to your own server, the contact page is the place to reach out.