How to Install Nginx Web Server on Ubuntu 24.04

Updated on July 19, 2024
How to Install Nginx Web Server on Ubuntu 24.04 header image

Introduction

Nginx is an open-source web server application that enables the delivery of static and dynamic web applications or services on your server. Nginx works as a web server, load balancer, reverse proxy or HTTP cache to integrate with existing applications to form a stack or deliver web applications using your server IP address or domain.

This article explains how to install the Nginx web server on Ubuntu 24.04 and set up sample web applications to run on your server.

Prerequisites

Before you begin:

Install Nginx

The latest Nginx package is available in the default APT repositories on Ubuntu 24.04. Follow the steps below to update the server packages and install the Nginx web server application.

  1. Update the server package index.

    console
    $ sudo apt update
    
  2. Install Nginx.

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

    console
    $ sudo nginx -version
    

    Your output should be similar to the one below:

    nginx version: nginx/1.24.0 (Ubuntu)

Manage the Nginx System Service

Nginx uses the nginx systemd service profile to control the web server run time, and processes on your server. Follow the steps below to enable the Nginx system service and manage the web server processes on your server.

  1. Enable the Nginx web server to start automatically at boot time.

    console
    $ sudo systemctl enable nginx
    

    Output:

    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 Nginx service status and verify that it's running.

    console
    $ sudo systemctl status nginx
    

    Output:

    ● 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"

    Based on the above output value Active: active (running), Nginx is active and running on your server. If the service activity status includes Active: active (failed), stop any processes running on the HTTP port 80 and restart the nginx service.

Create a new Nginx Virtual Host

Nginx virtual hosts include special configurations that enable the web server to deliver web application files from a specific directory using a specific domain on your server. In the following steps, create a new sample Nginx virtual host configuration to securely deliver web application files on your server.

  1. Create a new Nginx virtual host configuration in the /etc/nginx/sites-available directory. For example, app.example.com.conf.

    console
    $ sudo nano /etc/nginx/sites-available/app.example.com.conf
    
  2. Add the following configurations to the file.

    nginx
    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 Nginx virtual host configuration listens for incoming connections using your app.example.com domain and serves your web application files from the /var/www/app.example.com web root directory on your server.

  3. Test the Nginx configuration for errors.

    console
    $ sudo nginx -t
    

    Output:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  4. Link the configuration to the /etc/nginx/sites-enabled directory to activate the virtual host on your server.

    console
    $ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
    
  5. Create a new web root directory /var/www/app.example.com to store your web application files.

    console
    $ sudo mkdir -p /var/www/app.example.com
    
  6. Create a new HTML application file within the directory. For example, index.html.

    console
    $ sudo nano /var/www/app.example.com/index.html
    
  7. Add the following HTML contents 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 heading when accessed in a web browser.

  8. Restart Nginx to apply the new virtual host configurations on your server.

    console
    $ sudo systemctl restart nginx
    
  9. Access your domain to verify that Nginx correctly delivers the virtual host web application files on your server. For example, use the Curl utility to test access to your domain.

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

    Output:

    Greetings from Vultr

Secure the Nginx Web Server

SSL certificates enable encrypted communications between a user's browser and the Nginx web server using HTTPS. By default, Nginx listens for incoming connections on the insecure HTTP port 80 . Follow the steps below to generate trusted Let's Encrypt SSL certificates and secure the Nginx web server to enable encrypted HTTPS connection requests.

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

    console
    $ sudo snap install --classic certbot
    
  2. View the installed Certbot version on your server.

    console
    $ sudo certbot --version
    

    Output:

    certbot 2.11.0
  3. Generate a new SSL certificate for your domain. Replace app.example.com with the actual domain in your Nginx virtual host configurations.

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

Set Up Firewall Rules

Uncomplicated Firewall (UFW) is available and active on Ubuntu 24.04 by default. In the following steps, configure the firewall with new connection rules to enable the Nginx web server listen for incoming HTTP and HTTPS connections on your server.

  1. Allow network connections on the HTTP port 80.

    console
    $ sudo ufw allow 80/tcp
    
  2. Allow connections on the HTTPS port 443.

    console
    $ sudo ufw allow 443/tcp
    
  3. View the UFW table and verify that the new connection rules are active.

    console
    $ sudo ufw status
    

    Output:

    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 24.04 server and configured the web server to deliver web applications on your server. Nginx supports multiple virtual host configurations you can use to securely deliver web applications on your server. In addition, you can integrate the Nginx web server with other applications such as MySQL and PHP to deliver dynamic web applications on your server. For more information and configuration options, visit the official Nginx documentation.