# $PATH for apache2, how can I make it understand my /etc/profile?



## vito (Aug 13, 2009)

When I start a shell script from Apache/PHP by shell_exec() PHP function, the environment inside this shell script is NOT set as it should be.

In particular, $PATH variable contains this:
PATH=/usr/bin:/bin:/usr/sbin:/sbin

Instead of what it normally contains when I work in Terminal.

It means that the shell ignores the /etc/profile file. The question is - how can I instruct apache/php/sh to work with /etc/profile?


----------



## fintler (Aug 15, 2009)

I just want to say that from what you posted, you don't know what you're doing. This is a security feature. Tread lightly or some script kiddie is going to hack into your server. Using the shell_exec() securely is difficult for highly experienced people to do correctly.

That said, you want to export your PATH env as the command itself...


```
$command = 'export PATH="/foo:/foo/bar"; script.sh';
shell_exec($command);
```

Again, keep in mind that any user input that gets constructed into the command instance var can do some serious damage to your server. So be careful. Good luck!


----------



## vito (Aug 15, 2009)

thanks for your reply and code example. it will work. however, I want my script.sh to get all environment configuration as I get in bash terminal. it's not a question of just PATH, but all other env variables. They are NOT initialized now, when I do shell_exec(). Why?


----------



## fintler (Aug 16, 2009)

Try using system() instead of exec_shell() to get a few default env vars setup automagically (copying the char *envp[] from the parent process -- probably apache).

If you take this route, you want to be careful that there aren't any paths included that are writable by the web service process. So, try running system("export") to make sure, since it ma be different from your default login shell.


----------



## vito (Aug 17, 2009)

I found the solution. I have to define PATH variable by means of LSEnvironment in /System/Libraries/LaunchDaemons/org.apache.httpd.plist

By default ENV variables are NOT passed to applications, in Mac OS


----------

