Apache mod_macro for Multi Vhost Develpment

To quickly set up virtual hosts in Apache mod_macro has prooven to be a little gem. As the name suggests it allows you to use macros in the Apache configuration files. With mod_macro enabled you can add a new vhost by simply adding a single line to the vhost config file.

Here a brief overview on how to get it working.

Install and enable the module (Ubuntu/Debian):

root@michael:~$ apt-get install libapache2-mod-macro
root@michael:~$ a2enmod libapache2-mod-macro

Set up the macro in the development config file (/etc/apache2/sites-enabled/development):

<Macro VHOST $host $path>
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias $host

        DocumentRoot $path

        <Directory $path>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/$host.error.log
        CustomLog ${APACHE_LOG_DIR}/$host.access.log combined

        LogLevel warn

        RewriteEngine On
        RewriteLog ${APACHE_LOG_DIR}/$host.rewrite.log
        RewriteLogLevel 3

    </VirtualHost>

    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerAlias $host

        DocumentRoot $path

        <Directory $path>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/$host.ssl.error.log
        CustomLog ${APACHE_LOG_DIR}/$host.ssl.access.log combined

        LogLevel warn

        RewriteEngine On
        RewriteLog ${APACHE_LOG_DIR}/$host.ssl.rewrite.log
        RewriteLogLevel 3

        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/server.crt
        SSLCertificateKeyFile  /etc/apache2/ssl/server.pem
    </VirtualHost>
</Macro>

Use VHOST foo.local /src/foo
Use VHOST bar.local /src/bar

The Use VHOST foo.local /src/foo line adds a new vhost to the config with the server alias foo.local and the document root set to /src/foo.

Add an entry for each vhost to your hosts file (Windows: %windir%\System32\drivers\etc, Linux: /etc/hosts).

192.168.0.1 foo.local
192.168.0.1 bar.local

After restarting Apache you should be able to access http://foo.local and http://bar.local from your browser.

I'm available for contracting work. Check out my LinkedIn profile and my portfolio page for an overview of my skills and experience. If you are interested in working with me please use the contact form to get in touch.

Apache mod_macro for Multi Vhost Develpment

Leave a Reply

Your email address will not be published. Required fields are marked *