1

update:

after advice in the comments I found my error.log (/var/log/apache2/error.log):

Traceback (most recent call last):
File "/var/www/html/nano.py", line 7, in <module>
GPIO.setup(17,GPIO.OUT)
RuntimeError: No access to /dev/mem.  Try running as root!

I solved my problem by adding sudo:

<?php
echo exec("sudo /usr/bin/python /var/www/html/nano.py");
?>

but I still don't understand it. I need to run it as sudo when I call nano.py from my webserver, but when I run nano.py from my terminal (as pi user) I don't have to run it as sudo. I thought that I gave apache root access when I edited sudoers.

anyone who can explain this? and is this safe? (my apache server is only accessible from my own wifi network for now)


I know this question has been asked and answered many times, but I've tried every solution I could find but I still can't execute my script.py from my PHP webpage.

I can execute my nano.py script from terminal: nano.py

My apache server is also working fine (I can display text if I want)

nano.py script:

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
print "LED on"
GPIO.output(17,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(17,GPIO.LOW)

I have a python script called nano.py located at: /var/www/html/nano.py (-rwxrwxrwx 1 pi www-data 236 Jan 26 13:12 nano.py) and /home/pi/nano.py (-rwxr-xr-x 1 pi pi 225 Jan 26 13:19 nano.py) the scripts are both the same:

<?php
    echo shell_exec("python /var/www/html/nano.py");
?>

I tried many things, but I can't execute nano.py from my PHP page:

  • tried different paths(/var/www/html/nano.py /home/pi/nano.py nano.py)
  • tried shell_exec, exec
  • tried different python paths (python /usr/bin/python)

I tried giving apache sudo access:

www-data ALL=(ALL) NOPASSWD:ALL

I don't really know what other options I have, So I hope someone can help me.

1

2 Answers 2

2

Try

<?php
    echo shell_exec("/usr/bin/python /var/www/html/nano.py");
?>

Most likely your python binary can't be found. Also make sure these functions are not blocked by your php.ini.

If that doesn't work, /var/log/apache2/error.log can tell you more about the error.

Sign up to request clarification or add additional context in comments.

2 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
tried that, and many other options: #echo shell_exec("/usr/bin/python /var/www/html/nano.py"); #echo shell_exec("python /var/www/html/nano.py"); #echo shell_exec("/usr/bin/python /home/pi/nano.py"); #echo shell_exec("python /home/pi/nano.py"); echo exec("/home/pi/nano.py"); ?>
0

The tomcat server is most likely run under the "tomcat" user account. It is probably why you need sudo command.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.