[ale] setting an env variable that apache will see when calling a cgi script

Brandon Checketts brandon at brandonchecketts.com
Mon Jul 20 17:03:18 EDT 2009


Geoffrey wrote:
> I'm trying to figure out how to set an env. variable that will be 
> available to a perl cgi script.  I've tried putting it in the 
> /etc/sysconfig/httpd file as well as in the /etc/init.d/httpd script, 
> but it does not find it's way to the process that executes the cgi.
> 
> Anyone know how to do this?  For example:
> 
> print $ENV{'FOO'};
> 


You can set them inside your Apache config with the mod_env Apache module, which
should be included with most basic Apache installations.

<VirtualHost>
   ServerName your.domain.com
   ... your other configuration here...
   SetEnv  somevar  "some value"
   SetEnv
</VirtualHost>

Documentation for the module is at
http://httpd.apache.org/docs/2.2/mod/mod_env.html is pretty straightforward.


The mod_setenvif is a bit more flexible and allows you to use conditionals to
set environment variables.  Documentation at
http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html


Example:

SetEnvIf Host !"^www\.hostname\.com$" wronghost=1


That will set the environment variable 'wronghost' to 1 if the HTTP_HOST header
is not 'www.hostname.com'.  Your Perl script can then respond accordingly
(perhaps redirecting to the correct domain name in this case)



Thanks,
Brandon Checketts


More information about the Ale mailing list