PHP exec fails when called from a browser but not from CLI

老子叫甜甜 提交于 2019-12-07 12:19:18

问题


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 chowning 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!