How to organize apache site configuration files

apache-2.2apache-2.4httpd.conf

I'm looking for a way to organize the files that makes up the apache sites-enabled configuration.

Let's say I have sites A, B and C on my webserver. These sites have different DocumentRoot s and different RewriteRule s as well as different Authentication requirements. Defined by Alias and Directory contexts. I'm on a debian based server.

I want both a http and https version of A, B and C.

For VirtualHost ServerName www1.myhome.com;, I want to serve A and B.

For VirtualHost ServerName www2.myhome.com;, I want to serve B and C.

How do I avoid duplication of the configuration of A, B and C?

I was trying to use includes in different forms. Including configuration for e.g. A into the different virtualhosts, but I found the problem with file paths cumbersome. In particular it doesn't allow me to a2ensite A. Also it makes troubleshooting the configuration file a nuisance. This is all done as a part of migrating from Apache2.2 to Apache2.4.

Is there a good solution to this problem?

Configuration site A. B and C are similar in structure.

Alias /A /srv/rootA/
<Directory /srv/rootA/>
   // Unique stuff for A
   // In particular Auth and rewrite rules
</Directory>

Best Answer

It's one solution amongst many and I'm assuming you're on a debian based server.

In your apache serverroot directory, create a directory instances. In this directory create as many config files as you have "sites" (A,B or C). Define in them what you need to and name each file accordingly.

In sites-available directory, create one file for each virtual host you need. In these files, define you virtual host as usual and include "sites" config as needed, ie:

# Vhost for www1.myhome.com
<VirtualHost *:80>

   Include instances/siteA.conf
   Include instances/siteB.conf

</VirtualHost>

After, you'll be able to do a2ensite and debug easier.

Ref on Apache include command : http://httpd.apache.org/docs/2.4/mod/core.html#include

Related Topic