1

I am running a PHP document on an Apache server on my Raspberry Pi and I want it to run a file when a button is clicked. I put some echo commands under the command to make the file run and it prints out but the file doesn't run. The index.php file and lightson.py and lightsoff.py files are all in the same directory (/var/www) and I have added #!/usr/bin/env python to the top of both files and made them executable by using chmod +x lightson.py. If I run the command from the shell it works and turns on the light just like I want with the exact same command as in the file but yet it wont run through the command. The code:

<html>
<head>
<title>Light Controller</title>
</head>


<?php
if (isset($_POST['LightON']))
{
shell_exec("sudo python /var/www/lightson.py");
echo("on");
}
if (isset($_POST['LightOFF']))
{
shell_exec("sudo python /var/www/lightsoff.py");
echo("Off");
}
?>

<form method="post">
<button name="LightON">Light ON</button>&nbsp;
<button name="LightOFF">Light OFF</button><br><br>
</form> 


</html>
Bruh
  • 11
  • 1
  • 1
  • 5

2 Answers2

2

as you said you are running it like apache->php->shell_exec(SUDO..)

So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program

put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..

then

change group:

chgrp www-data /path/to/python-script.py

make it executabel

chmod +x /path/to/python-script.py

try it

shell_exec("/path/to/python-script.py");

I hope it works ;)

TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment

webdeb
  • 12,993
  • 5
  • 28
  • 44
  • So how would I give apache www-data rights? @webdeb – Bruh Sep 16 '15 at 02:27
  • I did all of that but it still is not wanting to do it. @webdeb – Bruh Sep 16 '15 at 02:42
  • 1
    Using the ls -l command, it gives me the following permisions for each file: -rw-r--r-- 1 root root 391 Sep 16 02:46 index.php, -rwxr-xr-x 1 root www-data 654 Sep 16 01:11 lightson.py, -rwxr-xr-x 1 root www-data 653 Sep 16 01:10 lightsoff.py @webdeb – Bruh Sep 16 '15 at 02:48
  • `chgrp www-data /path/to/python-script.py` `www-data` haven't any idea writing to `hardware pins`. Write a local service (or single line python shell command(only startwith `sudo`)) , and send to shell or service. don't play with file permission... – dsgdfg Sep 16 '15 at 05:13
  • @AaronScottVigal www-data should be able to execute python scripts you can add www-data into sodoers only for python scripts: `www-data ALL=(root) NOPASSWD: /usr/bin/python` and do it with sudo – webdeb Sep 21 '15 at 09:00
  • @AaronScottVigal One Tip: Use node.js instead of apache + php.. It's not done for such things.. Here's how simple you can setup node.js on RaspberryPi and you can even trigger the IO Pins over there.. http://weworkweplay.com/play/raspberry-pi-nodejs/ – webdeb Sep 21 '15 at 09:04
1

This worked for me:

test.php

<?php
    echo shell_exec("python test.py");
?>

test.py

f = open("test.txt", "a+")
f.write("hiya buddy!!\n")
f.close()
print "some output"

Here's my relevant ls -l output from /var/www/html:

jason@Jason-one /var/www/html $ ls -l
-rw-r--r--  1 jason    jason    44 Sep 20 18:12 test.php
-rwxr-xr-x  1 jason    jason    82 Sep 20 17:44 test.py
-rw-rw-rw-  1 jason    jason    38 Sep 20 18:15 test.txt

Since I don't have GPIO pins on my laptop, I decided to write to a file as a test. Notice I didn't have to use sudo because of the way I set the permissions on test.py.

Jason
  • 2,725
  • 2
  • 14
  • 22
  • This didn't work for me... would the correct ls -l output be root instead of jason since i'm running the default pi user on my raspberry pi? – Bruh Sep 20 '15 at 22:31
  • `root` should be fine as long as the python file's permissions has the three `x`'s in it. Did you try my code and write to the file, or did you try to manipulate the GPIO pins by using this code as a guide? – Jason Sep 20 '15 at 22:34
  • I tried to manipulate the GPIO pins using my code... should I use the code you put above and put my code in the .txt file ? – Bruh Sep 20 '15 at 22:36
  • No, the text file was just used for test output to make sure that my php file was making my python script run. If you test my code and file permissions that I provided above, we could verify that php was running your python script. If the python script runs, then the problem is likely a permission issue with the GPIO pins. Set up those three files and then run the php script. (`text.txt` doesn't need anything in it when you create it, but you do need to create it to set up the permissions for it.) – Jason Sep 20 '15 at 22:42
  • I copied that exact same script and after opening the page the .txt file is still blank @Jason – Bruh Sep 20 '15 at 22:48
  • Was there any output in the browser? – Jason Sep 21 '15 at 00:01
  • Nope, none at all. @Jason – Bruh Sep 21 '15 at 00:11
  • And you're not using `sudo` in the `shell_exec` call? – Jason Sep 21 '15 at 00:21
  • No. I copied exactly what you put @Jason – Bruh Sep 21 '15 at 00:23
  • This file might have some useful information: `/var/log/apache2/error.log` – Jason Sep 21 '15 at 00:48
  • I have exactly the same problem. I tried everything in this thread, the error.log file is empty. My ls -l looks like this: "-rwxr-xr-x 1 pi pi 113 Dec 10 17:26 action_lamps.php" "-rwxr-xr-x 1 pi www-data 152 Dec 10 2017 testPython.py" If I run the script in my console like: "php action_lamps.php" it works! As a test the testPython.py is writing the current time to a file with the permissions: "-rw-r--r-- 1 pi pi 38 Dec 10 17:37 testFile.txt". Did you finally manage to solve the problem? –  Jan 09 '18 at 14:07