Sign up ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

I found this code somewhere, that can execute Python scripts from PHP.

I've installed apache2 on the Pi and I'm able to access the PHP script. But when I press the HTML buttons, nothing happens! :(

When I execute the python script via SSH, it works.. so theres nothing wrong with the python scripts I think.

I'm using WebIOPi to monitor the status of the GPIO pins.

Whats the mistake?

PS: I know that this may not be a very safe approach to control the GPIO. I just wanna give it a try though :)

TIA.

<html>
<head>
<?php 
if (isset($_POST['RedON']))
{
exec('sudo python /var/www/red_on.py');
}
if (isset($_POST['RedOFF']))
{
exec('sudo python /var/www/red_off.py');
}
if (isset($_POST['YellowON']))
{
exec('sudo python /var/www/gpio/yellow_on.py');
}
if (isset($_POST['YellowOFF']))
{
exec('sudo python /var/www/gpio/yellow_off.py');
}
if (isset($_POST['GreenON']))
{
exec('sudo python /var/www/gpio/green_on.py');
}
if (isset($_POST['GreenOFF']))
{
exec('sudo python /var/www/gpio/green_off.py');
}
?>

  <title></title>
</head>
<body>
<form method="post">
  <table
 style="width: 75%; text-align: left; margin-left: auto; margin-right: auto;"
 border="0" cellpadding="2" cellspacing="2">
    <tbody>
      <tr>
        <td style="text-align: center;">Turn LED on</td>
        <td style="text-align: center;">Turn LED off</td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="RedON">Red On</button></td>
    <td style="text-align: center;"><button name="RedOFF">Red Off</button></td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="YellowON">Yellow On</button></td>
    <td style="text-align: center;"><button name="YellowOFF">Yellow Off</button></td>
  </tr>
  <tr>
    <td style="text-align: center;"><button name="GreenON">Green On</button></td>
    <td style="text-align: center;"><button name="GreenOFF">Green Off</button></td>
  </tr>
</tbody>
  </table>
</form>
</body>
</html>
share|improve this question
    
I understand that this is a permission issue. Any suggestions to resolve the same is much appreciated :) –  Krish May 11 '13 at 6:21

3 Answers 3

up vote 2 down vote accepted

The command sudo python XYZ will be execute as the apache user. You'll need to add this user to your sudoers file.

sudo echo "apache ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

Replace "apache" with your systems apache username (may be "httpd" or "www" on some systems.)

Suggestion

Why use Apache or PHP at all? SimpleHTTPServer is extremely simple to set-up. You can have this server handle your python scripts, while running with super-user privileges.

share|improve this answer
    
Worked! Thanks. The username was www-data in my case. :) And yeah, Apache2 is heavy! Thanks for the suggestion too. –  Krish May 13 '13 at 18:57

sudo python /var/www/red_on.py most probably asks for a sudo password, which is not provided anywhere, and the command fails.

you may try echo <password> | sudo -S ..., however I haven't checked if output redirect works well in the PHP exec().

share|improve this answer
1  
This will work. From man pages, you'll need to run sudo with this -S switch to read password from stdin. echo <password> | sudo -S ... –  emcconville May 13 '13 at 18:52

Try running the following commands

chown root:root ./script
chmod 4775 ./script
share|improve this answer

protected by Steve Robillard Nov 21 '14 at 15:37

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.