Install and Configure Nginx on Ubuntu 24.04

Nginx is a free, open-source web server that helps you deliver static and dynamic websites, apps, or services. On Ubuntu, you can use Nginx as a classic web server, a load balancer, a reverse proxy, or an HTTP cache.

This guide walks you through installing Nginx on Ubuntu 24.04 and setting up example web applications so they run smoothly on your server.

Prerequisites

Before you begin:

  • Deploy an Ubuntu 24.04 instance on centron.
  • Create a new domain or subdomain A record that points to your server IP address. For example, app.example.com.
  • Access the server using SSH and create a non-root user with sudo privileges.
  • Update the server.

Install Nginx on Ubuntu 24.04

The newest Nginx package is included in the standard APT repositories for Ubuntu 24.04. Use the steps below to refresh your package lists and install the Nginx web server.

Update the package index

Update the server package index.

Install Nginx

Install Nginx.

$ sudo apt install nginx -y

Verify the installed version

View the installed Nginx version on your server.

Your output should be similar to the one below:

nginx version: nginx/1.24.0 (Ubuntu)

Manage the Nginx System Service

Nginx relies on a systemd service called nginx to control its runtime and worker processes. Follow these steps to enable and manage the service on your server.

Enable Nginx at boot

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

$ 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

Start Nginx

Start the Nginx service.

$ sudo systemctl start nginx

Stop Nginx

Stop the Nginx service.

$ sudo systemctl stop nginx

Restart Nginx

Restart the Nginx service.

$ sudo systemctl restart nginx

Check service status

View the Nginx service status and verify that it’s running.

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

If you see Active: active (running), then Nginx is running correctly. If the status shows Active: active (failed), stop anything else using HTTP port 80 and then restart the nginx service.

Create a New Nginx Virtual Host

Nginx virtual hosts let you serve application files from a dedicated folder under a chosen domain. Use the steps below to set up a simple virtual host that securely publishes an example application.

Create a virtual host configuration

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

$ sudo nano /etc/nginx/sites-available/app.example.com.conf

Add the following configurations to the file.

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.

This configuration listens on your app.example.com domain and serves files from the directory /var/www/app.example.com.

Test the configuration

Test the Nginx configuration for errors.

Output:

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 configuration to the /etc/nginx/sites-enabled directory to activate the virtual host on your server.

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

Create the web root directory

Create a new web root directory /var/www/app.example.com to store your web application files.

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

Add a sample HTML page

Create a new HTML application file within the directory. For example, index.html.

$ sudo nano /var/www/app.example.com/index.html

Add the following HTML contents to the file.

Save and close the file.

When you open the page in a browser, this sample app shows a “Greetings from centron” heading.

Restart Nginx and test access

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

$ sudo systemctl restart nginx

Access your domain to confirm that Nginx serves the virtual host files correctly. For example, test it with Curl.

$ curl http://app.example.com

Output:

Secure the Nginx Web Server with SSL

SSL certificates encrypt traffic between a browser and your Nginx server over HTTPS. By default, Nginx accepts requests on the unsecured HTTP port 80. Use the steps below to create trusted Let’s Encrypt certificates and protect your web server with HTTPS.

Install Certbot

Install the Certbot Let’s Encrypt client package using Snap.

$ sudo snap install --classic certbot

Check Certbot version

View the installed Certbot version on your server.

Output:

Generate an SSL certificate

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

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

Set Up Firewall Rules

Ubuntu 24.04 includes Uncomplicated Firewall (UFW) enabled by default. Use the steps below to open HTTP and HTTPS ports for Nginx.

Allow HTTP traffic

Allow network connections on the HTTP port 80.

Allow HTTPS traffic

Allow connections on the HTTPS port 443.

Verify UFW rules

View the UFW table and verify that the new connection rules are active.

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’ve installed Nginx on Ubuntu 24.04 and set it up to serve web applications from your server. With Nginx, you can run multiple virtual hosts to publish different apps securely under separate domains. You can also combine Nginx with services like MySQL and PHP to power dynamic applications. For additional features and tuning options, refer to the official Nginx documentation.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: