How to Install and Configure Nginx on Ubuntu 24.04


Nginx, (pronounced Engine X), is a lightweight and very popular HTTP web server that is widely used to power high traffic internet websites. It can also be used as a reverse proxy, load balancer, TCP/UDP server, and mail proxy server.

This post discusses how to install Nginx on Ubuntu 24.04. We will go further to discuss how to set up ufw firewall to allow connections to the web server. We will also look at how to create server blocks, which enable Nginx to support multiple websites on a single installation of the web sever.

If you need to install and configure Apache web server on Ubuntu system, then the guide on installing and configuring Apache on Ubuntu will be of great help.

Prerequisites

Before installing nginx, it is important to ensure that we have the latest list of available packages for our system as well as their updated versions.

Step 1: Update Ubuntu System

To update the system’s package index, we will type and run the following command:

Bash
sudo apt update

Step 2: Install Nginx

Nginx is available in Ubuntu’s default repositories. Therefore, we will be able to install it easily using the apt command. We will type and run the following command to install Nginx on Ubuntu system:

Bash
sudo apt install nginx

The above command will begin the installation of Nginx and its dependencies. When prompted to continue, type Y and press [Enter] key to confirm and continue with the installation.

After confirming the installation, Nginx will be installed on your Ubuntu system, ready to run as HTTP web server and respond to web requests.

Step 3: Verify Nginx Status

After installation of Nginx, Ubuntu 24.04 starts the web server automatically. Therefore, we should expect Nginx to be running after the installation. However, we can choose to verify that the web server is up and running by issuing the following command to check its status:

Bash
sudo systemctl status nginx

After running the above command, the status of Nginx will be displayed. If everything went well with installation, Nginx should be active and running, as shown in the following image:

From the above image, we can see that Nginx is active and running.

Step 4: Automatically Start Nginx on Boot

As a service running on Ubuntu, Nginx is expected to be running at all times, listening to web requests and responding to them. Due to this, It is important to ensure that Nginx starts automatically when the system is booted, without requiring our intervention to start.

Ubuntu 24.04 enables auto-start of Nginx web server after installation. However, we need to be sure that this is enabled. We can check this by looking at the result of the status command.

If Nginx is configured to start automatically after system boot, we will see enabled on the systemd service load line, as shown in the following image:

If not enabled, then we should run the following command so that Nginx will start automatically upon system boot:

Bash
sudo systemctl enable nginx

With this command, the Nginx web server will automatically start on system boot. However, if we do not want Nginx to start automatically on system boot, then we will disable auto-start by running the following command:

Bash
sudo systemctl disable nginx

After running this command, we will need to manually start the web server after system boot. We will look at how to manage the Nginx web server process, such as starting, stopping, and restarting the web server, shortly.

Step 5: Configure Firewall

Before starting to use the web server, it is important to configure ufw firewall so that Nginx will be able to receive web requests. By default, Nginx is configured to listen on port 80 for HTTP and port 443 for HTTPS requests respectively. These ports need to be allowed for the web server to be accessible and receive requests.

Rather than allow access to Nginx with port numbers 80 and 443 directly, the web server registers itself with ufw firewall using application profile names for HTTP, HTTPS, and both. We will therefore allow access to Nginx web server using these profile names rather than use the port numbers directly.

To view the profile names that Nginx uses to register itself with the ufw firewall, we will type and run the following command:

Bash
sudo ufw app list

The command produces the output shown in the following image:

The output from the comand shows that Nginx has three application profiles registered with the firewall. Let’s briefly look at each of these profiles:

  • Nginx Full: This profile allows access to the web server through both HTTP and HTTPS protocols, using both port 80 and port 443.
  • Nginx HTTP: This profile allows access to the web server through the HTTP protocol only, using port 80.
  • Nginx HTTPS: This profile allows access to the web server through the HTTPS protocol only, using port 443.

To use any of the Nginx application profiles registered with the firewall, we will type the profile name rather than the port number. Since the profile names have spaces, we will need to specify our choice in a quoted string.

Suppose we want to allow access through HTTPS, port 443, only, then we will type and run the following command to allow access through the firewall:

Bash
sudo ufw allow 'Nginx HTTPS'

The above command is similar to running the following command which specifies a port number:

Bash
sudo ufw allow 443/tcp

If we rather need to allow access to the web server using both HTTP and HTTPS, ports 80 and 443 respectively, then we will run the following command:

Bash
sudo ufw allow 'Nginx Full'

The above command is similar to allowing access to the web server by using port numbers directly, such as in the following command:

Bash
sudo ufw allow 80,443/tcp

Although specifying the port numbers directly work, it is customary and preferable to allow access to the web server using the profile names registered with ufw firewall.

After allowing access to the web server, we should enable ufw firewall, if not already done, so that the rules will be applied. Type and run the following command to ensure that the firewall rules are applied:

Bash
sudo ufw enable

We can check the ufw firewall rules that are currently added by running the following command:

Bash
sudo ufw status

You should see the Nginx profile that you selected in the output of the above command.

Step 6: Test Nginx Web Request

With the web server installed and its access through the firewall enabed, we can now test to see a working page. Nginx has a default page that is displayed if no page has been set up for a domain or IP address.

If you have an IP address linked to your Ubuntu system, then you can launch the web servers’ default page by typing the IP address in the address bar of your browser window, such as the following:

http://ip_address

If there is no IP address available, then using localhost in place of the IP address will likely work.

http://localhost

You should now see the following default page of the web server.

If you see Nginx’s welcome page above, then it indicates that your web server is running and ready to receive web requests from clients.

Step 7: Setting Up Server Blocks

One important feature of Nginx is its ability to host multiple websites, each with its own domain. For instance, with a single installation of Nginx, we can configure the web server to host multiple websites such as example.com, anotherexample.com, somewebsite.org, and or admin.somewebsite.org. We do this by creating and configuring server blocks. If you have used Apache web server before, then we are actually refering to virtual hosts.

A server block is a block of Nginx configuration directives that specifies the directory which contains the website files for a specific domain, as well as restrictions imposed on certain files and directories in the specified root directory.

Each domain has its own server block which informs the web server of where it can locate the website files for the request. For example, if the web server receives a request from example.com domain, it will know the directory from which it can locate the website files by reading from the server blocks.

Suppose we have a domain, such as example.com, we can create a server block for the domain by going through the following steps:

Create Directory for Domain

As indicated earlier, the web server expects us to specify the root directory that contains the website files for a domain. Therefore, we will create the directory that contains the files. We will create this directory under the /var/www/ directory.

Bash
cd /var/www/
sudo mkdir example.com

In the discussions that follow, you can replace example.com with your preferred domain name. Additionally, it is not mandatory to include the domain TLD, .com. For instance, naming the directory example can do.

With the directory created, let’s create a very simple HTML document that will display content in the browser when accessed. We will name the file index.html. We will put the file in the directory that we just created: /var/www/example.com.

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

Type the following HTML code in the text editor:

HTML
<!doctype html>
<html>
  <head>
    <title>Home - example.com</title>
  </head>
  <body>
    <h1>This is example.com website home page</h1>
  </body>
</html>

Set Directory Ownership

For security reasons, it is important to ensure that the owner of the directory that will contain the website files is set to the web server user. On Ubuntu, this is www-data.

Type and execute the following command so that the web server will be the owner of the directory that we created:

Bash
sudo chown -R www-data:www-data /var/www/example.com/

Create Server Block

We have said that a server block directs the web server to the root directory where the website files are located. Since we have already set up a directory for our example website, we are now ready to create a server block for the directory.

On Ubuntu, Nginx is installed in /etc/nginx directory. We can create server blocks in two ways. We can create the server block file in /etc/nginx/sites-available/ directory, or we can choose to create the server block file in /etc/nginx/conf.d/ directory.

If we choose to create the server block file in /etc/nginx/sites-available/ directory, then we will need to create a symbolic link to the file after it is created.

However, if we choose to create the server block file in /etc/nginx/conf.d/ directory, we will not be required to create a symbolic link to the file. The only consideration here is that the file that we create for the server block should end in .conf suffix, for example, example.com.conf.

In this guide, we will choose to create our server block file in /etc/nginx/conf.d/ directory. If you like, you can choose to create the file in /etc/nginx/sites-available/ directory, as we will see the command to create the symbolic link to the file later.

Let’s use nano to create the file example.com.conf in /etc/nginx/conf.d/ or /etc/nginx/sites-available/ directory, any of them that you like.

Bash
sudo nano /etc/nginx/conf.d/example.com.conf

Type the following code in the text editor:

Nginx
server {
    listen 80;
    listen [::]:80;
    
    server_name example.com;
    root /var/www/example.com;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404
    }
}

The above code is Nginx server block for a domain, in this case example.com. It begins with the server keyword followed by directives enclosed in braces, {}.

Line 2 and line 3 are directives that tell the server to listen on port 80 for this server block. The difference between line 2 and line 3 is that, line 2 directs the server to listen on all ipv4 addresses on the server whiles line 3 directs the server to listen on all ipv6 addresses on the server.

Line 5 is the name assigned to this server block and informs the server to respond to web requests from domains with this name. For example, if a request is received from http://example.com/welcome/, then this server block will respond to the request because the domain part of this request has the name assigned to the server block on line 5.

However, if a request is received from http://somewebsite.com/welcome/, then this server block will not respond to the request because the domain part of the request isn’t the name assigned to the server block on line 5.

Line 6 specifies the root directory for this server block. It indicates the starting directory where requested resources will be searched for and fetched. If you remember, this is the directory we created earlier into which we also created the index.html file.

The code above is a basic form of server block directives. There are other directives that can be used in a server block. For now, the directives we have used are enough to make the server respond to web requests.

If you created the server block file in /etc/nginx/sites-available/ directory, then you need to run the following command to create a symbolic link to the file in /etc/nginx/sites-enabled/ directory. The command isn’t required if you created the file in /etc/nginx/conf.d/ directory:

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

This command informs the Nginx web server that the server block that we created is enabled and can be run to receive web requests and respond to them.

Test and Reload Configurations

Before the directives in the server block can work, we need to instruct Nginx to reload the configuration updates that we have done. But there is one important thing to take note of anytime there is the need to restart or reload configuration updates.

Before reloading or restarting the web server after configuration updates, it is important to test the configuration updates performed. This is the time we can be notified of errors in the configuration.

To test for the server configuration updates, type and run the following command:

Bash
sudo nginx -t

The -t flag tells the web server to test the configuration and report any errors. If the web server detects any problems, it will report an error. If no problems are detected, it will report that the test was successful, as shown in the following image:

You may be wondering why nginx reports the configuration file /etc/nginx/nginx.conf, yet we regard the report on our server block configuration as successful. The reason is that nginx loads and includes other configuration files in its main configuration file, nginx.conf.

Since the main configuration file includes our configuration file, it means that the test scans the server block file that we created. Once successful, our server configuration file is clean, and we can go ahead to reload the web server.

Type and run the following command to load our server block configuration:

Bash
sudo systemctl reload nginx

We may alternatively issue a restart command, which will load the server block configuration when the web server is being restarted:

Bash
sudo systemctl restart nginx

Now that the server configurations have been loaded, we can type the domain in the browser address bar, such as http://example.com. Remember to replace example.com with your own domain that was used to set up the server block.

Typing the domain in browser’s address bar will render the content of the index.html file in the browser.

Managing the Web Server

There are several commands that we can execute to control the running of the Nginx web server, such as stopping, starting, restarting, and reloading of server configurations. In the following section, we will look at the commands that we need to execute to perform these operations.

Stopping Nginx

To stop the web server, we will need to run the following command:

Bash
sudo systemctl stop nginx

This command will stop the Nginx web server process. Once stopped, it will not be able to receive web requests and respond to them.

Starting Nginx

To start the web server when it is not running, we will need to run the following command:

Bash
sudo systemctl start nginx

The above command will start the Nginx web server so that it will be able to receive web requests and respond to them.

Restarting Nginx

Sometimes, we may need to stop the web server and immediately start it again. Rather than issue separate commands to stop and start the server, we can run the restart command to do both for us.

To restart the web server, we will run the following command:

Bash
sudo systemctl restart nginx

The above command will stop the web server and immediately start it again.

Reloading Configuration

Anytime we perform updates to web server configuration files, the updates do not take effect until the configurations are reloaded.

As indicated earlier, it is always recommended to test configuration updates before reloading the web server configuration.

To reload configuration updates, we will need to run the following command:

Bash
sudo systemctl reload nginx

The above command reloads nginx configurations without stopping the server.

Enabling Auto-Start on Boot

If we need to ensure that the nginx web server automatically starts anytime the system is booted, then we will need to run the following command.

Bash
sudo systemctl enable nginx

With this command, we will not need to manually start the web server when the system boots or restarts.

Disabling Auto-Start on Boot

If we want to prevent the web server from starting automatically after system boot or restart, then we will need to run the following command:

Bash
sudo systemctl disable nginx

The command will prevent the web server from staring automatically after system boot. In this case, we will need to manually start the nginx web server before it will be able to receive web requests and respond to them.

Conclusion

Nginx is a lightweight and powerful HTTP web server application that runs a lot of high traffic internet websites. It can be used as a reverse proxy, mail proxy server, TCP/UDP server, etc.

In this guide, we have been able to install Nginx on Ubuntu system and understood how to create server blocks for hosting multiple websites on a single installation of Nginx.


Leave a Reply

Your email address will not be published. Required fields are marked *