问题描述
我有一个简单的PHP脚本:
I have a simple PHP script:
exec('git pull origin master', $shell_output, $output);
print_r($shell_output);
print_r($output)
当我通过CLI php git.php
调用它时,它可以正常工作.我得到了预期的输出,返回值为0.当我通过Web浏览器访问该页面时,它失败,返回值为1.
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.
我已将文件权限设置为777,并确保php.ini
不会阻止exec()
功能.
I've set file permissions to 777, and ensured php.ini
doesn't block the exec()
function.
推荐答案
在Ates Goral的一些帮助下,我能够解决此问题.
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
以查看在www-data
用户下运行时脚本的行为.有两个问题:
to see how the script behaved when run under the www-data
user. There were two issues:
www-data
没有自己的公钥.我创建了一个并将其添加到github仓库中.并且www-data
文件夹无法读取.git
文件夹.通过将目录chown
授予我和apache都属于的组www-data
的权限,可以解决此问题.
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.
这篇关于从浏览器而不是CLI调用时,PHP exec失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!