The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is extremely powerful and flexible.Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host. In this article, we are going to brief what is virtual hosting in Apache, and how to set up Apache Virtual Hosts.

What is Virtual Hosting in Apache?

Apache Virtual Hosts (Vhost) are used to run more than one web site domain using a single IP address. In other words you can have multiple web sites domains but a single server. Different sites will be shown depending on the user’s requested URL. Best part is you can have any number of virtual hosts in a single server. It simply means you can have any number of web sites domains in a single server.

Step 1 : Create the Directory Structure

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

Configure Apache Virtual Hosts In Ubuntu

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory. We will create a directory here for both of the virtual hosts we plan on making.

Within each of these directories, we will create a public_html folder that will hold our actual files. This gives us some flexibility in our hosting.

Step 2

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

The USER variable will take the value of the user you are currently logged in as when you press Enter. By doing this, our regular user now owns the public_html sub directories where we will be storing our content.

Step 3

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called dont.f that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.

The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRoot directive to reflect the directory we created, hope you get a useful information on what is virtual hosting in Apache.