Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

5 Answers

up vote 27 down vote accepted

You could use mod_macro.

Debian installation

Install with: apt-get install libapache2-mod-macro

and enable with 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
share|improve this answer
1  
Ah, that looks like what I need. Shame it isn't built in… But I guess I can live. Thanks! –  David Wolever Sep 12 '09 at 23:58
    
Excellent! This does what I was planning to do for far better than I thought would be possible to do. –  SpoonMeiser Nov 7 '09 at 0:09
add comment

You can enable or disable bits of configuration with IfDefine but that probably won't do what you want. Instead, You can set environment variables in your Apache init script to access within the configuration. For example, adding:

HOSTNAME=$(hostname)

to /etc/init.d/httpd (before the line that calls httpd!) on a RHEL machine passes the machine's hostname in as a variable. It doesn't have to be the output of a command -- anything that sets a variable in the environment which launches httpd is fine. Variables can be used in the configuration like so:

[root@dev ~]# cat /etc/httpd/conf.d/test.conf
Header set X-Hostname ${HOSTNAME}

[root@dev ~]# GET -Sed http://localhost
GET http://localhost --> 200 OK
Connection: close
Date: Fri, 11 Sep 2009 20:47:13 GMT
Server: Apache/2.2.3 (Red Hat)
Content-Length: 525
Content-Type: text/html;charset=ISO-8859-1
Client-Date: Fri, 11 Sep 2009 20:47:13 GMT
Client-Peer: 127.0.0.1:80
Client-Response-Num: 1
Title: Index of /
X-Hostname: dev.local

Of course, you're not restricted to the Header directive. The variables can be used anywhere, like <Directory ${FOO}> etc.

If you don't like this (and it's not that nice..) you can generate a configuration from a template using m4 or some other template language.

ADDITIONAL:

Hrm, one way to make it better would be to store all the variables in an external file, perhaps /etc/httpd/conf/variables.txt:

FOO=/path/to/dir
ROLE=development

and then include these into your Apache init.d script with:

. /etc/httpd/conf/variables

before calling httpd. Still not brilliant but at least it separates the startup script and variables.

share|improve this answer
    
hhmm… Thanks – while, as you say, this isn't quite as nice as I was hoping for, it's certainly better than nothing. Thanks. –  David Wolever Sep 11 '09 at 21:02
6  
A better (and cleaner) place for these environment variables would be /etc/sysconfig/httpd (RedHat, CentOS) or /etc/apache2/envvars (Ubuntu/Debian). Some environment variables are there already. –  Stefan Lasiewski Apr 22 '11 at 23:41
add comment

You can use system environement variables with mod_env and the PassEnv directive. See here

Example for debian :

Add you variable to /etc/apache2/envvars (this file is used by apache2ctl to define variables)

...
export APACHE_PID_FILE=/var/run/apache2.pid
export HOSTNAME=$(hostname)

Pass your variable into apache config

PassEnv HOSTNAME

You can now access the system environment variable like apache one

Header set Served-By %{HOSTNAME}e
share|improve this answer
add comment

I had the same problem and, after some research, the solution for Apache 2.x that exactly solved it for me (and nothing more) was this:

http://people.apache.org/~rjung/mod_define/

Beware that after unpacking you should build it like so (the install part of the docs seem to have forgotten to adhere to apache2?):

apxs2 -cia mod_define.c

Then create /etc/apache2/mods-available/define.load:

LoadModule define_module /usr/lib/apache2/modules/mod_define.so

After that, enable the module using a2enmod like you normally would.

The docs in the link above show how to use it. Now you can very simply define stuff and use it directly, all within the same apache2 config.

share|improve this answer
    
In case this isn't clear, this exact same functionality is available by default in Apache 2.4. –  ColinM May 7 '13 at 22:16
add comment

You may want to look into mod_passenger for apache which can also host django apps. We use it with great success. All you need to do in the vhost is, hmm, exactly nothing. Only thing you need is to create a "public" dir in the application root and create symlinks in "public" to your static directories like "media" (this will boost static serving performance) and point your DocumentRoot to it.

Then place the following file in "public/../passenger_wsgi.py":

import sys, os
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
sys.path.append('/PATH/TO/PACKAGES') # optional
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Fire up your browser: It works!

share|improve this answer
    
ah, that's good to know about – thanks. –  David Wolever Sep 11 '09 at 23:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.