Logo
Desease Vault
web-development

How To Setup Nginx Server Blocks In Ubuntu 24.04 LTS

How To Setup Nginx Server Blocks In Ubuntu 24.04 LTS
0 views
3 min read
#web-development

Before we start, make sure your system is up to date.

Introduction

When managing multiple websites on a single server, Nginx server blocks are an efficient and powerful tool. In this guide, you’ll learn how to set up and configure server blocks on Ubuntu 24.04 LTS, ensuring you can host multiple domains seamlessly.

Step 1: Update Your System

Run the following command:

sudo apt-get update

Step 2: Install Nginx

Install Nginx by running:

sudo apt-get install nginx

Once installed, verify it’s working by accessing the default Nginx page in your browser. Open http://<your_server_IP> or http://localhost. You should see the default Nginx welcome page.

Image

Great! Nginx is installed and operational.

Step 3: Create Directories for Each Website

For this example, we’ll create two server blocks:

  1. demo1.diseasevault.cloud
  2. demo2.diseasevault.cloud

First, create a directory for the first domain:

sudo mkdir -p /var/www/html/demo1.diseasevault.cloud/public_html

Repeat the process for the second domain:

sudo mkdir -p /var/www/html/demo2.diseasevault.cloud/public_html

Next, assign ownership of these directories to your current user:

sudo chown -R $USER:$USER /var/www/html/demo1.diseasevault.cloud/public_html
sudo chown -R $USER:$USER /var/www/html/demo2.diseasevault.cloud/public_html

Finally, set the correct permissions for the directories:

sudo chmod -R 755 /var/www/html/

Step 4: Create Sample Web Pages

Let’s add some content to test the configuration. For the first domain, create an HTML file:

sudo nano /var/www/html/demo1.diseasevault.cloud/public_html/index.html

Add the following content:

index.html
<html>
  <head>
    <title>www.diseasevault.cloud</title>
  </head>
  <body>
    <h1>Hello, This is a test page for demo1.diseasevault.cloud website</h1>
  </body>
</html>
This file contains sample HTML

Do the same for the second domain:

sudo nano /var/www/html/demo2.diseasevault.cloud/public_html/index.html
index.html
<html>
  <head>
    <title>www.diseasevault.cloud</title>
  </head>
  <body>
    <h1>Hello, This is a test page for demo2.diseasevault.cloud website</h1>
  </body>
</html>

Step 5: Configure Server Blocks

Create configuration files for each server block. Start by copying the default configuration:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/demo1.diseasevault.cloud.conf
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/demo2.diseasevault.cloud.conf

Edit the first configuration file:

sudo nano /etc/nginx/sites-available/demo1.diseasevault.cloud.conf

Update the server_name and root directives to match the first domain:

demo1.diseasevault.cloud.conf
server {
  listen 80;
  listen [::]:80;
 
  root /var/www/html/demo1.diseasevault.cloud/public_html;
 
  index index.html index.htm index.nginx-debian.html;
 
  server_name demo1.diseasevault.cloud www.demo1.diseasevault.cloud;
 
  location / {
    try_files $uri $uri/ =404;
  }
}

Repeat the process for the second domain:

sudo vi /etc/nginx/sites-available/demo2.diseasevault.cloud.conf

Make the necessary changes in the server_name and root directives to reflect to the second domain name. All changes are shown in bold letters.

demo2.diseasevault.cloud.conf
server {
  listen 80;
  listen [::]:80;
 
  root /var/www/html/demo2.diseasevault.cloud/public_html;
 
  index index.html index.htm index.nginx-debian.html;
 
  server_name demo2.diseasevault.cloud www.demo2.diseasevault.cloud;
 
  location / {
    try_files $uri $uri/ =404;
  }
}

Step 6: Enable Server Blocks

Disable the default server block and enable the new ones:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/demo1.diseasevault.cloud.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/demo2.diseasevault.cloud.conf /etc/nginx/sites-enabled/

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Conclusion

Congratulations! You’ve successfully set up Nginx server blocks on Ubuntu 24.04 LTS. Your server is now ready to host multiple domains. Simply upload your content to the respective directories, and you’re good to go!