views:

1133

answers:

3

I just want my apache to register some of my predefined environment so that i can retrieve it using getenv function in php. How can i do this? I tried adding /etc/profile.d/foo.sh with export FOO=/bar/baz using root and restarted apache.

+2  A: 

Environment variables are inherited by processes in Unix. The files in /etc/profile.d are only executed (in the current shell, not in a subshell) when you log in. Just changing the value there and then restarting a process will not update the environment.

Possible Fixes:

  • log out/log in, then start apache
  • source the file: # . /etc/profile.d/foo.sh, then restart apache
  • source the file in the apache init script

You also need to make sure that /etc/profile.d/ is sourced when Apache is started by init rather than yourself.

The best fix might also depend on the distribution you are using, because they use different schemes for configuration.

Torsten Marek
thanks for the great tip, i used the third option
ken
Good. Keep in mind that the init script of apache might change on upgrades.
Torsten Marek
A: 

You can use SetEnv in your config files (/etc/httpd/conf.d/*.conf, .htaccess ...). Additionally you should be able to define them in /etc/sysconfig/httpd (on RPM-based distribs) and export them (note: not tested).

Note: it wouldn't surprise me if some distributions tried quite hard to hide as much as possible, as far as system config is concerned, from a publically accessible service such as Apache. And if they don't, they might start doing this in a future version. Hence I advise you to do this explicitly. If you need to share such a setting between Apache and your shells, you could try sourcing /etc/profile.d/yourprofile.sh from /etc/sysconfig/httpd

niXar
A: 

Apache config files allow you to set environment variables on a per site basis.

So if your web server is serving pages from two logical sites you can have the same environment variable set differently for each site and thus get your PHP to react differently.

See the Apache mod_env for details:

Martin York