Tag Archives: Mac OS X Mavericks

How to define variables in Apache configuration files on Mac OS X Mavericks (10.9)

In Apache HTTP Server 2.4 it is possible to define variables in the configuration.  This allows a clearer representation if the same value is used at many places in the configuration.  Unfortunately, my Mac OS X Mavericks (10.9) Apache HTTP Server version is 2.2.24 where this functionality is not available.  After some searching I found an Apache2 module named mod-define that would give me this capability.  Here is the process I went through to install this module.

1.  Stop the Apache server.  (You will need to be an admin user when using sudo)

 sudo apachectl -k graceful-stop

2.  Download mod_define.c module from http://code.google.com/p/mod-define/.

3.  Compile the module.  (You will need to be an admin user to install Apache modules)

sudo apxs -c -i -a mod_define.c

Apxs compiles and installs Apache modules. The -c tells apxs to compile the module; the -i tells apxs to install the module; and the -a tells apxs to put a line for the module into Apache’s configuration file.

– I got this error:

env: /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.9.xctoolchain/usr/bin/cc: No such file or directory

– So I created a symlink to the Xcode toolchain:

cd /Applications/Xcode.app/Contents/Developer/Toolchains/
sudo ln -s XcodeDefault.xctoolchain OSX10.9.xctoolchain

This problem is caused by an incorrect Apache build configuration that Apple has shipped. This issue has supposedly been reported to Apple, but they haven’t done anything about it.

– I tried to compile the module again and got this error:

mod_define.c:49:10: fatal error: 'httpd.h' file not found

– So I updated/installed xcode command line tools.

xcode-select --install

Run xcode-select –install from the command line to reinstall the command line tools.  I already had Xcode installed but not the command line tools.

– I tried to compile the module again and it finally installed successfully!

4.  Test the new module.

sudo apachectl configtest

– It responded:

Syntax OK

5.  Restart the web server.

sudo apachectl -k graceful

– Sample Code:

# global defines - define only once!
Define global::root /srv/www

# local defines - define per vhost file
Define host test
Define domain example.com
Define root ${global::root}/$host
Define docroot $root/html

# http vhost
<VirtualHost *:80>
ServerAdmin     admin@$domain
ServerName      $host.$domain

DocumentRoot    $docroot
LogLevel        warn
ErrorLog        $root/log/error.log
TransferLog     $root/log/access.log
Redirect 301 / https://$host.$domain/

# https vhost
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin@$domain
ServerName $host.$domain

DocumentRoot $docroot
LogLevel        warn
ErrorLog        $root/log/error.log
TransferLog     $root/log/access.log

SSLEngine               on
SSLCertificateFile      $root/keys/www.https.pem
SSLCertificateKeyFile   $root/keys/www.https.key
BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

<Directory />
Options None
AllowOverride None
order allow,deny
deny from all

<Directory $docroot>
Options Indexes MultiViews
Order allow,deny
allow from all