How to Create Virtual Host in apache on ubuntu

How to Create Virtual Host in apache on ubuntu

Introduction

Hello Guyz, First you need to setup lamp in your vps. Kindly click here. After setup lamp in your vps. Please follow below article.

More about LAMP :- Click Here

In this article, I will show you how to config a virtual host in ubuntu 20.04 or any VPS.

So let’s get started.

Overview

Let’s talk about what is virtual host in VPS. virtual host means is a special config in apache webserver which helps to serve multiple websites in a single server.

Lets, Move forward for How to create virtual host ubuntu 20.04

Step1. Installing Apache

First, you need to install the Apache webserver. Which is used to serve files on the internet.

$ sudo apt update
$ sudo apt install apache2

Successfully installed the apache webserver.

Note:- Replace your firstdomain.com with your first domain which color is red and seconddomain.com with your second domain which color is blue.

Step2. Creating the Directory

Why we need to create a document root or directory because that will hold the site data eg. HTML files that we will be serving to visitors.

$ sudo mkdir -p /var/www/firstdomain.com

$ sudo mkdir -p /var/www/seconddomain.com

Step3. Giving Permission In Directories

Changing the ownership

$ sudo chown -R $USER:$USER /var/www/firstdomain.com

$ sudo chown -R $USER:$USER /var/www/seconddomain.com

Setting directories, subfolders, and file permission 755 for executing the file.

$ sudo chmod -R 755 /var/www

Your web server should now have permission. It can serve content.

Step4. Creating Testing Page For Each Virtual Host

Creating demo page for firstdomain.com

$ nano /var/www/firstdomain.com/index.html

And type

<html>
<head>
<title>Welcome to First Domain</title>
</head>
</html>

In nano editor. To save the file, press CTRL + X then Y then Enter. Your File has been saved.

Creating demo page for seconddomain.com

$ nano /var/www/seconddomain.com/index.html

And type

<html>
<head>
<title>Welcome to Second Domain</title>
</head>
</html>

In nano editor. To save the file, press CTRL + X then Y then Enter. Your File has been saved.

Also Read: Earn Money Online

Step5. Creating New Virtual Host Files

Virtual host file means apache web server will respond to various domain requests.

Copying default conf file to new conf file for the first domain.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/firstdomain.com.conf

Copying default conf file to new conf file for Second domain.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/seconddomain.com.conf

After Copying the Virtual host file. Need to edit this.

Now editing the first domain conf file which you are copy above.

Opening First domain conf file

$ nano /etc/apache2/sites-available/firstdomain.com.conf

It looks similar to this.

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Need to add, edit something in the previous code or you can replace it from the below code.

ServerAdmin admin@firstdomain.com
ServerName firstdomain.com
ServerAlias www.firstdomain.com

DocumentRoot /var/www/firstdomain.com

When your editing was complete file should look like this:

<VirtualHost *:80>
ServerAdmin admin@firstdomain.com
ServerName firstdomain.com
ServerAlias www.firstdomain.com
DocumentRoot /var/www/firstdomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Just save and close the file. In nano editor. To save the file, press CTRL + X then Y then Enter.

Now similarly need to edit the second domain configuration file.

Opening second domain conf file

$ nano /etc/apache2/sites-available/seconddomain.com.conf

When your editing was complete file should look like this for the second domain configuration.

<VirtualHost *:80>
ServerAdmin admin@seconddomain.com
ServerName seconddomain.com
ServerAlias www.seconddomain.com
DocumentRoot /var/www/seconddomain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Just save and close the file. In nano editor. To save the file, press CTRL + X then Y then Enter.

Lets, Move forward for Create Virtual Host apache.

Step6. Enable the New Virtual Host Files

After all the Configuration. Need to enable conf file for each site or conf file.

First, need to disable the default configuration file.

$ sudo a2dissite 000-default.conf

After disabling the default conf file. Need to activate the new conf which we are created above.

$ sudo a2ensite firstdomain.com.conf

$ sudo a2ensite seconddomain.com.conf

When you are finished. Need to restart the apache web server.

$ sudo systemctl restart apache2

After restarting the webserver.

Your server can serve two websites on a single server.

Also Read: Create Own File Sharing Website

Conclusion

So in this article, I show you how to create a virtual host in an apache VPS/server. The main benefit of a virtual host is to serve multi websites on a single server.
If you have any doubt or find any mistake on post leave comment below or contact to contact@tipsntricks.in

Thanks For Reading 😊

Hire Us for Digital Services :- Script Installation, Website Design & Development, VPS Configuration. Telegram Contact :-@Developer107