1

I need to use git commands in a php project.

Example :

exec('git clone <repo>');

The problem is that the command (used via an exec()) asks for my username and password. Which I don't have to enter when I run the command in a shell since I am using an ssh key.

I guess the problem appear because www-data does not know or does not have access to my ssh key?

Do you know any solution for that problem?

Thanks!

  • 3
    PHP, when accessed through a browser, is run as a specific user on the machine, and by default, this user is almost never "you". So when you run this command, git looks in the config directory of the current user (once again, not you) and can't find anything which is why it prompts. You can either run PHP as you, which probably isn't a good idea and also depends on your platform, or you can configure the current using PHP, or you can run some [extra commands](https://stackoverflow.com/q/4565700/231316) to get the shell aware of the context. – Chris Haas Aug 12 '21 at 17:04
  • 2
    You can e.g. copy your ssh key to `/var/www/.ssh/id_rsa` (or whatever that user's home dir is configured as) so www-data's default identity is 'you' - kinda depends why you want to do this e.g. is this a public service? a hobby project? Some internal tool? Please add a little more context about why you need to do this. I sure hope you aren't piping invalidated user input into these exec calls :). – AD7six Aug 12 '21 at 17:40
  • Thank for you answer. It seemed to me that this was the problem. I will do that! Thank for your help –  Aug 12 '21 at 17:54

0 Answers0