How To Setup Apache Virtual Host Configuration (With Examples)

Using Apache Virtual Host, you can run several websites on the same server.
For example, I can run both and on a single physical server that has one Apache webserver running on it.

Fig: Apache Virtual Host (Multiple websites, one Apache)

There are two types of Apache virtual host configurations: 1) IP-Based Virtual Host and 2) Name-based Virtual Host. Name-based virtual host is recommended for most scenarios.

IP-Based Virtual Host

In this configuration, when you are pointing two websites (with different ip-address) to the server that runs Apache, that physical server should have two different ip-address configured.
This means that the server should have two ethernet cards, each one of them configured to the ip-address of the corresponding website that Apache virtual host will be serving. So, this is not practical for most aspects, and you should not be using this.
In the following example, the server contains two NIC cards, one is configured with ip-address for, another is configured with for Both these ip-address are served by a single Apache webserver running on that server using IP-Based virtual host.

Fig: Apache IP-Based Virtual Host

Name-Based Virtual Host

In this configuration, when Apache webserver receives a request, it looks for the hostname in the HTTP header, and depending on the hostname, it servers different websites. This is very easy, as you need only one ip-address on that physical server; but, you update the DNS with multiple website names pointing to the same ip-address. For all practical purpose, you’ll be using only Name-based virtual host configuration.
In the following example, the server contains only one NIC card, which is configured with ip-address. The DNS entry for both and website points to ip-address. When Apache recives a request, it looks for the hostname entry in the HTTP header, and serves the corresponding website.

Fig: Apache Name-Based Virtual Host

1. Uncomment httpd-vhosts.conf in httpd.conf

If you’ve installed Apache 2 from source, by default, the following line will be commented in the httpd.conf file. Uncomment this line.
# vi /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf

2. Setup virtual hosts

Modify the httpd-vhosts.conf as shown below to setup named-based virtual host setting for two hosts.
  • NameVirtualHost *:80 – Indicates that all the name-based virtual hosts will be listening on the default port 80
  • – Enclose all the apache configuration parameters for each and every virtual host between these VirtualHost tags. Any apache directives can be used within the virtualhost container.
  • In the following example, we are setting up virtual host for and listening on the same port 80. So, there will be two , one for each website.
  • When you go to, the files under /usr/local/apache2/docs/thegeekstuff will be served by Apache; and the access_log and error_log for this site will go under /usr/local/apache2/logs/thegeekstuff
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80

DocumentRoot "/usr/local/apache2/docs/thegeekstuff"
ErrorLog "logs/thegeekstuff/error_log"
CustomLog "logs/thegeekstuff/access_log" common

DocumentRoot "/usr/local/apache2/docs/top5freeware"
ErrorLog "logs/top5freeware/error_log"
CustomLog "logs/top5freeware/access_log" common

3. Check VirtualHost Configuration Syntax

Verify virtual configuration syntax using “httpd -S” as shown below. When everything is setup properly, it just displays “Syntax OK”.
# /usr/local/apache2/bin/httpd -S
VirtualHost configuration:
Syntax OK
When something is not configured properly, it will display warning message, including “directory does not exit” message as shown below.
# /usr/local/apache2/bin/httpd -S
Warning: DocumentRoot [/usr/local/apache2/docs/top5freeware] does not exist
Warning: ErrorLog [/usr/local/apache2/logs/thegeekstuff] does not exist
Syntax OK

4. Restart the Apache and test

# /usr/local/apache2/bin/apachectl restart
Now, when you go to (or, the apache will serve the files from /usr/local/apache2/docs/thegeekstuff directory.
When you go to (or, the same apache running on the same server will serve the files from /usr/local/apache2/docs/top5freeware directory.
Just to reiterate, for the name-based virtual host to work properly, the DNS entry for both these websites should be pointing to the same external ip-address of the physical server where the Apache webserver is running.

Linux Sysadmin CourseLinux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!