I've set up a PHP script to perform a GitHub pull:
This is contained in my Github folder /home/mysite/public_html/github
github_pull.php
<?php
echo `git pull 2>&1`;
?>
My server does already have the SSH public key, as if I perform git pull
from Terminal:
ssh username@host.com
cd public_html/github
git pull
This works successfully (however I do have to enter the password for the rsa key first) Update: password is no longer needed (see comments)
However, when I run github_pull.php
I get the following error:
Permission denied (publickey). fatal: The remote end hung up unexpectedly
The SSH key is contained at /home/mysite/.ssh/id_rsa
When I run
<?php echo `whoami`;
It outputs mysite
You should first try to debug using the actual 'mysite' account.
From the error log, it seems to be a remote problem, not local. Meaning SSH can actually access your private key.
I suspect github is receiving your own personal private key (via ssh-agent) and not 'mysite' public key. You can validate this by running
ssh-add -l
within your php code, or withsudo -u mysite; ssh-add -l
and comparing with what's registered in github interface.Github has covered this problem extensively: https://help.github.com/articles/error-permission-denied-publickey
As commented, try first an https url:
This is far easier than tinkering with ssh keys, especially when they are passphrase protected.
If you must use ssh keys, then you must know the default location of the key is:
If the user executing the script is '
mysite
', then it will look for~mysite/.ssh/id_rsa
.And you need to make sure the
ssh-agent
is running asmysite
user. Which is why it is easier at first to try it with a private key not passphrase-protected.If your ssh key were to be somewhere else, then you would need a:
In that config file, as illustrated here, you can specify the location and name of the key you want to use.
Add the following to your .ssh/config
Edit your .git/config and update the remote repo URL from
to
That should help you login to the server using the given key. Replace the path to the key in the above to the full actual path on your machine. Replace the repo name with the actual repo name. Do note that the user 'mysite' has access to the key file. You can test that using fopen from PHP and confirm.