Apache Config – Using Variables to Reduce Duplication

apache-2.2

Is it possible to use variables in Apache config files?

For example, when I'm setting up a site with Django+WSGI, the config file might look like:

<Directory /path/to/foo/>
    Order allow,deny
    Allow from all
</Directory>
Alias /foo/static /path/to/foo/static
WSGIScriptAlias /foo /path/to/foo/run_wsgi

And I'd like to turn the '/path/to/foo' into a variable so it only needs to be defined in one place. Something like:

Variable FOO /path/to/foo
…

Thanks!

Best Answer

You could use mod_macro, which has been included in Apache httpd since version 2.4

Before that it had to be installed separately, see mod_macro. For example on Debian: apt-get install libapache2-mod-macro; a2enmod macro.

Example configuration

/etc/apache2/conf.d/vhost.macro

<Macro VHost $host $port>
  <VirtualHost $host:$port>

    ServerName $host
    DocumentRoot /var/vhosts/$host

    <Directory /var/vhosts/$host>
      # do something here...
    </Directory>
  </VirtualHost>
</Macro>

/etc/apache2/sites-available/vhost.mysite.com

Use VHost vhost.mysite.com 80