问题
I have a simple PHP script:
exec('git pull origin master', $shell_output, $output);
print_r($shell_output);
print_r($output)
When I call this via CLI php git.php
, it works fine. I get the expected output, and a return value of 0. When I visit the page via a web browser, it fails with a return value of 1.
I've set file permissions to 777, and ensured php.ini
doesn't block the exec()
function.
回答1:
The CLI runs with the currently logged in user's (you) credentials and it's most probably different from what the web server process uses. Do a exec('whoami')
etc. to verify.
回答2:
I was able to fix the issue with some help by Ates Goral.
To debug the issue, I ran:
sudo -u www-data php git.php
to see how the script behaved when run under the www-data
user. There were two issues:
www-data
did not have its own public key. I created one and added it to the github repo.
And the .git
folder was not readable by www-data
. This was fixed by chown
ing the directory to give permissions to the group www-data
that both I and apache belong to.
来源:https://stackoverflow.com/questions/13097550/php-exec-fails-when-called-from-a-browser-but-not-from-cli