I want to use php or jquery to control gpio pins on my raspberry pi 2. I have apache installed. When i execute python files using LXTerminal, they work fine. I have made the .py files executable and added www-data to sudoers file.

This is the php code:

if (isset($_POST['ON']))
exec('sudo python var/www/gpio/on.py');
if (isset($_POST['OFF']))
exec('sudo python var/www/gpio/off.py');
<form method="post">
<button name="ON">LED On</button>
<button name="OFF">LED Off</button>

python script

import RPi.GPIO as GPIO
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, 1)

What's the mistake?

  • I've removed the js from this because it is completely off-topic here; if you are looking for help with HTTP based client-server exchanges, that has nothing to do with the pi. I'd guess your basic problem doesn't either (you should edit in your sudoers file), but someone here may have a specific recommendation for you about a better method for accessing the GPIOs from apache via python. You should also examine the apache logs and include any error reported related to this.
    – goldilocks
    Jul 20, 2015 at 19:36
  • Well, the js was working fine on windows with xampp(apache) but it doesn't seem to work on the pi. I don't know whether the problem is with js or python script or with permissions. I did examined the errors and the only errors i saw were for File Not Found, which i fixed. Jul 21, 2015 at 4:42
  • Part of my point is that the only thing that could have anything to do with the pi here is the GPIO stuff -- if by "worked fine on windows" you mean a PC, obviously that part was not included. Apache is apache and the brand of hardware you run it on is irrelevant; questions about it and sudoers etc. are better off on our larger sibling site Unix & Linux. General programming belongs on Stack Overflow.
    – goldilocks
    Jul 21, 2015 at 13:17
  • Because of the GPIO thing I'm fine with leaving this open here, but I think it's really a red herring and you need to break this down, look at the logs, etc. Otherwise you are just going to wait forever. You haven't even bothered to explain in what sense this doesn't work. If you think the js has something to do with it, you haven't bothered to check what's going on in the browser, etc. Keep in mind it's your problem and no one is going to make an effort to drag information out of you, and this is not a discussion forum. It's a Q&A format.
    – goldilocks
    Jul 21, 2015 at 13:22

3 Answers 3


Check your paths to the scripts.

It would look like your paths need to be changed from

sudo python var/www/gpio/off.py


sudo python /var/www/gpio/off.py

I would blame on permissions. Consider the user that Apache is using and make sure you grant permissions for him. When I'm testing a just wanna make sure everything works I go the the path where the web is installed (cd /var/www for instance) then just make sudo chmod 777 * -R

This should give full access to anybody anywhere, please consider how weak your system will be after that. (not sure about the syntax, I'm on a windows machine right now).

If that fixes your problems, you'll know where your problem is and you'll have to deal with permissions for the particular Apache user and the particular files.


I was having the same issue using python, Executing shell script in PHP etc so the solution is Enable python scripts on apache 2 using this guide: https://www.server-world.info/en/note?os=Debian_9&p=httpd&f=5 then go to: Application Menu > Preferences > Raspberry Pi Configuration > Interfaces > Enable Remote GPIO And then type the address to the python script on the browser your python script should work fine with RPi.GPIO or any other python GPIO library

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.