Git pull from php

Posted on June 10, 2020

(Actually we could create the ssh key inside vult server connected with ssh from our console, so we can copy it and paste at github, but I need to test it first)

Check if there is already a ssh key in your server:

ls -al ~/.ssh

Create a new ssh key in your machine:

ssh-keygen -t rsa -b 4096

Get the private key and copy it from your console:

cat id_rsa

Add the new key to github repository as deployment key https://github/user/repo/settings/keys

Enter in vultr server with ssh

(To see the root password: got at https://my.vultr.com/-> select server-> Overview-> Show Password):

ssh root@server-address.com

Inside vultr server, create a ssh folder for www-data user and add the private key

cd /var/www
# www-data need write access to the repo folder
chown -R www-data:www-data repo
mkdir .ssh
chown -R www-data:www-data .ssh
nano .ssh/id_rsa
# paste here the content of private and save the file

Now confirm that www-data user can connect to git

sudo -u www-data ssh -T git@github.com

If succes now you can pull the repository from php:

exec('git pull git@github.com:user/repo.git 2>path_to_error_file');

 

https://www.vultr.com/docs/how-do-i-generate-ssh-keys/

https://help.github.com/en/enterprise/2.15/user/articles/adding-a-new-ssh-key-to-your-github-account