I have a PHP file, hook.php
, that looks like this:
The file is located in /var/www/ol
You've certainly got a permissions issue, maybe a couple.
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.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. .
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.
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