错误:在PHP脚本上通过shell_exec运行git pull时找不到存储库

时间:2018-08-25 17:14:37

标签: php git shell-exec

我已经正确设置了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.

1 个答案:

答案 0 :(得分:1)

当您通过SSH进入服务器时,

PHP(网络服务器)可能使用的用户身份不同。因此,它没有访问权限/ 不使用正确的SSH密钥来对GitHub进行身份验证。

我可以想到2种简单的解决方案:


将此规则添加到sudo-conf(sudo visudo)中,以允许用户www-data/usr/bin/git的身份(仅)运行yourotheruser

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访问此脚本。这样,将无法在预期目录中执行除git以外的其他pull操作。


  • 更改用户PHP本身已执行,其中包括: