Search code examples
phpgitshell-exec

ERROR: Repository not found whilst running git pull via shell_exec on php script


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  [email protected]:sayopaul/autodeploy-tutorial.git (fetch)
origin  [email protected]: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.

Solution

  • 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:


    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: