Apache 2.4 Macro Module

apacheOver the weekend I upgraded my work environment from Ubuntu 12.04 LTS to Ubuntu 14.04.1 LTS. I’ve been meaning to upgrade to 14.04 LTS to have an environment with a more up-to-date PHP package, among other benefits. Waiting for the .1 release of the LTS assures that most major show-stopping bugs have been fixed in Ubuntu 14.04.

In the upgrade Apache went from version 2.2 to 2.4 there are numerous changes outlined by Apache in Apache’s configuration a developer or DevOps should be aware of. If you use shared hosting this information may not be use benefit to you, but if you run a VPS or a dedicated server than knowing this can save you lots of time.

Among other things my server configuration required renaming all my virtualhost entries to have a naming convention that ends in .conf. I also had to add an Authorization Rule to each virtualhost entry. The rule for every virtual host would look like:

<Directory /path/to/public/website/>
   Require all granted
</Directory>

I have approximately 20 virtualhost entries — I don’t want to have to write this in many virtualhost files. So, new to Apache 2.4 is mod_macro! Mod Macro allows you to define macros to be used elsewhere in Apache configuration files. You can think of a macro as a variable or simple function you’re defining to be evaluated at runtime to and used by Apache.

So for this Authorization rule I wanted to create a macro. My macro looks as follows:

<Macro U1404DefaultGrantAccess $docroot>
    <Directory $docroot>
      Require all granted
      AllowOverride all
    </Directory>
</Macro>

This macro is called U1404DefaultGrantAccess and it takes a single parameter $docroot. When you call the named macro the usages of $docroot in the <Macro> … </Macro> definition get replaced with the value we pass in and the whole string is returned and used by the system.

In my virtualhost entry I use this macro as follows:

<VirtualHost *:80>
  ServerName example-site.com
  DocumentRoot /var/www/site1
  Use U1404DefaultGrantAccess /var/www/site1
</virtualHost>

When Apache loads my configuration files the Use U1404… macro line takes the docroot path as a parameter and expands out the macro definition so that the final virtualhost entry looks like:

<VirtualHost *:80>
  ServerName example-site.com
  DocumentRoot /var/www/site1
  <Directory /var/www/site1>
      Require all granted
      AllowOverride all
  </Directory>
</virtualHost>

So this macro is saving me some extra typing and allowing me to template out some typical configuration values used across my virtual hosts. The Apache mod_macro documentation I provided a link previously for shows an example of a simple complete virtualhost entry macro. I think that is going a tad too far — typically webapps may require their own configuration and so trying to macro out your complete vhost configuration seems like biting off more than you can chew with macros. But, macro-ing out sets of rules in vhost entries seems completely reasonable to me.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under Linux Server Admin | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

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