Apache Variables
Apache Variables are not supported by defaultby apache but you can do two things to achieve this functionality::
- Option 1 – Use system variables
- Option 2 – Use mod_macro
Applicable to
- Centos 6.x
This article was written with instructions for Centos 6.x, but the concept applies any distibution.
Option 1 – System Variables
Add variables in init script
To set environment variables in your Apache init script (/etc/init.d/httpd) before the line that calls httpd. For example, add:
MYVAR=value
Better would be to store all the variables in an external file, for example you can call it: /etc/httpd/conf/vars.txt and add to it all your variables:
MYAPP=/path/to/dir
BAR=/path/to/dir
and then include these into your Apache init.d script with:
. /etc/httpd/conf/vars.txt
you can also reload the file into memory by using the last command on shell.
to check that the variables added to your system variables use the set command:
set
Using inside configuration
Variables can be used in the configuration like so:
${MYVAR}
The variables can be used anywhere.
You can pass the variable into your CGI scripts and SSI pages using mod_env by adding to your confs:
PassEnv LD_LIBRARY_PATH
Option 2 – mod_macro
You can use apache Variables as functions using mod_macro extension,
mod_macro enables you to create macro functions with variables like:
<Macro VHost $host $port $dir> Listen $port <VirtualHost *:$port> ServerName $host DocumentRoot $dir <Directory $dir> # do something here... </Directory> # limit access to intranet subdir. <Directory $dir/intranet> order deny,allow deny from all allow from 10.0.0.0/8 </Directory> </VirtualHost> </Macro> ## Use of VHost with different arguments. Use VHost www.apache.org 80 /projects/apache/web Use VHost www.perl.com 8080 /projects/perl/web Use VHost www.ensmp.fr 1234 /projects/mines/web
* example taken from here.
Prerequisites for mod_macro
- The scripts in the article uses wget: yum install wget
- apxs: yum install httpd-devel.i386
- apache version 2.3
check your apache version using:
httpd -v
centos repository have apache 2.2.15, so you’ll need to compile newer version from source which is out of the bounds of this article. if you have later version of apache you can still use mod_macro which is better solution (for me) over system variables
mod_macro
to download and install mod_macro type:
cd /tmp
wget http://people.apache.org/~fabien/mod_macro/mod_macro-latest.tar.gz
tar xzvf mod_macro-1.2.1.tar.gz
apxs -cia mod_macro-1.2.1/mod_macro.c
Update 1.2.1 if wget returns newer version.
read more information and view demonstration code of how to use this extension check: http://www.cri.ensmp.fr/~coelho/mod_macro/
That’s it! select the way appropriate for your apache variables…
Development Specialist, Artist and Activist
Personal Website