0

Giving php root access to run linux cli application

My server is of Ubuntu build, with Nginx & PHP5-FPM

I tried to do the following

$shell_cmd = "someprogram 'runthispath'";
shell_exec($shell_cmd);

When I tried to do so, it just won't work, because my php5-fpm is run as user "nginx" and I guess it does not have power to run someprogram.

My server is only use by me, and its for some developing of app, security is not a big concern as of now. What should I do to enable my php to run application, I read that I could do so with editing sudoer file, but I want to run the app without using "sudo" or if sudo is the only choice, I will just do it , hope to hear some advise.

Thanks!

user3504335
  • 177
  • 1
  • 13
  • 1
    Is it an option to simply change the permissions on whatever files the CLI application is modifying? – wavemode Aug 24 '14 at 03:41
  • Possible duplicate: http://stackoverflow.com/questions/8532304/execute-root-commands-via-php – wavemode Aug 24 '14 at 03:43
  • I tried edit my sudoers file with nginx ALL=(ALL:ALL) ALL and it doesn't seems working still. – user3504335 Aug 24 '14 at 03:47
  • @wavemode, i need to execute quite a few shell cli using my php through web interface php – user3504335 Aug 24 '14 at 03:48
  • @user3504335 Your PHP application isn't going to use `sudo`, so of course adding the user to the sudoers won't work. Change the permissions on what you're executing. – Brad Aug 24 '14 at 05:49

1 Answers1

0

I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

    $shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
    // you must escape the path quotes.
    $return1  = $shell->exeCmd("someprogram \"runthispath\"");
    //the return will be a string containing the return of the command
    echo $return1;
MerlinTheMagic
  • 575
  • 5
  • 16