Why Apache PerlModule directive doesn't set custom environment variables?
Question
I am building a mod_perl website, and I need to set an environment variable that will be used in the Perl code.
Until now I was using the PerlSetEnv
directive to set this variable:
PerlSetEnv MYVAR myvalue
<LocationMatch /perlpath/>
SetHandler modperl
PerlResponseHandler myhandler
Header set Cache-control "no-cache"
</LocationMatch>
This works fine, but I'd like to preload my mod_perl handler, because the first call is very slow. So I changed my Apache virtualhost file to:
PerlSetEnv MYVAR myvalue
PerlModule myhandler <- add this line
<LocationMatch /perlpath/>
SetHandler modperl
PerlResponseHandler myhandler
Header set Cache-control "no-cache"
</LocationMatch>
But if I do this, my custom environment variable MYVAR
is not set when preloading myhandler
, and my code fails.
So is it possible to set an environment variable that will be exported by the PerlModule
directive?
Solution
You could use PerlPassEnv to forward the env variables to the programs running in the server. However, they take effect in the first phase of apache request cycle unlike the setEnv & PassEnv which don't take effect until fixup phase.
In the past, I have used BEGIN Clause to set the env variable like ORACLE_HOME that I want to be visible much before the request phase.
Try setting the variable inside a perl BEGIN block in your httpd.conf that might expose it much ahead of the traditional directives.
OTHER TIPS
Maybe you have to move the setenv inside your virtualhost config?
<LocationMatch /perlpath/>
SetHandler modperl
PerlSetEnv MYVAR myvalue
PerlResponseHandler myhandler
Header set Cache-control "no-cache"
</LocationMatch>