I tried to add $PATH for all users including the webserver user (i.e. www-data) with different methods such as editing /etc/profile, /etc/environment, etc. In all cases, it works in terminal, but not with shell commands within PHP.

For example echo $PATH in terminal shows available paths including that I added; but, shell_exec('echo $PATH') in PHP shows the original paths without the path I added: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

How can I set the $PATH globally to be usable by the webserver user? I am using nginx on Ubuntu/Debian.

I tried to edit /etc/init.d/nginx, I think this is the starting point for nginx, but no effect.


Solution 1:

You don't say which distro but my guess is Ubuntu or similar.

The default PATH, defined in /etc/init.d/apache2 is /usr/local/bin:/usr/bin:/bin

On my Ubuntu systems there is a file /etc/apache2/envvars. You can define the PATH in this file and when you restart Apache that will be the path that is used.

PATH=$PATH:/your/addtional/path

For nginx you can pass the path that you want as a fastcgi_param

location ~ \.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/tmp/php.socket;
    fastcgi_param PATH /usr/local/bin:/usr/bin:/bin:/your/path;
}

You need to specify the whole PATH that you want


Further update.

I have php set up as fcgi so (thanks to @MichaelHampton for some chat discussion) and I found that the path that system(...); sees is the one set in your php init script (in my case /etc/init.d/php-fcgi).


And after much digging around I found this which leads to the solution

env[PATH]=/your/custom/path

in php5-fpm.conf or as @Ali points out in the php5-fpm start script.