Configure Apache Virtual Host on Ubuntu 20.04 LTS

In this tutorial, we are going to configure Apache virtual host on Ubuntu 20.04 LTS.

What is a virtual host? And its uses.

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.

Prerequisite:

Here we used ubuntu 20.04 LTS for this experiment, It also applicable for 16.04 and higher versions.

Steps to configure Apache Virtual Host:

  • Open up your terminal and update the package list
  • Install Apache Server
  • Creating Virtual Host Users
  • Creating Index file
  • Configure Virtual Host Directives
  • Add Local DNS Resolver
  • Test Virtual Host 

Step-1) Update the package list

First thing is first, do update your package list. Go ahead and type

 sudo apt update

Step-2) Install Apache HTTP Server

After updating the package list, you need to be installed apache into your system.

sudo apt install apache2

After installing Apache, you could check it perfectly running or not by using the service command

 service apache2 status

Step-3) Creating User for Virtual Host

 Now 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 userA
sudo useradd -m userB

Here 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_html
sudo mkdir /home/userB/public_html

Now we have a public_html directory under each user’s directory.

Step-4) Creating Index File

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.html

And put this markup text and save the file.

<html>
   <head>
      <title>Welcome to abc.com</title>
   </head>
   <body>
      <h1>Hello! There welcome to abc.com page</h1>
   </body>
</html>

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.

Step-5) Creating Apache Virtual Host Config

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.

<VirtualHost *:80>
 ServerAdmin abc@abc.com
 ServerName abc.com
 ServerAlias www.abc.com
 DocumentRoot /home/userA/public_html/
<Directory /home/userA/public_html/>
 Options Indexes FollowSymLinks
 AllowOverride None
 Require all granted
</Directory>
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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 abc@abc.com
 ServerName abc.com
 ServerAlias www.abc.com
 DocumentRoot /home/userA/public_html/
<Directory /home/userA/public_html/>
 Options Indexes FollowSymLinks
AllowOverride None
Require 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.com
sudo a2ensite xyz.com

And finally restart the apache, type

sudo systemctl restart apache2.

Step-6) Add Local DNS Resolver

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/hosts

And add the following lines at the end

127.0.0.1   abc.com
127.0.0.1   xyz.com

Step-7) Test your Virtual Host

Finally, test your settings, open up the web browser and open xyz.com and you could see the web page serving from xyz vhost.

Apache Virtual Host

Also, open another tab aside and type abc.com and this page serving form abc vhost.

apache vhost

You could follow 7 best practices for Apache security:

  1. Keep your Apache updated with the latest releases and patches
  2. Disable Directory Listing
  3. Disable unnecessary modules
  4. Turn off unnecessary services
  5. Ensure that Apache server-info is disabled
  6. Disable Trace HTTP Request
  7. Distribute the ownership and don’t run Apache as ‘root’

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.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

2 Comments

  • can you add a virtual host for a directory placed in another partition of ubuntu 20.04 installation, not working for me.

    • Either you can mount your external drive to document root directory or you may set proper permission to your external drive like that:
      You must change the owner of /media/your_location as follows:
      sudo chown -R www-data:www-data /media/your_location

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.