Linux

How To Configure Nginx Server Blocks on Ubuntu 24.04 LTS

Configure Nginx Server Blocks on Ubuntu 24.04

In this tutorial, we will show you how to configure Nginx server blocks on Ubuntu 24.04 LTS. Nginx, a powerful and flexible web server, has gained immense popularity due to its high performance, scalability, and ease of configuration. One of the key features of Nginx is its ability to host multiple websites on a single server using server blocks. Server blocks, also known as virtual hosts, allow you to define separate configurations for each website, enabling you to host multiple domains or subdomains on the same server. I

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step set up Nginx server blocks on Ubuntu 24.04 (Noble Numbat). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies.
  • An Ubuntu 24.04 system with root access or a user with sudo privileges.

Configure Nginx Server Blocks on Ubuntu 24.04 LTS Noble Numbat

Step 1. Updating the Package Repository.

First, it’s crucial to update your Ubuntu system to the latest version. This ensures that you have access to the most recent packages, bug fixes, and security updates. Open a terminal window and run the following commands:

sudo apt update
sudo apt upgrade

The sudo apt update command refreshes the package list, fetching information about the newest versions of packages and their dependencies. This step helps the package manager determine which packages need to be upgraded.

Step 2. Installing Nginx.

If you haven’t already installed Nginx, you can do so by running the following commands:

sudo apt update
sudo apt install nginx

After the installation is complete, verify that Nginx is running by checking its status:

sudo systemctl status nginx

You should see output similar to this:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2024-05-17 12:34:56 UTC; 182 days ago
       Docs: man:nginx(8)
   Main PID: 2341 (nginx)
      Tasks: 2 (limit: 1137)
     Memory: 3.5M
        CPU: 36ms
     CGroup: /system.slice/nginx.service
             ├─2341 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─2342 nginx: worker process

Step 3. Create Directory Structure.

Nginx server blocks are typically stored in the /etc/nginx/sites-available directory. To keep your configuration organized, create a directory for each website you want to host.

For example, let’s say we want to host two websites: idroot.us and idroot.net. We’ll create directories for each site as follows:

sudo mkdir -p /var/www/idroot.us/html
sudo mkdir -p /var/www/idroot.net/html

Replace idroot.net and idroot.us with your actual domain names. The /html suffix is where you’ll place your website files.

Set the appropriate permissions for the directories:

sudo chown -R www-data:www-data /var/www/idroot.us
sudo chown -R www-data:www-data /var/www/idroot.net
sudo chmod -R 755 /var/www

These commands assign the www-data user and group ownership to the website directories and set the permissions to allow Nginx to read and execute files.

Step 4. Create Server Block Configuration Files.

With the directory structure in place, it’s time to create the server block configuration files for each website. Server block configuration files are stored in the /etc/nginx/sites-available directory.

Let’s create a configuration file for idroot.us:

sudo nano /etc/nginx/sites-available/idroot.us

Add the following configuration:

server {
    listen 80;
    server_name idroot.us www.idroot.us;
    root /var/www/idroot.us/html;
    index index.html index.htm;

    access_log /var/log/nginx/idroot.us.access.log;
    error_log /var/log/nginx/idroot.us.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

Replace idroot.us with your actual domain name and update the root directive to match the directory you created in the previous step.

The configuration includes the following directives:

  • listen: Specifies the port on which Nginx will listen for incoming HTTP requests (80 for non-SSL, 443 for SSL).
  • root: Sets the document root directory for the website.
  • index: Specifies the default index files to be served when a directory is requested.
  • server_name: Defines the domain names associated with the server block.
  • location: Configures how Nginx should handle requests for specific URIs.

Repeat this process for each website you want to host, replacing the domain names and document root directories accordingly.

To enable a server block, create a symbolic link from the configuration file in /etc/nginx/sites-available to the /etc/nginx/sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/idroot.us /etc/nginx/sites-enabled/

The symbolic links in the sites-enabled directory tells Nginx which server blocks should be active and served to visitors. By creating these links, we ensure that Nginx loads the configuration files during startup or when the service is reloaded.

Before restarting Nginx, it’s essential to test the configuration for any syntax errors:

sudo nginx -t

If the configuration is valid, you’ll see output similar to this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once the configuration test passes successfully, it’s time to restart Nginx to apply the changes. To restart the Nginx service, run the following command:

sudo systemctl restart nginx

To verify that the server blocks are working correctly, open a web browser and navigate to your domain(s). You should see the default Nginx welcome page or the content you’ve placed in the document root directory.

If you encounter any issues, check the Nginx error log for more information:

sudo tail -n 20 /var/log/nginx/error.log

Step 5. Configure Firewall.

If you have a firewall enabled on your server, make sure it allows incoming HTTP and HTTPS traffic. If you’re using ufw (Uncomplicated Firewall), you can enable the necessary ports with the following commands:

sudo ufw allow 'Nginx Full'
sudo ufw reload

This command allows both HTTP (port 80) and HTTPS (port 443) traffic through the firewall.

Verify the firewall rules by running:

sudo ufw status

You should see the allowed ports and services in the output.

Step 6. Secure Nginx with SSL/TLS.

To enhance the security of your websites, it’s recommended to enable SSL/TLS encryption. You can obtain a free SSL certificate from Let’s Encrypt using Certbot:

sudo apt install certbot python3-certbot-nginx

Next, run Certbot to obtain and install the SSL certificate for your website:

sudo certbot --nginx -d idroot.us -d www.idroot.us

Replace idroot.us with your actual domain name. Certbot will automatically obtain the certificate, configure Nginx to use it, and set up automatic renewal.

Step 7. Verify Server Block Setup.

With the server blocks configured and Nginx restarted it’s time to verify that your websites are accessible and functioning correctly. Open a web browser and enter the domain name of one of your websites, such as https://idroot.us. You should see a secure padlock icon in the browser’s address bar, indicating that the connection is encrypted. If the server block is set up correctly, you should see the website’s content displayed.

If you encounter any issues, such as a “404 Not Found” error or a default Nginx page, double-check the following:

  • Ensure that the domain name is correctly pointed to your server’s IP address.
  • Verify that the server block configuration files are properly configured and enabled.
  • Check the Nginx error logs (/var/log/nginx/error.log) for any specific error messages.

Congratulations! You have successfully set up Nginx vHost. Thanks for using this tutorial to configure Nginx server blocks on the Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button