ERROR: Repository not found whilst running git pull via shell_exec on php script

ERROR: Repository not found whilst running git pull via shell_exec on php script

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

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 进入服务器时使用的用户 不同 运行。因此,它没有 access/permission / 不使用正确的 SSH 密钥 与 GitHub.

进行身份验证

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


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

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本身被执行