PHP execute sudo command with shell_exec()

Asked

Viewed 5,871 times

4

I’m having the following problem, I’m on time trying to run a file sh with php using shell_exec for example:

echo shell_exec("./example etc etc");

My problem is that it does not perform, is it a permission problem? detail that when I type: php x.php it returns right (only tested with root user)

  • Without you putting what appears in the error log, it is difficult to know. It would be nice [Edit] and add this relevant information.

  • Some commands are disabled by default. If you update the question using the real command, not an example, it will be easier to get help.

1 answer

5


If you want to run the command through the file url, example http://exemplo.com/teste.php and in this test.php has its shell_exec, you need to make sure that the Apache have permission to execute this command.

To give permission to Apache just know the user, it will depend on the Linux distribution you are using. If you are using Hundreds will be Apache even if you are using Ubuntu will be www-data.

After knowing this check which file path is where the files you want to execute the permission sudo by the Apache, then you edit the sudoers

vim /etc/sudoers

and add the following line.

apache          ALL=(root)  NOPASSWD: /caminho/da/sua/pasta

or

www-data        ALL=(root)  NOPASSWD: /caminho/da/sua/pasta

Basically you will be telling the server that your Apache is allowed to run the files from this folder as root.

  • Thanks Man, it worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.