错误:在php脚本上通过shell_exec运行git pull时未找到存储库
我已经正确设置了ssh密钥,并将它们添加到我的github帐户。每当我ssh到服务器并运行git pull时,一切都正常运行,它从存储库中提取更改。然而,我有一个通过shell_exec()运行git pull的部署脚本,但它返回这个错误错误:在php脚本上通过shell_exec运行git pull时未找到存储库,php,git,shell-exec,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) ERR
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(Web服务器)很可能与SSH进入服务器时使用的用户不同。因此,它没有访问/权限/没有使用正确的SSH密钥对GitHub进行身份验证
我可以想出两个简单的解决方案:
- 利用:
sudo visudo
)中,以允许用户www-data
作为/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
吉特拉力
并只允许sudo
访问此脚本。这样,除了在目标目录中执行pull
操作外,不能执行任何其他git
操作
- 更改用户PHP本身是通过以下方式执行的:
- 使用
php fpm
- 使用
- 使用
www-data-ALL=(yourotheruser)NOPASSWD:/usr/bin/git
,然后调用git,比如:sudo-u yourotheruser-git-pull
sudo方法只作为另一个用户运行一个命令(通过只允许访问在正确目录下运行的自定义脚本,可以进一步限制潜在的损害)。另一种方法是使用PHP fpm
或.Hello here!!它可以工作!我终于使用您的建议让它工作了。您是对的,用户www数据没有访问ssh密钥的权限。我运行了“sudo visudo”并添加了规则www data ALL=(yourotheruser)NOPASSWD:/usr/bin/git就像你说的,然后用sudo-u你的另一个用户git pull调用git,现在它成功运行了。非常感谢!再次非常感谢!花了这么多时间在它上面,我很高兴它现在可以工作了。