In this tutorial, we are going to configure Apache virtual host on Ubuntu 20.04 LTS.
Well, the term Virtual Host refers to the practice of running more than one web site such as company1.example.com and company2.example.com and so on within a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “name-based”, meaning that you have multiple names running on the same IP address.
So, the fact is that they are running on the same physical server but it is not apparent to the end-user. Means Company1 does not have access to Company 2’s files and whatever inside going on. Thus, it secure and reliable.
In the real world, we often called this method shared web hosting. The price for shared web hosting is much lesser than a dedicated web server because many customers can be hosted on a single server.
VIDEO
Here we used ubuntu 20.04 LTS for this experiment, It also applicable for 16.04 and higher versions.
First thing is first, do update your package list. Go ahead and type
sudo apt updateAfter updating the package list, you need to be installed apache into your system.
sudo apt install apache2After installing Apache, you could check it perfectly running or not by using the service command
service apache2 statusNow we are going to create two users for this experiment one is userA and other is userB. Let’s go and type
sudo useradd -m userAsudo useradd -m userBHere the flag -m will Create the user’s home directory if it does not exist. Now create a directory named public_html inside each user’s directory. Go ahead and type
sudo mkdir /home/userA/public_htmlsudo mkdir /home/userB/public_htmlNow we have a public_html directory under each user’s directory.
Now you need to put some markup text inside public_html to identify your virtual host. So, we are gonna create an index.html file under public_html directory. Go ahead and type
sudo nano /home/userA/public_html/index.htmlAnd put this markup text and save the file.
And similarly, do the same for user B.
sudo nano /home/userA/public_html/index.html<html> <head> <title>Welcome to xyz.com</title> </head> <body> <h1>Hello! There welcome to xyz.com page</h1> </body></html>Now each user had their index file ready to serve over apache.
Now it’s time to create vhost config file for each user, go ahead and type
sudo nano /etc/apache2/site-available/abc.com.conf.So, now you need to tell apache about your vhost and its configuration. Actually, these are called tags and directives.
So, roughly this is a standard Virtual Host structure which I made here, and my recommendation is first to understand your needs and then place the configuration. Do not blindly copy-paste from any tutorial in your production server. This can bring disaster at any time.
Or you could learn more about directives and Virtual host examples at apache documentation just open up the documentation.
Now we are going to create another Virtual host configuration for xyz.com
sudo nano /etc/apache2/site-available/xyz.com.conf.<VirtualHost *:80> ServerAdmin [email protected]com ServerName abc.com ServerAlias www.abc.com DocumentRoot /home/userA/public_html/<Directory /home/userA/public_html/> Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>So, after all this stuff you need to enable your sites. Go ahead and type
sudo a2ensite abc.comsudo a2ensite xyz.comAnd finally restart the apache, type
sudo systemctl restart apache2.Now, one last thing you needed is that a local DNS resolver as we are in a local environment, so, edit your hosts file using nano editor and add a local DNS record for each host. And if you are in a production environment then point your public IP to your DNS manager.
sudo nano /etc/hostsAnd add the following lines at the end
127.0.0.1 abc.com127.0.0.1 xyz.comFinally, test your settings, open up the web browser and open xyz.com and you could see the web page serving from xyz vhost.
Also, open another tab aside and type abc.com and this page serving form abc vhost.
Hope this tutorial helpful to you. Leave a comment if you have any questions. Also, click on subscribe button to encourage us and get the latest update. Thank you.
I like Programming and New Technologies. And work with Linux.
PREV: VNC Connect Error Messages
NEXT: Connect to a Report Server in Management Studio - SQL Server ...