1

I have a PHP file, hook.php, that looks like this:

<?php `cd .. && git pull`;

The file is located in /var/www/oliverash.me/site/. However, the git repository that needs to be pulled is /var/www/oliverash.me/. ./site is the folder Apache looks to as the document root.

When I run the file in my browser, it does not seem to be pulling the repository.

I have also tried to echo the result, but the page is blank.

<?php echo `cd .. && git pull`;

4 Answers4

5

I can't post a comment in reply to you, but I am assuming that you are running a *nix system. You will be getting a permission denied if your apache/php daemons don't have permission to access .git/. You can change the owner/group of the .git/ directory recursively. Or do a chmod -R o+rw .git/* to give everyone (ie, not owner, not group) access to read and write in the git directory, which should clear up the permissions error that you are getting.

EDIT Just re-read the question, so what follows probably isn't needed, but leaving it just in case.

Though, doing that, you need to keep in mind that anyone with access to your server will be able to go to http://myurl/.git/ etc to access those. So as a security precaution, I would add a .htaccess file like:

order deny, allow
deny from all

in the.git directory so that apache will deny access from a web browser to everything in there.

Jon
  • 4,746
  • 2
  • 24
  • 37
  • Many thanks. Almost sorted, I think. However, now I’m getting this error in `error_log`: `Could not create directory '/var/www/.ssh'.^M Host key verification failed.^M fatal: The remote end hung up unexpectedly` –  Oct 15 '12 at 21:48
  • 1
    Start off trying `mkdir /var/www/.ssh` then `chmod o+rw /var/www/.ssh` to set up the `.ssh` directory with access for all. Let me know what errors you may get after that. That should help solve the first two, and hopefully it will solve the last one since the Host key should be able to be verified after. – Jon Oct 15 '12 at 23:54
  • Now I’m just getting this: `Host key verification failed.^M fatal: The remote end hung up unexpectedly`. Why did it want a `.ssh` in `www`? –  Oct 16 '12 at 14:17
  • 1
    I think it wanted the `.ssh ` as apache's 'home' directory. What happens when you run the command from the command line? I am assuming it works correctly, but checking just in case. If it works fine, you should be able to resolve it by copying from your `~/.ssh` in to `/var/www/.ssh` and doing a `chmod 0+r *` inside that directory. – Jon Oct 16 '12 at 15:08
  • After this I’m getting `fatal: Unable to create '/var/www/oliverash.me/.git/ORIG_HEAD.lock': Permission denied`. Should I `chmod o+r* /var/www/oliverash.me`? –  Oct 16 '12 at 15:17
  • I would think most in `/var/www/oliverash.me/` should already be `o+r`, but doing `chmod o+rw /var/www/oliverash.me/.git` should solve that. – Jon Oct 16 '12 at 15:27
  • So I got it working by creating a symbolic link from `/root/.ssh` to `/var/www/.ssh`, running `chmod o+r *` inside of `.ssh`, and then finally `chmod o+rw /var/www/oliverash.me/.git`. I think I can see why this was necessary – the `git pull` command worked through SSH because I was logged in as root, and that account had the SSH keys necessary for the git remote. The `apache` user didn’t. Is that right? –  Oct 16 '12 at 15:39
  • I would be interested in getting things working using the other method @pjz suggested, whereby I had a line to `/etc/sudoers`. I think this means I won’t have to worry about permissions on `.git` on future projects. –  Oct 16 '12 at 15:40
  • The `git pull` only gets so far, however, as these errors are now appearing in my `error_log`: http://hastebin.com/fimuloloru.coffee. –  Oct 16 '12 at 15:45
  • That is correct. And adding to `sudoers` would work, but for the most part, you _never_ want your web service to have permission to run anything as the root user. It would be better to add your apache user to a group that you give `+rw` access to directories with. Though, since you created the symbolic link with `.ssh` you should have any more issue with other projects as well - just make sure you `+rw` the new `.git` directories that you end up creating. – Jon Oct 16 '12 at 15:47
  • Should I execute `chmod o+rw /var/www/oliverash.me` to solve this new error? –  Oct 16 '12 at 15:49
  • Those errors are because your apache user doesn't have permission to change within your pulled git. Do a `chmod -R o+rw` to the base directory that git is using. – Jon Oct 16 '12 at 15:50
  • You learn fast! Exactly that, might want to do it recursively though so add `-R` before the `o+rw` – Jon Oct 16 '12 at 15:50
  • You might be able to help with my next issue, too! http://stackoverflow.com/questions/12918981/git-webhook-php-cannot-access-rubygem-command –  Oct 16 '12 at 17:04
  • Just run through all of this again – to clarify, the symbolic link didn’t work, so I had to actually copy the files. –  Oct 21 '12 at 16:00
1

You've certainly got a permissions issue, maybe a couple.

  1. The php page is going to execute as the apache user
  2. That user must be able to write to the git repo in question
  3. That user must be able to do the pull in question
  4. You didn't specify what the source of the pull is, but if it's, for instance, a git: or ssh: repo, then that user will need perms (keys, username/password, whatever) to access the remote to do the pull from.
  5. Just saw that it wants /var/www/.ssh - so you're using a ssh:// remote, which is fine, but since it's running as user apache (/var/www is user apache's homedir), it's looking for keys in /var/www/.ssh, which it's not finding, hence the failure. Solutions:
    1. use sudo to switch to a user that does have perms and run the git pull as that user (in your php, do 'sudo git pull', and in your /etc/sudoers put a line allowing user apache to run the 'git pull' command)
    2. set up a .ssh/config file that specifies a Host that's the remote, a User to use to login, and an Identity that is the path to the private key that the remote will allow to ssh in and do the pull.
pjz
  • 41,842
  • 6
  • 48
  • 60
  • Is it the `apache` user or the `www` user? –  Oct 16 '12 at 15:09
  • @OliverJosephAsh: As written, the `whoami` command outputs the username, that is `apache` in your case, the `/var/www` directory is the that user's home-directory. – hakre Oct 16 '12 at 15:16
  • I tried adding `apache ALL = (root) /usr/bin/git pull` to `/etc/sudoers`. Still getting the same error in my `error_log`: `sudo: sorry, you must have a tty to run sudo`. [Here’s my most recent `hook.php`](http://hastebin.com/lapowepupe.php). –  Oct 16 '12 at 15:30
1

create webhook.php in the root or anywhere from where you can access it

$result = exec("cd /path/to/repo && git pull origin branch");

make sure the permission is 775 and user of your file and your site directory is www-data owner

0

You are having a problem with the user here that is executing the command.

According to your various comments, the system commands are executed as the user named apache (homedir is /var/www). You can verify this by running the whoami command from within your PHP script:

<?php echo `whoami`;

That user named apache is commonly the user your webserver runs under, which then runs PHP which then runs the shell commands.

Obviously you want to run the command as some other user, but you have not shared so far the information which one.

Run the shell command under the right user and the problem should go away.

On a linux system, the command to run other commands under a different user is called sudo, another one su:

Alternatively you can make use of suexec to execute PHP under a different user than the webserver user.

In any case you need to ensure that you have a user that is able to execute the git command. I have no clue how you tested that on your own, best way I know is to ssh into the server box, do the git pull manually and collect the needed data like user-name, homedirectory etc. .

hakre
  • 193,403
  • 52
  • 435
  • 836
  • `whoami` returns apache. If I precede the command with sudo, then I get the following error in my error_log: `sudo: sorry, you must have a tty to run sudo` –  Oct 16 '12 at 14:37
  • Yes the user is apache apache, www was only the basename of the homedir. And then the tty error, please see http://stackoverflow.com/questions/3173201/sudo-in-php-exec and other similar ones. http://maymay.net/blog/2010/03/17/how-to-work-around-sorry-you-must-have-a-tty-to-run-sudo-without-sacrificing-security/ – hakre Oct 16 '12 at 14:48