问题
I have set up ssh keys properly and added them to my github account . Whenever I ssh into the server and run git pull , everything runs normally and it pulls changes from the repository . However I have a deploy script that runs git pull via shell_exec() but it returns this error;
origin git@github.com:sayopaul/autodeploy-tutorial.git (fetch)
origin git@github.com:sayopaul/autodeploy-tutorial.git (push)
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
回答1:
PHP (the webserver) likely doesn't run as the same user you use when you SSH into the server. Thus, it doesn't have access/permission / doesn't use the correct SSH keys to authenticate vs GitHub.
I can think of 2 easy solutions:
- Utilize sudo:
Add this rule in the sudo-conf (sudo visudo
) to allow the user www-data
to run (only) /usr/bin/git
as yourotheruser
:
www-data ALL=(yourotheruser) NOPASSWD: /usr/bin/git
Now you can invoke git
using:
sudo -u yourotheruser git pull
Security advise: To limit the potential damage done if someone manages to execute arbitrary code through www-data
:
Create a script owned by yourotheruser
(and not writeable by others), e.g. /home/yourotheruser/deploy.sh
with the contents:
cd /path/to/repo
git pull
And allow the sudo
access only to this script. This way, no other git
action than pull
in the intended directory can be performed.
- Change the user PHP itself is executed with:
- Use
php-fpm
- Use the ITK MPM
- Use
来源:https://stackoverflow.com/questions/52019553/error-repository-not-found-whilst-running-git-pull-via-shell-exec-on-php-script