【问题标题】:ERROR: Repository not found whilst running git pull via shell_exec on php script错误:在 php 脚本上通过 shell_exec 运行 git pull 时未找到存储库
【发布时间】:2018-08-25 17:14:37
【问题描述】:

我已经正确设置了 ssh 密钥并将它们添加到我的 github 帐户中。每当我 ssh 进入服务器并运行 git pull 时,一切都会正常运行,它会从存储库中提取更改。但是我有一个通过 shell_exec() 运行 git pull 的部署脚本,但它返回此错误;

origin  git@github.com:sayopaul/autodeploy-tutorial.git (fetch)
origin  git@github.com:sayopaul/autodeploy-tutorial.git (push)
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

【问题讨论】:

  • PHP(或网络服务器)是否以与您通过 SSH 连接到服务器时使用的用户相同的用户身份运行。如果没有,它可能无法访问您的用户 SSH 密钥来对 GitHub 进行身份验证。
  • 嗯,非常感谢。这是有道理的。 PHP(网络服务器)作为用户 www-data 运行,而我用来通过 SSH 连接到服务器的用户是不同的。但是,如果 SSH 密钥有问题,我希望错误将是“Permission denied (Public Key)”。请问有没有办法可以更改 PHP 脚本的用户以进行测试?
  • 它有点编码在 “确保您拥有正确的访问权限” 但我同意,可以更好地表述错误。我能想到一些事情,一个简单的就是利用sudo:添加这条规则:www-data ALL=(yourotheruser) NOPASSWD: /usr/bin/git,然后像这样调用git:sudo -u yourotheruser git pull
  • sudo 方法仅以另一个用户身份运行一个命令(您可以通过仅允许访问在正确目录中运行git pull 的自定义脚本来进一步限制潜在的损害)。另一种方法是更改​​运行 PHP 进程本身的用户,使用 php-fpmITK MPM
  • 你好!有用 !我终于用你的建议让它工作了。你是对的,用户 www-data 没有访问 ssh 密钥的权限。我运行了“sudo visudo”并添加了规则 www-data ALL=(yourotheruser) NOPASSWD: /usr/bin/git 就像你说的那样,然后用 sudo -u yourotheruser git pull 调用了 git,现在它运行成功了。非常感谢你 ! ??????????

标签: php git shell-exec


【解决方案1】:

PHP(网络服务器)可能不会以您通过 SSH 连接到服务器时使用的用户身份运行。因此,它没有访问/权限/没有使用正确的 SSH 密钥来验证 GitHub。

我能想到 2 个简单的解决方案:


在 sudo-conf (sudo visudo) 中添加此规则,以允许用户 www-datayourotheruser 运行(仅)/usr/bin/git

www-data ALL=(yourotheruser) NOPASSWD: /usr/bin/git

现在您可以使用以下方法调用git

sudo -u yourotheruser git pull

安全建议:如果有人设法通过www-data 执行任意代码,则限制可能造成的损害:

创建一个属于yourotheruser 的脚本(其他人不可写),例如/home/yourotheruser/deploy.sh 内容:

cd /path/to/repo
git pull

并且只允许sudo 访问这个脚本。这样,除了pull 之外,在预期目录中没有其他git 操作可以执行。


  • 更改执行用户 PHP 本身

【讨论】:

  • 再次非常感谢您!花了这么多时间,我很高兴它现在可以工作了。 ?
猜你喜欢
  • 2021-09-23
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 2016-10-11
  • 2016-05-29
  • 2016-07-28
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多