Add an Apache’s Virtual Host

What is an Apache’s Virtual Host?

One of the main advantages of Apache is that it can be used either as and standalone server, that is, to store and serve only a single website or application or as a multiple sites server, that is, storing and serving several sites at the same time, each one of them is called a Virtual Host.

These Virtual Hosts can have its own Domain Name, let’s say:

  • example1.com
  • example2.com
  • exampleN.com

Or another possibility is to have them all as subdomains, let’s say:

  • subdom1.example.com
  • subdom2.example.com
  • subdomN.example.com

Adding an Apache’s Virtual Host

Adding an Apache Virtual Host is a quite straightforward process if you can confirm the following prerequisites:

  • Your Apache web server is configured to run Virtual Hosts
  • You are able to add a new Domain/Subdomain (DNS settings)

Apache Virtual Hosts readiness

Is your Apache’s web server ready for the addition of a new Virtual Host? How can you tell if your Apache’s web server is or isn’t configured to serve multiple Virtual Hosts?

The easy way to verify if your Apache’s web server is configured to serve Virtual Hosts is to check the folder /etc/httpd/conf.d/ to see if you can find there one or more Virtual Host files (*.conf), see below image:

/etc/httpd/conf.d/

linux directory tree apache virtual host file

Note: It is possible that along with the Virtual Hosts files you can also find the respective SSL files, on which the location for the SSL Certificate, Key file and Chain file are defined, we are going to cover the SSL configuration in another article.

Example of a typical Virtual Host file

Just as an example, see below screenshot to have an idea of how they look like, they are just small text files containing the necessary settings to define each Virtual Host.

virtual host file example

Download Virtual Host template

In case you need a Virtual Host template, feel free to download the following one:

What’s inside the .conf file?

A record and C-Name

As you can see inside the “.conf” file, the parameter called ServerName is actually the site name, in DNS terms called “Domain Name” or just “Domain”, depending on each case this can be also one particular “subdomain”, this parameter in DNS terms is known as an “A” record.

The ServerAlias parameter or simply “alias” is what in DNS terms is called CNAME, the most common and well known alias is the “www”, do not worry much about all of this, we are going to configure both parameters when covering the DNS part.

Virtual Host related folders

Two important folders are also defined inside each virtual host file, the main folder called “DocumentRoot” where all the website files will be located (html, javascript, css, php, images, etc).

  • /var/www/example.com/public_html

and the “Logging folder” where the respective site error log files will be saved.

  • /var/log/httpd

Apache Virtual Hosts ready

If you can find the Virtual Host file and also its respective folder and log files, that means your Apache web server is serving virtual hosts and now you can move forward to add a new one, of course we are going to guide ourselves using an existing virtual host to do a sort of mirroring the configuration.

Where to go next?

The next step is to create a new Virtual Host file (.conf)