Before moving to EC2, I would push commits to a bitbucket repo and have a post-receive hook pull them to the server: after a commit, the url http://mywebsite.com/update.php is called, where update.php is:
<?php `git pull`; ?>
That used to work like a charm. However, it's not working on EC2 and I'm not sure why.
I tried the following:
sudo chmod +x update.php
This should make update.php executable.Changed update.php to
<?php `sudo git pull`; ?>
Changed update.php to
<?php `sudo -s`; `git pull`; ?>
Changed update.php to
<?php `sudo -s git pull`; ?>
I think it has something to do with permissions, because when I'm on my instance via ssh, I can run "git pull" as ec2-user. Also, "git pull" works when I'm the root user.
How can I get it to work? I think it was something to do with permissions.
Update
I did some troubleshooting (thanks for the tips @cyberx86) and found the following:
I was able to execute the update hook on the command line by running php update.php
and it worked why I was root, but not when I was ec2-user. The error log showed
error: cannot open .git/FETCH_HEAD: Permission denied
so I ran the command chmod -R 777 .git
as the root user.
Now I'm able to pull updates via git pull
and php update.php
on the command line, but it's not working when I do it through the post-receive hook or when I point my browser to url/update.php. The error log shows
Host key verification failed.^M
fatal: The remote end hung up unexpectedly
I think this means that whichever user runs the command when it's accessed via a browser doesn't have an ssh key set up, so it looks like the shell command is running, but the remote repository is not letting that user pull.
As per @cyberx86's advice, I checked out the users in /etc/passwd and while there is no PHP user, there is an user named apache: apache:x:48:48:Apache:/var/www:/sbin/nologin
and then I ran sudo -u apache php -q update.php
and got the following message
Could not create directory '/var/www/.ssh'.
The authenticity of host 'bitbucket.org (207.223.240.181)' can't be established.
RSA key fingerprint is REDACTED
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/var/www/.ssh/known_hosts).
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
So it seems like it's a matter of setting up a ssh key for the user (presumably apache) that runs the shell script via browser.