Understand And Setup Apache Virtual Host In Linux

The Apache web server allows you to run more than one website on a single machine. This is possible through virtual hosting. Virtual hosting reduces the cost of website hosting. This article describes how virtual hosting can be configured in Apache Web Server on a RHEL machine.

Apache Web Server

To install apache in you system, you need to install "httpd" package. If you want to compile and install additional modules for Apache, then you need httpd-devel package. The httpd-manual package provides complete manual and reference guide for Apache. Having installed required packages, we are now ready to configure and run Apache web server.

The Apache web server works on port 80 (http) and 443 (https) by default. The default location of placing the website content is /var/www/html/ directory, i.e. by default, website stored in this directory will be displayed on the web server.

The configuration file for Apache is "/etc/httpd/conf/httpd.conf". This file has three sections:

1. Global Environment - This section controls the overall operation of Apache.

2. 'Main' Server Configuration - Here, the default values for server are defined, that can be overridden by virtual hosts

3. Virtual Hosts - The directives controlling the behavior of virtual hosts.

We need to be concerned about the third section only. Let us see the default configuration for this section.

### Section 3: Virtual Hosts
# VirtualHost: If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
# You may use the command line option '-S' to verify your virtual host
# configuration.

# Use name-based virtual hosting.
#NameVirtualHost *:80
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#<VirtualHost *:80>
# ServerAdmin [email protected]
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common

Virtual Hosting

The virtual hosts can either be name based or IP based. Name based virtual hosts are also called 'host-based' or 'non-IP virtual hosts'.

Name Based Virtual Hosts

In name based virtual hosting technique, different host share the same IP address. With this technique, the name of the host is sent as the part of HTTP header. The DNS server maps the names with the correct IP address.

For name based virtual hosting, an IP address must be configured to accept the requests for different hosts. In the above file, the directive 'NameVirtualHost' is used for this configuration. This directive must be set for name based virtual hosting. To configure the IP address for this purpose, use the following.


If the server has only one IP address or all the IP addresses are to be configured for name based virtual hosting, then * can be assigned for the IP address:

NameVirtualHost *:80

Note that this directive is commented by default. So remember to remove the leading '#' to uncomment the line. Adding this line will prepare Apache for accepting requests for name based virtual hosting.
Now we need to configure directives for individual hosts.  The virtual hosts are configured with "<VirtualHost>" directive. In the above file, the last tag is "<VirtualHost>" tag:

#<VirtualHost *:80>

#    ServerAdmin [email protected]

#    DocumentRoot /www/docs/dummy-host.example.com

#    ServerName dummy-host.example.com

#    ErrorLog logs/dummy-host.example.com-error_log

#    CustomLog logs/dummy-host.example.com-access_log common


Any set of directives for virtual hosts must be enclosed between "<VirtualHost IP_addr:[port]>" and "</VirtualHost>" (like in the example above). As we are configuring the host for name based virtual hosting, so the address in this directive must match with NameVirtualHost entry. As in our example, the IP address is, so here, the entry will be written as:



#Other directives required for virtual hosts.



Now let us discuss the other directives used for the virtual hosts.

• ServerName specifies the FQDN (Fully Qualified Domain Name) of the host. The server will serve for this name to the clients (i.e. URL of the website). An optional number can also be provided.

• DocumentRoot is the directory (tree) where the website files are placed ('index.html' file goes in this directory). Note that this location must not contain the trailing forward slash (i.e. the location must be given as "/var/www/html"; not as "/var/www/html/").

• ErrorLog is the location where errors will be stored.

• ServerAdmin is the email address that the server returns to the client in case of some error.

IP Based Virtual Hosting

For IP based virtual hosting, the server needs different IP addresses for each individual host. The NameVirtualHost directive will be absent in this case. The VirtualHost directive will contain different IP addresses corresponding to each host. Rests of the directives are similar. For example:


#DocumentRoot /var/www/site1

#ServerName server1.example.com

#ErrorLog /var/logs/site1/error_log




#DocumentRoot /var/www/site2

#ServerName server2.example.com

#ErrorLog /var/logs/site2/error_log


Raghu 3:24 pm

About Raghu

Raghu is working as Linux Server Administrator in Acknown Technologies Pvt. Ltd. He has been using Linux from last 5 years. He completed his RHCE certification in 2009. He likes to read about Linux and other Open Source Technologies and write articles on these.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.