Configuring Virtual Hosts in Apache 2.x

Hello friends. Today I am going to tell you about configuring Virtual Hosts in Apache. Firstly, you should know that what are Virtual Hosts? If not, below is definition from Wikipedia:

Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently.

In simple words, If you want to host multiple websites (domains) on a single server then Virtual Hosting is what you need !!. Lets have some discussion about Virtual hosts, if you don’t like theory you can skip directly to configuration part.

Types of Virtual Hosts:

Virtual hosts can be of two types:

  1. Name-based Virtual Hosts
  2. IP based Virtual Hosts.

Name-based Virtual Hosts:

Name-based virtual hosts use multiple host names for the same webserver IP address. Using this method you can host multiple domains (different host names), even if your server has only one Network Interface Card (NIC) i.e one IP. Web server is configured to provide different website based on host name requested by user. If the user requests directly using IP address of server, then default website is provided.

IP-based Virtual Hosts:

In IP-based Virtual Hosts, each site points to a unique IP address. IP-based Virtual Hosting is useful if your server has more than one NIC /IP’s. In that case you can configure your web server to host different site based on the IP address for which request is coming. This type is Virtual hosting is very rarely used.

Name-based Virtual Hosts Configuration:

Here, I am explaining configuration for Apache 2.x. If you are not using Apache as web server then rest of the post is not useful to you. To configure Name-based Virtual Hosts open your server’s httpd.conf file. Search and uncomment the following line is not already uncommented.

NameVirtualHost *:80

If you are unable to find above line in your configuration then it may be possible that Virtual Hosts settings are defined in separate file. Take a look at include directives defined in the httpd.conf file and try to find the right file. For example- In my case, I found the following line in my httpd.conf file:

Include conf/extra/httpd-vhosts.conf

Add or edit rest of configuration at appropriate place according to your httpd.conf. Note that Virtual Hosts in Ubuntu are handled differently, if you install Apache using Ubuntu repositories. I will explain configuration for Ubuntu later in this topic.

For default website, place its contents under Document Root of your web server. You do not need to configure Virtual host for default website. For websites other that default, enter the following lines in appropriate file.

<VirtualHost *:80>
 DocumentRoot "/var/www/drupal"
 ErrorLog "logs/"
 CustomLog "logs/" common

Here, I am using as host name. Change it to your host name. Also, change the value of DocumentRoot to the path of your Virtual Host web directory i.e directory which you want to server when request comes from the name defined as ServerName. Restart Apache for the configuration to take effect.

IP-based Virtual Hosts Configuration:

For IP-based Virtual Hosts, add following lines in the appropriate file (see above section for selecting file).

DocumentRoot "/var/www/example1"
ErrorLog "logs/"
CustomLog "logs/" common

Do not forget to replace host name and DocumentRoot with your own host name. You can also use IP address in <VirtualHost > directive here. Then restart Apache.

Virtual Hosts configuration in Ubuntu:

If you are using Ubuntu Linux distribution and Apache is installed using Ubuntu repositories. Then to configure Virtual Hosts you have to follow following steps:

  • Open /etc/apache2/apache2.conf file. Be sure that following line is uncommented under Virtual Hosts section.
Include /etc/apache2/sites-enabled/
  • Then create a separate file for each Virtual Host you want to configure in the /etc/apache2/sites-enabled/ folder.
  • Write Virtual Host configurations for each Virtual Hosts in their respective files. Configuration is same as discussed in above sections.
  • Restart your webserver. Following command can do it.
$sudo /etc/init.d/apache2 restart


2 thoughts on “Configuring Virtual Hosts in Apache 2.x

  1. Good day I was luck to seek your theme in google
    your post is splendid
    I get much in your website really thanks very much
    btw the theme of you website is really magnificentsuper
    where can find it

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s