php运行git得到"ssh Permission denied"

use*_*046 6 php git ssh assembla

我试图从浏览器中运行一个php脚本中的git pull,但我得到了"sh:connect to host git.assembla.com port 22:Permission denied"

我的PHP脚本:

<?php
$output=array();
$returnVar=0;
chdir("/var/www/html");
exec('git pull git@git.assembla.com:andrewadel.git master 2>&1', $output , $returnVar);
// exec('pwd', $output , $returnVar);
echo "<pre>\n";
echo "return status: $returnVar\n\n";
print_r($output);
echo "</pre>\n";
Run Code Online (Sandbox Code Playgroud)

当我手动运行脚本为"apache"时,一切都很好

bash-4.1$ whoami
apache
bash-4.1$ php gitsync.php
<pre>
return status: 0

Array
(
    [0] => From git.assembla.com:andrewadel
    [1] =>  * branch            master     -> FETCH_HEAD
    [2] => Already up-to-date.
)
</pre>
Run Code Online (Sandbox Code Playgroud)

当我从浏览器运行它时,它失败了

http://103.7.164.33/gitsync.php?111

return status: 1

Array
(
    [0] => ssh: connect to host git.assembla.com port 22: Permission denied
    [1] => fatal: The remote end hung up unexpectedly
)
Run Code Online (Sandbox Code Playgroud)

谢谢

Jim*_*oad 4

这里有很多变量...但我在使用我正在处理的远程 cgi 脚本时遇到了几乎完全相同的行为。

就我而言,问题与 CentOS 上的 SELinux 有关。

user@remoteserver:~$ getsebool -a | grep httpd

显示:

...
httpd_can_network_connect --> off
...
Run Code Online (Sandbox Code Playgroud)

测试可能的修复(sudo 或以 root 身份运行):

user@remoteserver:~$ setsebool httpd_can_network_connect=1
//...then initiate your serverside script remotely
Run Code Online (Sandbox Code Playgroud)

永久修复(如果上述已证明有效):

user@remoteserver:~$ setsebool -P httpd_can_network_connect=1

-P 选项确保主题 SELinux 布尔值在将来重新启动时设置为指定值作为默认值。参见: man getsebool和 man setsebool