I have made a very small script for my Raspberry Pi where I turn on the LED.

import RPi.GPIO as GPIO
import time

The script works I checked it with sudo python led.py

I'm using apache server on my rapberry pi. Python version is 2.7.3


                echo "on"
                exec('sudo python /var/www/led.py');
        else if(isset($_GET['off'])){
                echo "off";

                <form method="get">
                        <button name="on">on</button><br>
                        <button name="off">on</button><br>

I tried with echo exec(whoami);

1 Answer 1


It is all to do with permissions.

The scipt works locally because it is run as the (presumably) pi user. The pi user has (passwordless) access to the sudo command to run programs as root. The RPi.GPIO module requires to be run as root.

It fails in your script because it runs as the (Apache) www-data user. The www-data user in not a member of sudoers so can not run sudo. Thus the RPi.GPIO script fails as you don't have root access.

There are ways around this but all the ones I know of are pretty insecure and will compromise the security of your Pi.

  • I only use my pi on my own network... Do you know another method for enabling remote controk
    – GProduct
    Jul 24, 2015 at 17:25
  • @GProduct Google will reveal dozens of ways depending on how heavyweight a solution you want. Personally I use my own pigpio Python module as scripts run as an ordinary user (no sudo required) and communicate with a daemon which runs as root.
    – joan
    Jul 24, 2015 at 17:42
  • Okay I'm gonna take a look at it
    – GProduct
    Jul 24, 2015 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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