Featured image of post How to Install Nginx on Ubuntu 24.04 ⚙️

How to Install Nginx on Ubuntu 24.04 ⚙️

Detailed guide to install and configure Nginx Web Server so you can deploy and optimize your website quickly.

📌 Introduction

Nginx is an open-source web server that can serve static and dynamic web applications. It can run as a web server, load balancer, reverse proxy, or HTTP cache, and integrates with existing applications to build complete systems or distribute web apps via IP or domain.

This guide shows how to install Nginx on Ubuntu 24.04 and set up a sample web app on your server.

✅ Prerequisites

Before you begin, make sure you:

  • 🚀 Provision an Ubuntu 24.04 server.
  • 🌍 Create an A record pointing your domain or subdomain to the server IP (e.g., app.example.com).
  • 🔐 Access the server via SSH and create a non-root sudo user.
  • 🔄 Update the system packages.

⚙️ Install NGINX on Ubuntu 24.04

The latest NGINX package is available in Ubuntu 24.04’s default APT repository. Follow these steps to update the system and install NGINX.

🔄 Update package lists

1
sudo apt update

📦 Install NGINX

1
sudo apt install nginx -y

🔍 Check installed NGINX version

1
sudo nginx -version

Sample output:

1
nginx version: nginx/1.24.0 (Ubuntu)

⚙️ Manage the NGINX service

NGINX uses the nginx systemd service to manage runtime and processes. Use the commands below to enable and manage the service.

🚀 Enable NGINX to start on boot

1
sudo systemctl enable nginx

Result:

1
2
Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable nginx

▶️ Start NGINX

1
sudo systemctl start nginx

⏹️ Stop NGINX

1
sudo systemctl stop nginx

🔄 Restart NGINX

1
sudo systemctl restart nginx

🔍 Check NGINX status

1
sudo systemctl status nginx

Sample output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Wed 2024-06-26 10:55:50 UTC; 1min 0s ago
       Docs: man:nginx(8)
    Process: 2397 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2399 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2400 (nginx)
      Tasks: 2 (limit: 1068)
     Memory: 1.7M (peak: 2.4M)
        CPU: 13ms
     CGroup: /system.slice/nginx.service
             ├─2400 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─2401 "nginx: worker process"
  • If Active: active (running) appears, NGINX is running. If you see Active: active (failed), stop any process using HTTP port 80, then restart NGINX.

🌐 Create an Nginx virtual host

An Nginx virtual host serves web app files from a specific directory using a domain name. Follow these steps to set up a sample virtual host securely.

📂 Create a new virtual host config file

  • In /etc/nginx/sites-available, create a new config file, for example: app.example.com.conf.
1
$ sudo nano /etc/nginx/sites-available/app.example.com.conf
  • Add the following configuration:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
server {
        listen 80;
        listen [::]:80;

        server_name app.example.com;

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

        location / {
                try_files $uri $uri/ =404;
        }
}
  • Save and close the file.

✅ Test Nginx configuration

  • Check the configuration for errors:
1
$ sudo nginx -t
  • Result:
1
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

🔗 Enable the virtual host

  • Link the config file into /etc/nginx/sites-enabled:
1
$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/

📁 Create the web root directory

1
$ sudo mkdir -p /var/www/app.example.com

📝 Create a sample HTML file

  • Create index.html in the web root:
1
$ sudo nano /var/www/app.example.com/index.html
  • Add the following content:
1
2
3
4
5
6
<html>
    <head></head>
    <body>
        <h1>Hello Hoang Duong</h1>
    </body>
</html>
  • Save and close the file.

🔄 Restart Nginx

1
$ sudo systemctl restart nginx

🌍 Verify the virtual host

  • Use curl to test:
1
$ curl http://app.example.com
  • Result:
1
Hello Hoang Duong

🔒 Secure the Nginx web server

SSL certificates encrypt communication between the browser and your server over HTTPS. By default, Nginx listens on insecure HTTP port 80. Follow these steps to obtain a trusted SSL certificate from Let’s Encrypt and enable HTTPS.

Install Certbot – Let’s Encrypt client 🔧

  • Install Certbot using Snap:
1
$ sudo snap install --classic certbot
  • Verify the Certbot version:
1
$ sudo certbot --version

📌 Sample output:

1
certbot 2.11.0

🛡️ Generate an SSL certificate for Nginx

  • Create a new SSL certificate for your domain. Replace app.example.com with your real domain:
1
$ sudo certbot --nginx -d app.example.com --agree-tos

This will:

  • ✅ Generate a valid SSL certificate
  • ✅ Automatically configure Nginx to use SSL
  • ✅ Enable HTTPS on your web server

Test automatic renewal 🔄

  • Let’s Encrypt certificates last 90 days. Check auto-renewal with:
1
$ sudo certbot renew --dry-run

If no errors appear, the certificate will renew automatically.

🌐 Verify the site with HTTPS

  • Open the browser and visit:
1
https://app.example.com

If you see the lock icon 🔒, SSL is installed successfully! 🚀


🔥 Configure UFW firewall rules

Uncomplicated Firewall (UFW) is installed and enabled by default on Ubuntu 24.04. Follow these steps to allow HTTP and HTTPS traffic.

Allow HTTP (Port 80) 🔌

  • Run:
1
$ sudo ufw allow 80/tcp

Allow HTTPS (Port 443) 🔐

  • Run:
1
$ sudo ufw allow 443/tcp

Check firewall status 🔢

  • Run:
1
$ sudo ufw status

👉 Sample output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

🛡️ After setup, the server only allows HTTP and HTTPS connections.


🎯 Conclusion

Congratulations on installing Nginx on Ubuntu 24.04 and configuring a web server for your applications. Nginx supports multiple virtual hosts to deploy applications securely. You can also integrate Nginx with MySQL and PHP to build dynamic web apps. For more configuration options, visit the official Nginx documentation. 🚀😊

Licensed under CC BY-NC-SA 4.0
Last updated on 00:31 20/02/2026