Is ‘Include /path/to/*/live.conf’ possible with Apache

apache-2.2

We store VirtualHost entries in (dev|staging|live).conf files within the SVN repository for the site.

For example:

/var/www/vhosts/site1.com/live.conf
/var/www/vhosts/site2.com/live.conf

It'd be handy to be able to include them automatically, such as:
Include /var/www/vhosts/*/live.conf

But that doesn't seem to work.

We've worked around it with a simple PHP script:

$vhosts = '';
foreach (glob('/var/www/vhosts/*/vhost/dev.conf') as $filename) {
    $vhosts .= "\nInclude $filename";
}

file_put_contents('/var/www/vhosts/vhosts.conf', $vhosts);

However, I'd love to simplify and get Apache (2.2.3) to do the job without requiring the PHP step.

Possible?

Best Answer

apache2 does not support wildcards in includes. However, you could arrange it a bit in the following manner:

  • create some directory for your configs (such as /etc/apache2/myconfigs)
  • symlink your configuration dynamically to apache2:
rm -f /etc/apache2/myconfigs/*
for i in $(find /var/www/vhosts/*/live.conf); do \
tempfn=$(echo $i|cut -d/ -f5-6|sed s,/,_,);  \
ln -s $i /etc/apache2/myconfigs/$tempfn.conf \
done
  • add a "Include /etc/apache2/myconfigs" in your apache2.conf

Note: on cut, i used -f5-6 for delimiter joining (to provide something like site1.com_live.conf symlink name), but you might have to adjust that according to your own fs path. Note on note: the above is for bash, might need adapting for other shells.

Related Topic