Website Vidya
DeploymentStep-by-Step Guide

How to Install Nginx on Ubuntu 24.04

Learn how to install nginx on ubuntu 24.04 LTS with UFW firewall configuration, server blocks, systemd service management, and SSL security.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Install Nginx on Ubuntu 24.04
AI Executive SummaryCORE CIS AI

5 min read

Learn how to install nginx on ubuntu 24.04 LTS with UFW firewall configuration, server blocks, systemd service management, and SSL security.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Installing Nginx via APT on Ubuntu 24.04
  • Step 2: Configuring UFW Firewall for Nginx
Grounded in full engineering runbookCORE CIS Ai Engine

Interactive Builder for this Guide

Build your custom nginx syntax (How to Install Nginx on Ubuntu 24.04)

Generated Config Output
server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name app.example.com;

    ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript application/xml;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
Production Nginx reverse proxy -> 127.0.0.1:3000 for app.example.com.
Flag-by-Flag Breakdown
server_nameBinds this virtual host to app.example.com
return 301 https://...Redirects all plain HTTP requests permanently to TLS/HTTPS
proxy_pass http://127.0.0.1:3000Forwards HTTP + WebSocket requests to local application server
X-Content-Type-Options / X-Frame-OptionsProtects users against MIME sniffing and clickjacking

Nginx (pronounced 'engine-x') is an asynchronous, event-driven, open-source web server, reverse proxy, load balancer, and HTTP cache engineered for high concurrency and minimal memory consumption.

Unlike traditional process-per-connection web servers, Nginx handles thousands of simultaneous client connections per worker process, making it the premier choice for modern high-traffic web applications, microservices architectures, and static content delivery.

Ubuntu 24.04 LTS (Noble Numbat) includes modern, stable Nginx binaries directly in its core package repositories, configured with sensible defaults and integrated systemd service management.

In this tutorial, we guide you through how to install nginx on ubuntu 24.04, configure firewall profiles, manage background processes, and set up your initial virtual host server block.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A non-root user account with sudo administrative privileges.
  • An active UFW firewall.
  • A domain name pointing to your server's public IP address (recommended for SSL setup).

Step 1: Installing Nginx via APT on Ubuntu 24.04

Update your local package index to ensure you retrieve the latest release information and security enhancements.

Install the nginx package using the APT package manager:

Advertisement

APT downloads the Nginx binary, standard core modules, and systemd service configurations automatically:

# Update APT repository package lists:
sudo apt update

# Install Nginx web server:
sudo apt install -y nginx

# Check the installed Nginx version:
nginx -v
# Expected output confirms:
# nginx version: nginx/1.24.0 (Ubuntu)

Step 2: Configuring UFW Firewall for Nginx

Before Nginx can serve incoming web traffic, configure UFW to permit connections on ports 80 (HTTP) and 443 (HTTPS).

Nginx registers three application profiles with UFW upon installation:

Enable the Nginx Full profile to accommodate both standard HTTP traffic and encrypted HTTPS traffic:

Confirm that UFW successfully applied the rules by querying its status:

Advertisement
# List available Nginx firewall profiles:
sudo ufw app list
# Profiles: 'Nginx HTTP', 'Nginx HTTPS', 'Nginx Full'

# Allow both unencrypted HTTP and TLS HTTPS traffic:
sudo ufw allow 'Nginx Full'

# Confirm active firewall rules:
sudo ufw status
# Status: active
# Nginx Full                 ALLOW       Anywhere

Step 3: Checking the Nginx Service Status

On Ubuntu 24.04, the Nginx service automatically starts upon completion of the installation process.

Check the systemd service status to confirm that Nginx is active and running properly:

Send an HTTP request to the loopback address using curl to verify that Nginx delivers the default welcome page:

# Verify Nginx systemd daemon status:
sudo systemctl status nginx
# Expected output confirms: Active: active (running)

# Query default HTTP response:
curl -I http://localhost
# Expected output confirms:
# HTTP/1.1 200 OK
# Server: nginx/1.24.0 (Ubuntu)
# Content-Type: text/html

Step 4: Managing the Nginx Service with systemctl

Governing the Nginx service lifecycle is straightforward using standard systemctl commands.

Use the following commands to control the Nginx daemon:

Whenever you modify configuration files, prefer systemctl reload nginx over restart to reload directives without interrupting active connections:

# Stop the Nginx web server:
sudo systemctl stop nginx

# Start the Nginx web server:
sudo systemctl start nginx

# Restart Nginx (shuts down and restarts worker processes):
sudo systemctl restart nginx

# Reload configuration gracefully without dropping active connections:
sudo systemctl reload nginx

# Disable Nginx from launching automatically on system startup:
sudo systemctl disable nginx

# Enable Nginx to launch automatically on system startup:
sudo systemctl enable nginx

Step 5: Setting Up an Nginx Server Block (Virtual Host)

Server blocks in Nginx function similarly to Virtual Hosts in Apache, allowing you to encapsulate configuration details and host multiple websites from a single server.

Create a dedicated document root directory for your domain, such as example.com, and assign ownership to your active user:

Next, create an index.html sample page inside your new website directory:

# Create document root directory structure:
sudo mkdir -p /var/www/example.com/html

# Assign directory ownership to current user:
sudo chown -R $USER:$USER /var/www/example.com/html

# Configure standard directory permissions:
sudo chmod -R 755 /var/www/example.com

# Create a test index.html file:
cat << 'EOF' > /var/www/example.com/html/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Example.com</title>
</head>
<body>
    <h1>Success! The example.com Nginx server block is working on Ubuntu 24.04!</h1>
</body>
</html>
EOF

Creating the Server Block Configuration File

Create a new server block configuration file inside /etc/nginx/sites-available/:

Add the following configuration block, updating the domain name and root path accordingly:

# /etc/nginx/sites-available/example.com
server {
    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

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

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

Enabling the Server Block and Testing Syntax

Enable the server block by creating a symbolic link from sites-available to sites-enabled:

Always test your Nginx configuration syntax with nginx -t before applying changes to prevent service outages:

Once verified, reload Nginx to activate the new site configuration:

# Enable server block via symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# Validate Nginx configuration syntax:
sudo nginx -t
# Expected output confirms:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# Reload Nginx service:
sudo systemctl reload nginx

Step 6: Securing Nginx with Let's Encrypt SSL

Encrypting web traffic protects user privacy, enhances SEO performance, and secures login credentials.

Install Certbot and the Python Certbot plugin for Nginx using APT:

Obtain and install a free SSL certificate automatically with a single command:

# Install Certbot and Nginx plugin:
sudo apt install -y certbot python3-certbot-nginx

# Obtain certificate and configure HTTPS redirect:
sudo certbot --nginx -d example.com -d www.example.com

# Test automatic renewal process:
sudo certbot renew --dry-run
# Output confirms: Congratulations, all simulated renewals succeeded.

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

Cause: Another web server daemon (such as Apache) is currently bound to port 80.

Resolution: Stop or disable Apache using sudo systemctl stop apache2 && sudo systemctl disable apache2.

sudo systemctl stop apache2 && sudo systemctl restart nginx

403 Forbidden error on static files

Cause: Incorrect directory permissions or missing index file in document root.

Resolution: Ensure /var/www/example.com/html has 755 permissions and contains index.html.

sudo chmod -R 755 /var/www/example.com && ls -la /var/www/example.com/html

could not build server_names_hash_bucket_size

Cause: Domain names in server blocks are long and exceed the default memory hash bucket allocation.

Resolution: Uncomment server_names_hash_bucket_size 64; in /etc/nginx/nginx.conf.

sudo nano /etc/nginx/nginx.conf

Security Best Practices and Hardening

Hide Nginx version information in HTTP headers by adding server_tokens off; inside the http block of /etc/nginx/nginx.conf.

Configure aggressive rate limiting and connection limits in Nginx to mitigate Layer 7 brute-force and DDoS attacks.

Disable unnecessary HTTP methods (such as TRACK, TRACE, and OPTIONS) inside server block definitions.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Individual site configuration files reside in /etc/nginx/sites-available/. They are enabled by creating symbolic links pointing into /etc/nginx/sites-enabled/.
Run sudo nginx -t in your terminal. If the syntax is valid, it prints syntax is ok and test is successful.
Nginx writes standard access logs to /var/log/nginx/access.log and diagnostic error logs to /var/log/nginx/error.log.
Run sudo systemctl reload nginx. Nginx launches new worker processes with the updated configuration while allowing existing workers to gracefully finish active requests.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Uncategorized•

How to Redirect HTTP to HTTPS in Nginx

If you’re running a website today and it’s still accessible over plain HTTP, you’re already behind. Modern browsers flag non-HTTPS sites as “Not Secure,” search engines favor encrypted websites, and users are far more cautious about where t...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout