Environent Variables through PHP/Apache running on Linux

I'm trying to  make an environment variable  available to a PHP script.
I have exported the variable in /etc/profile, and it is available to all users including apache.  (I temporarily allowed apache shell access through /etc/passwd to see what is going on)

but when I put: "PassEnv MYVAR" into /etc/httpd/conf/httpd.conf I get the following warning:
[Wed Apr 21 10:44:09 2010] [warn] PassEnv variable MYVAR was undefined
and its unavailable.

I tried the following in PHP: echo exec ("env> /tmp/env.txt")
and the output is as follows:
TERM=xterm
PATH=/sbin:/usr/sbin:/bin:/usr/bin
_=/bin/env
PWD=/var/www/html
LANG=C
SHLVL=3


How can I get access to the variable MYVAR?
LVL 4
amroxAsked:
Who is Participating?
 
amroxAuthor Commented:
Have solved it.
The solution was to source the /etc/profile file before...

so the PHP command would be:

echo exec (". /etc/profile; env> /tmp/env.txt")

This now gives me all the env vars including ofcourse the one that i wanted
0
 
medveddCommented:
To use PassEnv you need to set up variables in shell process which starts httpd.
You can put this variables in /etc/sysconfig/httpd file, for example:

MYVAR=1

and restart httpd.

If you want to set variable in httpd.conf, you should use SetEnv directive:

SetEnv MYVAR 1

Otherwise, you can set variables for php in .htaccess file

php_value name value
0
 
amroxAuthor Commented:
I'm not sure I get you....

I don't want to set it anywhere else, its already set in /etc/profile.

If I was to declare it in sysconfig/httpd, then it becomes a separate variable.  I need to be able to access the GLOBAL var not a LOCAL one.

Are you saying this cannot be done?

Also, where is it getting these from:
TERM=xterm
PATH=/sbin:/usr/sbin:/bin:/usr/bin
_=/bin/env
PWD=/var/www/html
LANG=C
SHLVL=3

These are not passenv'd or declared anywhere in sysconfig/httpd or /etc/httpd/conf/* or conf.d/*

0
 
medveddCommented:
/etc/profile sets variables only for login shells. httpd is started from init, and init is not processing /etc/profile
Some of variables you mentioned are inherited from init, some are set in /etc/init.d/httpd script
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.