Git pull from php

August 11, 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> select server-> Overview-> Show Password):


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

If succes now you can pull the repository from php:

exec('git pull 2>path_to_error_file');