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!
You could use mod_macro, which has been included in Apache httpd since version 2.4
Before that it had to be installed separately, see mod_macro. For example on Debian:
apt-get install libapache2-mod-macro; a2enmod macro
.Example configuration
/etc/apache2/conf.d/vhost.macro
/etc/apache2/sites-available/vhost.mysite.com
Much simpler using
Define
keyword. See Define Directive....
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:
to
/etc/init.d/httpd
(before the line that callshttpd
!) 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 launcheshttpd
is fine. Variables can be used in the configuration like so: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
:and then include these into your Apache
init.d
script with:before calling
httpd
. Still not brilliant but at least it separates the startup script and variables.You can use system environement variables with mod_env and the PassEnv directive. See here
Example for debian:
Add your variable to /etc/apache2/envvars (this file is used by apache2ctl to define variables)
Pass your variable into apache config
You can then access the system environment variable like if it was an apache variable.
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?):
Then create
/etc/apache2/mods-available/define.load
: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.
unbelievable but on httpd 2.2 on centos 6.4 this works
export env vars in /etc/sysconfig/httpd
then simply do this ...
then finally ....
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":
Fire up your browser: It works!