Run ./script.sh from PHP

310 views Asked by At

I have web server that play music from Raspberry and turn on LED with script gpio.sh. I am using mpd, mpc and gpio.

My /var/www/index.php :

<html><body><?php
echo exec('whoami');
if(isset($_POST['button1']))
{
   shell_exec('mpc play')
   shell_exec('/bin/bash /var/www/gpio.sh');
}
?>
    <form method="post">
    <p align=center>
    OUTPUT (AUDIO) => <button name="button1">PLAY</button>
    </p>
    </form>
    </body></html>

In terminal, I can run /bin/bash /var/www/gpio.sh successfully and LED turning on. From web server 'mpc play' WORKS and can play a song BUT it can't run that gpio.sh . The owner of index.php is www-data

-rwx------ 1 www-data www-data 1262 Dec 8 10:45 gpio.sh

-rwx------ 1 www-data www-data 272 Dec 9 09:39 index.php

What should I do ? When I change owner of index.php or gpio.sh to root, php can't execute. Is my index.php wrong? I can't execute .sh from php. Please help.

1

There are 1 answers

0
Scony On

Perfect solution for you would be to set SUID for script gpio.sh but unfortunately you can't do so as far as gpio.sh is a script.

You have three options:

  • You can turn your script into the say C++ application and then set SUID
  • You can use some GPIO lib for python that don't force usage by root like: pigpio
  • You can set SUID for python interpreter, but I'm not sure its good idea generally