Git WebHook won't pull (PHP)

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 to pull out is /var/www/oliverash.me/ . ./site is the folder in which Apache looks like the root of the document.

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

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

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

+1
source share
3 answers

I cannot post a comment in response to you, but I assume that you are using the * nix system. You will get permission if your apache / php daemons do not have permission to access .git/ . You can recursively change the owner / group of the .git/ directory. Or do chmod -R o+rw .git/* to give everyone (i.e. not the owner, not the group) read and write access to the git directory, which should clear the permission error you are getting.

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

While doing this, you need to keep in mind that anyone who has access to your server will be able to go to http://myurl/.git/ etc. to gain access to them. Therefore, as a precaution, I would add a .htaccess file, for example:

 order deny, allow deny from all 

in the .git directory so that apache forbids access from the web browser to everything that is there.

+4
source

You probably have a problem with permissions, maybe a couple.

  • Php page will execute as apache user
  • This user should be able to write to the git repository.
  • This user should be able to do this.
  • You did not indicate what the pull source is, but if it is, for example, git: or ssh: repo, then this user will need perms (keys, username / password, whatever) to access the remote control to perform a shutdown.
  • I just saw that he wants /var/www/.ssh, so you use remote ssh: //, but thatโ€™s fine, but since he works as an apache user (/ var / www is an apache homedir user) he is looking for keys in / var /www/.ssh which it does not find, therefore, a failure. Solutions:
    • use sudo to switch to a user who has perms and run git pull as that user (in your php, do 'sudo git pull', and in your / etc / sudoers enter a line that allows the apache user to run the git pull command )
    • configure the .ssh / config file, which tells Host that it is remote, a User for login and Identity is the path to the private key that the remote allows ssh to do and pull.
+1
source

You have a problem with the user executing the command.

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

 <?php echo `whoami`; 

This user, called apache , is usually the user of your web server that runs PHP, which then runs shell commands.

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

Run the shell command under the desired user, and the problem should disappear.

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

Alternatively, you can use suexec to execute PHP under a different user than the web server user.

In any case, you need to make sure that you have a user who can execute the git command. I donโ€™t know how you tested this yourself, the best way I know is ssh in the server field, do git pull manually and collect the necessary data such as username, homedirectory etc.

0
source

All Articles