Simplifying apache vhosts configuration with mod_macro

One of the reasons why Apache is still so popular on a web servers market is a huge number of modules available for it. In this post i would like to share with you my experience with using third party module called mod_macro for simplifying maintenance of Apache configuration files. This module really allows you to follow DRY rules in your configs by wrapping repeatable piece of config with a special Macro tag and than reusing it passing different variables within macro call.

Anyway it is worth to read original module documentation.

Here is a few config snippets I use:

# This VHost macro is used on my local machine
# and is used for development purposes, it allows
# me to specify relative document root directory
<Macro VHost $host $webroot>
  <VirtualHost *:80>
      DocumentRoot /www/vhosts/$host$webroot
      ServerName $host
      ServerAlias *.$host
      ErrorLog /www/vhosts/$host/error.log
  </VirtualHost>
</Macro>
 
Use VHost localhost /
Use VHost pma /
Use Vhost webmaster.local /
Use VHost framework.js.local /svn/trunk
Use VHost intranet.local /www
# This one is used on our development machine where every developer
# has his own virtual host (mapped with mod_rewrite). This particular
# part is used for setting mod_php configuration parameters
 
#
# Developer macro
#
 
<Macro Developer $name>
  <Directory /home/$name/sites>
    php_admin_value open_basedir            /home/$name/
    php_admin_value upload_tmp_dir          /home/$name/sites/tmp
    php_admin_value session.save_path       /home/$name/sites/tmp
    php_admin_value soap.wsdl_cache_dir     /home/$name/sites/tmp
    php_admin_value error_log               /home/$name/sites/php_error.log
  </Directory>
</Macro>
 
#
# List of developers
#
 
Use Developer boaz
Use Developer thomas
Use Developer danube
Use Developer smith

NB! Windows version of mod_macro can be downloaded from Apache Lounge website.

Leave a Reply