How to Configure Virtual Hosts For Apache Part 1: Hosting 101

5th April, 2016 by
One of the most common tasks to perform when using Apache to serve websites on a dedicated server or VPS is to set up virtual hosts. This is the method by which Apache is able to present different websites to users depending upon which domain name they entered into their web browser. In this article, I will be explaining how to create a virtual host in Apache as installed via the default repositories in Debian/Ubuntu or CentOS/Red Hat.

Getting Started

The standard configuration is intended for virtual host configuration files to be placed within the /etc/apache2/sites-available directory in Debian and Ubuntu and the /etc/httpd/conf.d directory on CentOS/Red Hat. These files can then be enabled, which will then allow them to be read by Apache when restarted or is reloaded. On Debian and Ubuntu, the system will start with a default configuration, imaginatively named 000-default.conf which contains the configuration that Apache uses when it is first installed, so you could copy and edit this file to get started. CentOS and Red Hat don’t really have a default virtual host configuration file, so you’ll be starting from scratch with your virtual host configuration in a blank file.  

Whilst you can name your virtual host configuration files anything you like, I’d generally recommend naming the file using the main domain name of the site you will be hosting. If you were hosting on your website, I’d name the file to make it clear which virtual host is defined inside. The configuration in these files is broken down into lines of configuration that Apache calls directives.

Next, we’ll look at some of the more commonly used directives when setting up virtual hosts:

<VirtualHost [addr[:port]]> … </VirtualHost>


This directive is used to group all of the configuration specific to a particular virtual host. The addr and port sections in square brackets are optional and are used to specify the exact IP address and port on the server that the incoming connection should arrive to in order for Apache to respond with that virtual host. If neither is provided then Apache will use this virtual host regardless of IP or port used to connect. Those familiar with HTML will probably realize this already, but the two sections of this directive each contained within angle brackets should be separated to show the start and end of the virtual host’s directives.

<VirtualHost *:80>

    … More directives here …


In the example above Apache will see the virtual host configuration as valid for any IP address on the server where the connection is to port 80. Generally, for a configuration file for a virtual host within the sites-available directory, it would be expected for the file to start with the opening <VirtualHost> directive and end with the last line being the closing </VirtualHost> directive.

The next directive to look at is the ServerName directive, which is used to identify the domain name that Apache will use the virtual host configuration for, a couple of examples of which are below:



The first example would match the domain name, the second would match the subdomain Note that these will be an exact match, so while the first one would match for someone browsing to, it wouldn’t match for someone browsing to If you want to match multiple domain names for a single virtual host then you need the ServerAlias directive. You can list multiple domain names separated with spaces in a single ServerAlias directive or alternately you can have multiple ServerAlias directives.  Another good thing with the ServerAlias directive is that * can be used to denote wildcards:


ServerAlias *


So with the example directives above, if all three lines were included in a virtual host then that would match for, every subdomain of and also and

That’s about all we have time for in this part – but come back for the next part where we will be looking at the rest of the commonly used directives for setting up virtual hosts.