PHP Github拉出脚本错误'权限被拒绝(publickey)'

Tom*_*Tom 7 php apache git ssh github

我已经设置了一个PHP脚本来执行GitHub拉取:

这包含在我的Github文件夹中 /home/mysite/public_html/github

github_pull.php

<?php
echo `git pull 2>&1`;
?>
Run Code Online (Sandbox Code Playgroud)

我的服务器已经拥有SSH公钥,就好像我git pull从终端执行:

ssh username@host.com
cd public_html/github
git pull
Run Code Online (Sandbox Code Playgroud)

这成功(但我必须先输入rsa 密码的密码)更新:不再需要密码(请参阅注释)

但是,当我运行时,github_pull.php我收到以下错误:权限被拒绝(publickey).致命:远程端意外挂断

SSH密钥包含在 /home/mysite/.ssh/id_rsa

我跑的时候

<?php echo `whoami`;
Run Code Online (Sandbox Code Playgroud)

它输出 mysite

Von*_*onC 3

正如评论所述,首先尝试 https 网址:

 ssh username@host.com
 cd public_html/github
 git remote set-url origin https://github.com/username/reponame
 git pull
Run Code Online (Sandbox Code Playgroud)

这比修改 ssh 密钥容易得多,尤其是当它们受密码保护时。


如果您必须使用 ssh 密钥,那么您必须知道密钥的默认位置是:

~/.ssh/id_rsa(.pub)
Run Code Online (Sandbox Code Playgroud)

如果执行脚本的用户是“ mysite”,那么它将查找~mysite/.ssh/id_rsa.
并且您需要确保ssh-agent以用户身份运行mysite这就是为什么一开始使用不受密码保护的私钥进行尝试会更容易。

如果您的 ssh 密钥在其他地方,那么您将需要:

~mysite/.ssh/config
Run Code Online (Sandbox Code Playgroud)

在该配置文件中,如此处所示,您可以指定要使用的密钥的位置和名称。