How to Install Nginx Web Server on Ubuntu 25.04

Updated on 25 April, 2025
How to Install Nginx Web Server on Ubuntu 25.04 header image

Nginx is a free and open-source web server software that facilitates the hosting of static and dynamic web applications or services on your server. It can function as a web server, load balancer, reverse proxy, or HTTP cache, enabling integration with other applications to form a complete stack or to serve web applications through your server’s IP address or domain.

This article explains the process for installing Nginx on Ubuntu 25.04 and configuring sample web applications to ensure optimal performance on your server.

Prerequisites

Before you begin, you need to:

Install Nginx on Ubuntu 25.04

Nginx is included in the default APT repositories for Ubuntu 25.04. To install the latest version of Nginx, follow the steps below to update your server's package list and install the web server.

  1. Update the package index on your server.

    console
    $ sudo apt update
    
  2. Install the Nginx web server.

    console
    $ sudo apt install nginx -y
    
  3. View the installed version of Nginx web server.

    console
    $ nginx -version
    

    Your output should be similar to the one below:

    nginx version: nginx/1.26.3 (Ubuntu)

Manage the Nginx System Service

Nginx operates as a systemd service on Ubuntu, which allows you to control its processes and manage its runtime. Follow the steps below to enable the nginx service start automatically on boot and control the Nginx system service.

  1. Enable the Nginx service to start automatically on boot.

    console
    $ sudo systemctl enable nginx
    

    Your output should be similar to the one below:

    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
  2. Start the Nginx service.

    console
    $ sudo systemctl start nginx
    
  3. Stop the Nginx service.

    console
    $ sudo systemctl stop nginx
    
  4. Restart the Nginx service.

    console
    $ sudo systemctl restart nginx
    
  5. View the status of the Nginx service to confirm it's running.

    console
    $ sudo systemctl status nginx
    

    Your output should be similar to the one below:

    ● 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 2025-04-23 16:27:12 UTC; 8s ago
     Invocation: 83add31885be44869b2e3573230fbdee
           Docs: man:nginx(8)
        Process: 5578 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
        Process: 5580 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
       Main PID: 5581 (nginx)
          Tasks: 5 (limit: 8761)
         Memory: 4.4M (peak: 5M)
            CPU: 35ms
         CGroup: /system.slice/nginx.service
                 ├─5581 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
                 ├─5582 "nginx: worker process"
                 ├─5583 "nginx: worker process"
                 ├─5584 "nginx: worker process"
                 └─5585 "nginx: worker process"

    Based on the output value Active: active (running), Nginx is running on your server. If the service status shows Active: active (failed), it indicates an issue. In such cases, stop any processes currently occupying HTTP port 80 and then restart the Nginx service to resolve the issue.

Create a New Nginx Virtual Host

Nginx virtual hosts allow you to configure the server to deliver specific web application files based on the domain and directory structure of your server. Follow the steps below to create a new virtual host configuration for securely serving web applications.

  1. Create a new Nginx virtual host configuration file in the sites available directory.

    console
    $ sudo nano /etc/nginx/sites-available/app.example.com.conf
    
  2. Add the following code to define the virtual host settings.

    ini
    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.

    The above configuration ensures that Nginx listens for requests to the app.example.com domain and serves the content from the /var/www/app.example.com web root directory.

  3. Link the virtual host configuration to the /etc/nginx/sites-enabled directory to activate in on the server.

    console
    $ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
    
  4. Set up a new directory at /var/www/app.example.com to store your web application’s files

    console
    $ sudo mkdir -p /var/www/app.example.com
    
  5. Create an HTML file (e.g., index.html) in the web root directory.

    console
    $ sudo nano /var/www/app.example.com/index.html
    
  6. Add the following HTML content to the file.

    html
    <html>
        <head></head>
        <body>
            <h1>Greetings from Vultr</h1> 
        </body>
    </html>
    

    Save and close the file.

    The above HTML application displays a Greetings from Vultr message when accessed in a web browser.

  7. Run the following command to check for any configuration errors.

    console
    $ sudo nginx -t
    

    Your output should be similar to the one below:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  8. Restart the nginx service to apply the new virtual host configuration.

    console
    $ sudo systemctl restart nginx
    
  9. Access your domain defined in the virtual host using curl to confirm that the Nginx virtual host is working correctly.

    console
    $ curl http://app.example.com
    

    If configured correctly, the command returns the following response:

    <html>
        <head></head>
        <body>
            <h1>Greetings from Vultr</h1> 
        </body>
    </html>

Secure the Nginx Web Server

SSL certificates protect data by encrypting communication between a user's browser and the Nginx web server over the HTTPS protocol. Since Nginx listens on the unencrypted HTTP port 80 by default, you can enhance security by generating a trusted SSL certificate with Let's Encrypt using Certbot. Follow the steps below to secure your Nginx server with HTTPS support.

  1. Install the Certbot Let's Encrypt client using Snap.

    console
    $ sudo snap install --classic certbot
    
  2. VIew the installed Certbot version.

    console
    $ certbot --version
    

    Your output should be similar to the one below:

    certbot 4.0.0
  3. Allow HTTP connections on port 80 for Certbot's ACME verification.

    console
    $ sudo ufw allow 80/tcp
    
  4. Generate an SSL certificate for your domain. Replace app.example.com with your actual domain name configured in the Nginx virtual host file.

    console
    $ sudo certbot --nginx -d app.example.com --agree-tos
    

    Your output should be similar to the one below:

    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/app.example.com/fullchain.pem
    Key is saved at:         /etc/letsencrypt/live/app.example.com/privkey.pem
    This certificate expires on 2025-07-22.
    These files will be updated when the certificate renews.
    Certbot has set up a scheduled task to automatically renew this certificate in the background.
    
    Deploying certificate
    Successfully deployed certificate for app.example.com to /etc/nginx/sites-enabled/app.example.com.conf
    Congratulations! You have successfully enabled HTTPS on https://app.example.com

Set Up Firewall Rules

Uncomplicated Firewall (UFW) is installed and enabled by default on Ubuntu 25.04. Use the following steps to configure the UFW firewall and allow secure connections to your Nginx web server.

  1. Allow HTTPS traffic on port 443.

    console
    $ sudo ufw allow 443/tcp
    
  2. View the UFW status and verify that the updated firewall rules are active.

    console
    $ sudo ufw status
    

    Your output should be similar to the one below:

    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)

Conclusion

You have installed Nginx on your Ubuntu 25.04 server and configured it to securely serve applications over HTTPS using a trusted Let's Encrypt SSL certificate. Nginx supports multiple virtual host configurations and can be integrated with other services such as MySQL and PHP to deploy dynamic web applications. For additional configuration options, visit the official Nginx documentation.

Comments

No comments yet.