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
  • By "sites" I do you mean Virtual Hosts or just three directories? – garethTheRed Sep 17 '14 at 8:45

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


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

  • Thanks this is useful. How would I go about to a2ensite siteA.conf? – Captain Giraffe Sep 17 '14 at 16:33
  • You won't, siteX.conf aren't seen as "sites" from apache POV. You'll do a2ensite on files present in sites-available directory. – Pierre-Yves Gillier Sep 18 '14 at 7:38
  • I'm able to put instances in sites-available and a2ensite instances/siteA. The command works but gives warnings. Not sure if I'll go that route though. Thanks for your insights. – Captain Giraffe Sep 18 '14 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.