How to Install and Configure NGINX on Ubuntu 24.04


NGINX, pronounced as Engine-X, is a robust asynchronous and event-driven web server. It can handle multiple requests on a single thread, hence making it an efficient server for high-traffic websites.

In this guide, I will go through how to install the NGINX server on Ubuntu and how to configure it.

In this guide, I will guide you through the steps to install and set up the NGINX server on the most recent Ubuntu 24.04 Noble Numbat release. I will guide you through each step, from downloading and installing NGINX to configuring it for single and multiple-site hosting. By the end of this guide, you will know how to deploy NGINX on Ubuntu and customize it to suit your needs.

Prerequisites

Before installing NGINX, it is advised to remove any other server that is listening to ports 80 and 443, such as Apache. It helps to avoid the port conflict and other complexities. To remove the Apache from Ubuntu, execute the command given below:

sudo apt remove --autoremove apache2 apache2-utils

Next, remove the Apache configuration files.

sudo rm -r /etc/apache2

sudo rm -r /var/log/apache2

Note: To install NGINX and Apache side by side, you need to assign different ports to each server by modifying their configuration files.

 

Installing NGINX on Ubuntu

Go through the steps mentioned below to install the NGINX web server on Ubuntu 24.04 Noble Numbat.

1. Update the packages list and system repositories.

sudo apt update

2. NGINX is available in Ubuntu’s default repository, making it installable using the APT package manager that comes with Ubuntu.

sudo apt install nginx 

3. To verify the installation, check the status of the server using the systemctl status command.

systemctl status nginx

To check the version of the NGINX.

nginx -v

 

NGINX Home Page

Now, type your server IP address or localhost in the browser to see the welcome page of the NGINX web server.

http://localhost

The server IP address can be obtained using the ip a command.

 

Managing NGINX Server

The NGINX on Ubuntu can be managed using the default systemctl command.

To start and stop the web server.

sudo systemctl start nginx

sudo systemctl stop nginx

To instruct the systemd service manager to restart the NGINX server.

sudo systemctl restart nginx

To enable and disable the web server.

sudo systemctl enable nginx

sudo systemctl disable nginx

The disable command will ensure that the web server remains disabled even after a system reboot.

 

Hosting a Single Site

The NGINX stores the site configuration in a server block also known as a virtual host. The server block is a configuration file that essentially defines how traffic to a specific domain will be handled. The NGINX web server is pre-configured for a single site. It can be utilized in its current form or customized based on your specific preferences and requirements.

First of all, let’s understand the default server block located in /etc/nginx/sites-enabled/default.

sudo nano /etc/nginx/sites-enabled/default

As can be seen in the above output, the default server block is serving the site from the /var/www/html directory.

To set up hosting for a single website, you can place all the necessary files and content of your website into the designated directory located at /var/www/html on your server. Alternatively, you have the option to create a custom directory where you can organize and store your website files. For example, if you want to host www.mywebsite.com, and set /srv/mywebsite/html as a serving directory then you need to modify the default server block configuration file.

Go through the steps given below to configure NGINX for a single site hosting.

Step 1: Create the Website Directory and Files

Create a directory structure using mkdir command.

sudo mkdir -p /srv/mywebsite/html

Assign the owner with the necessary permissions to have full control over accessing, modifying, and running the associated content as needed.

sudo chmod -R 755 /srv/mywebsite/html

Now, create a sample index.html page in the /srv/mywebsite/html directory.

sudo nano /srv/mywebsite/html/index.html

And add the following code to it.

<html>

 <head>

 <title>Welcome to My Web Site!</title>

 </head>

 <body>

 <h1>The domain set up is working!</h1>

 </body>

</html>

Before proceeding, save the file with the .html extension.

Step 2: Modify the Server Block File

Now, open the default server block file sudo nano /etc/nginx/sites-enabled/default and make the following changes.

  • Set root to /srv/mywebsite/html
  • Set index to index.html; the file created in the /srv/mywebiste/html directory
  • Set server_name to mywebsite.com and www.mywebsite.com

Reload the NGINX server after modifying the server block file.

sudo systemctl restart nginx

Step 3: Open the Hosted Web Page in the Browser

Next, launch your web browser and enter the server’s IP address into the address bar to access the hosted webpage, or simply type localhost.

http://localhost

Note: To open the hosted page using www.mywebsite.com in the browser, you need to modify the Linux /etc/hosts file.

<h2=”post-15372-bookmark=id.hy5l6cef7lhu”>Hosting Multiple Sites

To host multiple sites using the NGINX web server, you need to create a separate server block file in the /etc/nginx/sites-available/ directory alongside the default.

Go through the steps given below to configure NGINX for hosting multiple sites.

Step 1: Create the Website Directory and Files

Let’s create a directory to serve the second website www.mysecondwebsite.com files and give the owner read, write, and execute permissions.

sudo mkdir -p /srv/mysecondwebsite/html

sudo chmod -R 755 /srv/mysecondwebsite/html

Create an index.html file in the directory.

sudo nano /srv/mysecondwebsite/html/index.html

And add the following code.

<html>

 <head>

 <title>Welcome to My Web Site!</title>

 </head>

 <body>

 <h1>The second domain set up is working!</h1>

 </body>

</html>

Save the file and exit the editor.

Step 2: Modify the Server Block File

Now, create the server block file with the name of your domain.

sudo nano /etc/nginx/sites-available/mysecondwebsite

You can also assign a different listening port; 80 is the default listening port for HTTP, while 443 is for HTTPS.

Step 3: Creating a Symlink

Now, create a symlink of the configuration file mysecondwebsite in the /etc/sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/mysecondwebsite /etc/nginx/sites-enabled/

Note: Step 3 is very crucial to properly set up the NGIX for multi-site hosting.

You are now able to verify the functionality of the website by accessing it through your web browser.

<h2=”post-15372-bookmark=id.h0iovkm3su6c”>Disable A Hosted Website

To disable the hosted website, you can remove the server block symlink file from the /etc/nginx/sites-enabled directory.

For example, to disable the mysecondwebsite, use the following command:

sudo rm /etc/nginx/sites-enabled/mysecondwebsite

After removing the file, the site will no longer be accessible.

 

Understanding Important NGINX Files

To further configure the NGINX web server, you need to understand key files and directories associated with the server.

All the NGINX server files are located in /etc/nginx/ directory.

The nginx.conf is the main configuration file that is used to modify the global settings of the server.

The sites-available is a directory that contains all the server blocks of the websites the server is hosting. By default, the NGINX does not use these server block files unless they are linked in the sites-enabled directory.

The sites-enabled is also a directory that holds the symbolically linked files of the server blocks present in the sites-available directory.

 

Installing NGINX Modules

To extend the functionality of the NGINX server, there are many official and third-party modules available. Many core modules are pre-built in the default server. However, there are tons of other modules that add new features to the servers.

There are two types of NGINX modules:

  • Static
  • Dynamic

To install the static module, you need to compile it into the NGINX code and rebuild it. On the other hand, the dynamic modules can be installed at any time.

In this following guide, I will cover how to install the dynamic module.

To install the NGINX dynamic modules, follow the steps given below:

1. First check the version of the installed NGINX.

nginx -v

The output indicates that the NGINX version 1.18.0 is running.

2. Now, you can proceed to download the source code of NGINX from the given link. Make sure the source code version matches your installed version.

Or download it from the terminal using the wget command. For version 1.18.0, I will use the following command.

wget -O nginx-1.18.0 https://nginx.org/download/nginx-1.18.0.tar.gz

3. Extract the NGINX source code files.

tar -xvf nginx-1.18.0

It will create a directory nginx-1.18.0; navigate to this directory.

cd nginx-1.18.0

4. Now, run the configure file with the –help option.

./configure --help

In the output you will see many dynamic modules that can be installed. Let’s install image_filter_module.

./configure --with-http_image_filter_module=dynamic

While installing the module, you may encounter an error saying the GD library is required.

To fix it, install the required library.

sudo apt install libgd-dev

5. Compile the NGINX server with the enabled module.

make

A .so file will be created and saved in the objs directory.

6. Copy the module file to the nginx modules directory.

sudo cp objs/ngx_http_image_filter_module.so /usr/share/nginx/modules

7. To enable the module open the NGINX configuration file.

sudo nano /etc/nginx/nginx.conf

After making changes, reload the nginx to apply the updated settings and to ensure that the modifications take effect.

sudo systemctl restart nginx.service

The dynamic module for NGINX is installed.

 

NGINX vs Apache

The key differences between NGINX and Apache are given below:

NGINX Apache
Use single thread to handle multiple connection Use multiple threads for multiple connections
Preferred for websites that demand high-traffic Preferred when shared hosting is required
Support asynchronous processing Does not support asynchronous processing
Less resource-intensive Consumes more CPU
Less compatible with operating systems like Windows Fully compatible with all major operating systems

Is NGINX Better than Apache?

When considering performance, scalability, responsiveness, and efficiency, NGINX is widely regarded as superior to the Apache web server. It is less resource-intensive and particularly preferred when high performance is required.

Conclusion

NGINX is conveniently accessible from the default Ubuntu repository, allowing for quick installation using the APT package manager. It can easily be configured for single and multiple website hosting. Moreover, to enhance the basic functionality of the server, NGINX also offers many third-party modules. Choosing between NGINX and Apache depends upon various factors. If you require efficacy and high performance then go with the NGINX. On the other hand, Apache offers more compatibility, quick shared hosting, and third-party modules.

Print Friendly, PDF & Email
Categories