Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Does it work when using the www-data user in shell? (su -s www-data) –  Zulakis Aug 18 '12 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? –  All Aug 18 '12 at 21:13
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
You're quite right about Ubuntu, but I am using nginx. However, it is a useful hint. –  All Aug 19 '12 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. –  All Aug 20 '12 at 9:40
    
@Ali Ha! I just figured it out too :) –  Iain Aug 20 '12 at 9:54
1  
You can uncomment it in /etc/php5/fpm/pool.d/www.conf. –  quanta Aug 20 '12 at 10:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.