preload preload


Manage Multiple Sites with Apache httpd using site-specific configurations

Apache httpd is a great webserver. Efficient, easy to install, easy to manage and lots of modules to play with for all your needs. Morever, it also allows you run as many websites as you want on a single server. It only takes to configure VirtualHost entry in your httpd-vhosts.conf and you are good to go.

However, with increasing number of sites, your httpd-vhosts.conf site will be cluttered if each and every one is configured in one single configuration file. Recently, I came along with the same situation. It was difficult to manage specially when we have to make changes in one single site entries and for that, you have to edit a big httpd-hosts.conf risking your other sites if there is any error in one.

So, how to solve it? I was looking for an easy solution. I have really liked the a2ensite and a2dissite tools provided in Ubuntu. You can configure and enable/disable any single site with a just a command. I tried to look for the similar tools for my server (which is a CentOS) but unable to find.

Then the Apache Include directive came for my rescue. The Include directive of Apache httpd allows you include the smaller parts of your main configuration file, which you can split into manageable units. Now with the help of this directive, I am able to have all my individual sites configurations splitted from the main httpd-vhosts.conf file and yet include them in the same main file to run them parallely. Having the individual files allows me to customize my sites VirtualHost configuration easily. Let me explain you how to achieve the above results:

Note: The below instructions are related to server running Apache httpd 2.2 version but it will hold good for 2.0 as well.

1. Create a directory in your apache configuration directory (i.e. /usr/local/apache2/conf/extra/vhosts.d).

2. In this directory, create the individual configuration files like domainname1.conf, domainname2.conf etc and put the whole VirtualHost block for the particular domain into each file. For e.g. domainname1 VirtualHost block will go to domainname1.conf, domainname2 VirtualHost block in domainname2.conf and so on. For the SSL specific sites you can follow the convention like ssl.domainname1.conf.

3. After creating all the necessary VirtualHost configuration files, open your httpd-vhosts.conf and delete all the VirtualHost entries from it. There should be only three entries in the httpd-vhosts.conf file and that is


NameVirtualHost *:80

NameVirtualHost *:443

Include conf/extra/vhosts.d/*.conf

The above settings will include all your domainname configuration files having an extension of .conf.

The advantage of this approach is, now whenever you are required to take a site down for some configuration settings changes, you can just change the extension (.conf) of that site and restart the Apache httpd server. After the changes are done, you can rename it back with .conf extension and restart the server again. This way, you will not be into a problem of having a human error on other sites and all your sites can live happily with their own settings. A bit of manual job to restart the server, but not a big one though.

Hope this helps you.

Liked the content? Then why not share with your pals

  • Leave a Reply

    * Required
    ** Your Email is never shared