简体   繁体   English

从浏览器调用PHP exec失败但从CLI调用失败

[英]PHP exec fails when called from a browser but not from CLI

I have a simple PHP script: 我有一个简单的PHP脚本:

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. 当我通过CLI php git.php调用它时,它工作正常。 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. 我得到了预期的输出,返回值为0.当我通过Web浏览器访问该页面时,它失败并返回值为1。

I've set file permissions to 777, and ensured php.ini doesn't block the exec() function. 我已将文件权限设置为777,并确保php.ini不会阻止exec()函数。

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. CLI使用当前登录的用户(您)凭据运行,它很可能与Web服务器进程使用的不同。 Do a exec('whoami') etc. to verify. 做一个exec('whoami')等来验证。

I was able to fix the issue with some help by Ates Goral. 我能够在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. 查看在www-data用户下运行时脚本的行为方式。 There were two issues: 有两个问题:

www-data did not have its own public key. www-data没有自己的公钥。 I created one and added it to the github repo. 我创建了一个并将其添加到github仓库。 And the .git folder was not readable by www-data . www-data无法读取.git文件夹。 This was fixed by chown ing the directory to give permissions to the group www-data that both I and apache belong to. 这是固定通过chown荷兰国际集团的目录给权限组www-data ,我和Apache同属。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM