How to Create Virtual Host in Ubuntu 22.04?

By Hardik Savani May 1, 2024 Category : Ubuntu

Hello Friends,

Now, let's see an example of how to create virtual host in ubuntu 22.04. you can see how to create virtual host in ubuntu 20.04. we will help you to give an example of ubuntu 22.04 create virtual host. This article will gaive you a simple example of how to create virtual host in ubuntu 20.04.

Creating a virtual host in Ubuntu 22.04 involves configuring the Apache web server to host multiple websites on a single server. Here's a step-by-step guide to creating a virtual host:

Follow the following steps to create a virtual host in the Ubuntu 22.04 system.

Step 1: Install Apache (if not already installed)

Run the following command to install apache2 server.

sudo apt update

sudo apt install apache2

Step 2: Create a new directory for your website files

Here, we will create "example.test" directory.

sudo mkdir /var/www/html/example.test

Replace "example.test" with your domain or preferred website name.

Step 3: Set appropriate permissions for the directory

run following command to setup folder permission:

sudo chown -R $USER:$USER /var/www/html/example.test

sudo chmod -R 755 /var/www/html

Step 4: Create a simple HTML file for testing purposes (optional)

Here, we will create simple "index.html" file with "hello world" test. so, run the below command:

echo "<html><head><title>My Website</title></head><body><h1>Hello, World!</h1></body></html>" | sudo tee /var/www/html/example.test/index.html

Step 5: Create a virtual host configuration file

Run the below command to configure apache file.

sudo nano /etc/apache2/sites-available/example.test.conf

Replace "example.test" with your domain or preferred website name, and paste the following configuration:

<VirtualHost *:80>

ServerAdmin webmaster@example.test

ServerName example.test

DocumentRoot /var/www/html/example.test

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/html/example.test>

Options Indexes FollowSymLinks MultiViews

AllowOverride All

Require all granted

</Directory>

</VirtualHost>

Step 6: Enable the virtual host

Run the below command to enable virtual host.

sudo a2ensite example.test.conf

Step 7: Restart Apache to apply the changes

run the below command to restart apache2 server.

sudo systemctl restart apache2

Step 8: Update the hosts file (if testing on a local machine)

add host entry by following command:

sudo nano /etc/hosts

Add the following line:

127.0.0.1 example.test

Replace "example.test" with the domain you specified in your virtual host configuration.

Open a web browser and navigate to your virtual host

http://example.test

Replace "example.test" with your domain or server IP address.

Your virtual host should now be accessible, and you can customize the content in the `/var/www/html/example.test` directory to host your website files.

I hope it can help you...

Tags :
Shares