【问题标题】:shell_exec('which git') returns empty string on Godaddy shared hosting servershell_exec('which git') 在 Godaddy 共享主机服务器上返回空字符串
【发布时间】:2015-05-18 05:00:22
【问题描述】:

我已经用 git 和我的服务器(godaddy 共享主机)设置了关于 SSH 密钥的所有内容,并且可以使用git pull git@github.com:username/reponame.git通过 SSH 成功更新服务器

我正在尝试使用 simple-php-git-deploy 在我提交对我的 git 存储库的更改时自动更新测试服务器

当下面的 php 脚本摘录运行 shell_exec('which git') 时,函数返回一个空字符串。

由于我可以在 SSH 中运行 git 命令,我觉得这一定是权限问题。

我尝试运行chmod 755 foldertoupdate,没有任何变化。

我需要做什么才能让 php(或当前的 apache 用户)调用 git 命令?

// Check if the required programs are available
$requiredBinaries = array('git', 'rsync');

//.....

foreach ($requiredBinaries as $command) {
    $path = trim(shell_exec('which '.$command));
    if ($path == '') {
        die(sprintf('<div class="error"><b>%s</b> not available. It needs to be installed on the server for this script to work.</div><br> Output was: %s', $command,$path )); // this check fails on the first element, "git" claiming git is not installed
    } else {
        $version = explode("\n", shell_exec($command.' --version'));
        printf('<b>%s</b> : %s'."\n"
            , $path
            , $version[0]
        );
    }
}

更多信息:

  • 我正在更新的文件夹位于~/html/TeamBoard
  • 我从中调用代码的文件位于~/html/TeamBoard/deploy.php

【问题讨论】:

  • 您的脚本从哪个目录运行,您是否能够从该位置成功发出命令行 git 命令?
  • @TimBiegeleisen php 文件位于我要更新的文件夹中,~/html/TeamBoard 是文件夹,文件位于~/html/TeamBoard/deploy.php。当cd 进入html 文件夹时,我能够将repo 克隆到这个文件夹,但现在我尝试做git pull 我得到你要求从远程'git@github.com:DelightedD0D/TeamBoard .git',但没有指定分支。因为这不是您当前分支的默认配置远程,所以您必须在命令行上指定一个分支。
  • apache 用户(或任何 php 在浏览器中运行的用户)需要相关文件夹/二进制文件的权限
  • @Anthony 你能告诉我如何改变吗?我以为chmod 755 foldertoupdate 会起作用,但它没有。是否需要为实际安装 git 的文件夹设置访问权限?
  • 我不确定,但请记住,如果 apache 在共享环境中运行,那么让它工作将使托管在同一机器上的任何其他站点能够在您的 repo 文件夹上执行 git 命令。也许看看像 suphp 之类的东西?

标签: php git github ssh


【解决方案1】:

Godaddy 技术支持确认 php 在其共享主机环境中无法访问 git 命令。他们说需要 VPS 来实现这一点。

【讨论】:

    【解决方案2】:

    正如我所见,这不是关于从您的代码中执行 git,而是关于执行 shell 程序(在本例中为which)。不清楚您是通过浏览器还是直接在 shell 中执行 deploy.php 脚本。

    尝试使用exec('command -v git', $output, $retval); 在 PHP 交互模式 (php -a) 下检查 git 命令的可用性,看看这是否有效。

    【讨论】:

    • deploy.php 正在浏览器中运行。这个想法是让 github 的 webhook 功能在 repo 上运行提交时触发脚本以更新服务器。我在文件中添加了exec('command -v git', $output, $retval); echo $retval;,但它也回显了一个空字符串,所以似乎不行。
    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多