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 :)


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');

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

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 "www-data", "httpd" or "www" on some systems.)


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.

  • 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().

  • 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

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

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

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.