Multiple apache virtual hosts configuration files

Multiple apache virtual hosts configuration files

Recently I’ve developed a control panel for my server so I’m able to manage all the websites I control using a simple interface. One of the main features is enabling me to update Apache’s virtual host files without ever going into the server and manually editing the vhosts.conf file.

Problem

As I’m a very organised person, I wanted to split each of the virtual hosts into separate configuration files and not have them all crammed into a single vhosts.conf file. Doing a search online return very little help about how to do this, but luckily I was able to workout a very simple yet effective solution.

Solution

Inside the Apache httpd.conf file, near the bottom, there is a section about virtual hosts. To enable your server to use the organised virtual host structure, follow this simple guide:

Firstly is the section which will enable name based virtual hosting, feel free to customize the IP address (*) and the port (80):

NameVirtualHost *:80

Secondly you should give a default directory for all sites to be served from if there isn’t a virtual host setup for the requested host name:

<VirtualHost *:80>
    DocumentRoot /var/www/html
</VirtualHost>

And finally, the magic. You need to create a folder within the installation directory of Apache (/etc/httpd) naming it something like “vhosts”. Within that folder you can add separate virtual host configuration files and name them accordingly, such as: foo.com.conf, bar.co.uk.conf – Just be sure to include the “.conf” at the end of each file. Back within the httpd.conf, add the following lines to automatically include all virtual host configurations:

Include vhosts/*.conf

Sample Virtual Host Configuration File

There is a comprehensive guide to configuring virtual hosts on the Apache website, however, here is a very simple sample of the type of content in one of the configuration files, lets look at foo.com.conf for example:

<VirtualHost *:80>
    ServerName www.foo.com
    ServerAlias foo.com
    DocumentRoot /var/www/vhosts/foo.com/public
    ErrorLog /var/www/vhosts/foo.com/logs/error.log
    ServerAdmin [email protected]
</VirtualHost>

Overview

The control panel on my server simply generates a configuration file within the vhosts directory for each site I’ve currently got set up. This organizes the virtual host directory and gives me a simple structure if I’m ever required to debug.