11

I was trying the Perfect Workflow, with Git, GitHub, and SSH, and i have everything set up, except running the command git pull from php.

When i run exec('git pull') i get:

Could not create directory '/.ssh'. Host key verification failed. fatal: The remote end hung up unexpectedly

If i run it in the terminal (as root) it works just fine, but i need this hook to work from the Post-Receive URL (Github).

If i do exec('whoami') i get apache.

It's a (dv) from mediatemple with CentOS.

AFRC
  • 902
  • 3
  • 9
  • 27
  • 2
    you need to make the user of the git repository the same as the user the web app is running as. – Ben Lee Feb 21 '12 at 01:41
  • 2
    And what is the `apache` user's home dir set to? `ssh` is getting the root directory of the machine as the home directory, and it can't write there as the `apache` user. (`root` obviously can, which is why it works) – Brian Roach Feb 21 '12 at 01:42
  • 1
    Thanks for the reply guys. Ben: How can i do that? Brian: i dont know. Im not a sys/admin guy. Im running on a mediatemple's (dv) with CentOS – AFRC Feb 21 '12 at 01:48
  • 1
    @AFRC have a look at my answer, I've provided some details. Hopefully it's enough to get you rolling. – quickshiftin Feb 21 '12 at 05:15
  • 1
    Answered here --> http://serverfault.com/questions/362012/running-git-pull-from-a-php-script – AFRC Feb 21 '12 at 18:39
  • A collection of considerations when attempting to run `git pull` from php ... http://jondavidjohn.com/b/7m – jondavidjohn Oct 05 '12 at 21:38
  • @AFRC Any chance you can accept my answer here? I do apologize for the `chmod`/`chown` mixup, but it was like 4/5am when I posted the answer lol. Also I read your other thread on ServerFault and there's really no need to use `sudo`. – quickshiftin Jan 28 '14 at 20:44

1 Answers1

9

If you want apache (the user) to be able to pull from git, you'll have to create an ssh key for apache, then add that to the read only keys on github.

The flow is something like this (tweak to your needs)

usermod -s /bin/bash apache
su apache
cd ~
ssh-keygen # work through the keygen dance (added a dash)

Upload (tilde here refers to apache's homedir) ~/.ssh/id_rsa.pub to github and give apache access to whichever repos it needs to pull from.

Then you can test on the server by again su'ing to apache and running the git pull

su apache
cd ~/working-copy
git clone my-project

Once that's working you should be able to run a git pull through PHP.

Tim
  • 749
  • 4
  • 14
quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • Hi quickshiftin. Thanks. When i run the command `chmod apache -s /bin/bash` i get: _chmod: cannot access `apache': No such file or directory_ . Sorry, but im really new to this server admin/unix thing. – AFRC Feb 21 '12 at 12:16
  • 1
    Whoops, that should be `chmod -s /bin/bash apache`. It's setting the apache user's shell to bash so that you can su to apache and test git operations before moving on to running them through PHP; I'll change it in my answer. – quickshiftin Feb 21 '12 at 16:23
  • Still get chmod: cannot access `apache': No such file or directory – AFRC Feb 21 '12 at 16:58
  • 1
    dear god; i need to stop posting so late at night! – quickshiftin Feb 21 '12 at 17:21