7

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.

2
  • Does it work when using the www-data user in shell? (su -s www-data)
    – Zulakis
    Aug 18, 2012 at 21:10
  • yes, in shell works (SSH terminal), but not when running as webserver users (executing shell commands in PHP). You mean su - www-data?
    – Googlebot
    Aug 18, 2012 at 21:13

1 Answer 1

8

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.

6
  • You're quite right about Ubuntu, but I am using nginx. However, it is a useful hint.
    – Googlebot
    Aug 19, 2012 at 12:31
  • I figured it out! env[PATH] in php5-fpm should be modified to include custom PATH. If you add this to your answer, I will set it as accepted answer. It is very useful to let others know that this should be set in PHP rather than nginx.
    – Googlebot
    Aug 20, 2012 at 9:40
  • @Ali Ha! I just figured it out too :)
    – user9517
    Aug 20, 2012 at 9:54
  • 1
    You can uncomment it in /etc/php5/fpm/pool.d/www.conf.
    – quanta
    Aug 20, 2012 at 10:14
  • Setting env[PATH] works. Now my shell_exec calls are working in PHP again. Aug 5, 2014 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .