How to Install Nginx Web Server on Ubuntu 24.04
Easily install the Nginx web server on Ubuntu 24.04 with this simple guide. Learn essential commands and configuration steps to get your server up and running in minutes.

How to Install Nginx Web Server on Ubuntu 24.04

Nginx is one of the most widely used web servers today, known for its high performance, scalability, and ease of use. Whether you're hosting a simple website or deploying complex applications, Nginx offers the flexibility and power needed to handle modern web traffic efficiently. In this guide, we will walk you through the step-by-step process of installing and configuring Nginx on Ubuntu 24.04.

What Is Nginx?

Nginx (pronounced “engine-x”) is a free, open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache. Originally created to solve the C10k problem (handling 10,000 concurrent connections), Nginx has grown in popularity and is now used by many of the world’s busiest websites, including Netflix, Dropbox, and WordPress.com.

Prerequisites

Before we begin the installation, make sure you have:

  • A system running Ubuntu 24.04.

  • A user account with sudo privileges.

  • Access to a terminal or SSH connection to the server.

  • An active internet connection for downloading packages.

Step 1: Update Your System

 

Before installing any software, it’s best to update the system packages to ensure compatibility and security.

sudo apt update

sudo apt upgrade -y

This command refreshes the package list and upgrades all the installed software to the latest versions.

Step 2: Install Nginx

Nginx is included in Ubuntu’s default package repository, making the installation process straightforward.

 

To install Nginx, run:

sudo apt install nginx -y

The -y flag automatically confirms the installation prompt.

 

Once the installation is complete, Nginx will be available on your system.

Step 3: Start and Enable Nginx

 

To ensure that Nginx starts now and also after every system reboot, use the following commands:

sudo systemctl start nginx

sudo systemctl enable nginx

You can verify that the service is running with:

sudo systemctl status nginx

If it's active and running, Nginx is up!

Step 4: Adjust the Firewall

Ubuntu 24.04 often uses UFW (Uncomplicated Firewall). If UFW is enabled, you need to allow Nginx traffic.

 

Check the status of UFW:

sudo ufw status

If it’s active, allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'

sudo ufw reload

Step 5: Verify Nginx Installation

 

To confirm that Nginx is working, open a web browser and enter your server’s IP address:

http://your_server_ip

If Nginx is correctly installed and running, you will see the default Nginx welcome page, which means the web server is operational.

 

To find your server’s IP address, you can use:

ip a

or

curl ifconfig.me

Step 6: Basic Nginx Commands

 

Here are some basic commands you’ll use frequently to manage Nginx:

sudo systemctl start nginx

Stop Nginx:

sudo systemctl stop nginx

Restart Nginx:

sudo systemctl restart nginx

sudo systemctl reload nginx

Check for configuration errors:

sudo nginx -t

Step 7: Serve Your Website

By default, Nginx serves files from the /var/www/html directory. You can place your HTML files there to display a custom site.

 

To test this, create a simple HTML page:

sudo nano /var/www/html/index.html

Paste the following sample code:

<!DOCTYPE html>

<html>

<head>

  <title>Welcome to My Site</title>

</head>

<body>

  <h1>Success! Nginx is working on Ubuntu 24.04.</h1>

</body>

</html>

Save and close the file (CTRL+O, then CTRL+X), and reload Nginx:

sudo systemctl reload nginx

Visit your server’s IP again in the browser to see your custom page.

Step 8: Set Up a Server Block (Optional)

Nginx uses server blocks (similar to Apache’s virtual hosts) to manage multiple sites.

Here’s how to set one up:

 

  1. Create a directory for your site:

    sudo mkdir -p /var/www/example.com/html

Assign ownership:

Create a sample index.html:

nano /var/www/example.com/html/index.html

Create a new server block file:

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

Add:

server {

    listen 80;

    server_name example.com www.example.com;

 

    root /var/www/example.com/html;

    index index.html;

 

    location / {

        try_files $uri $uri/ =404;

    }

 

}

Enable the site:

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

Test configuration and reload:

sudo nginx -t

sudo systemctl reload nginx

Make sure to point your domain’s DNS to the server’s IP.

Conclusion

Installing the Nginx web server on Ubuntu 24.04 is a simple and efficient process. With just a few commands, you can have a powerful web server up and running. Whether you're hosting a personal project, a company website, or a high-traffic application, Nginx provides the performance and flexibility you need.

 

As a next step, you might consider securing your site with Let's Encrypt SSL, configuring firewall rules, or using Nginx as a reverse proxy for applications like Node.js or Python.

 

How to Install Nginx Web Server on Ubuntu 24.04

disclaimer

Comments

https://themediumblog.com/public/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!