washichi
washichi

Reputation: 100

raspberry pi: Can't execute script.py from PHP

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:

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.

Upvotes: 1

Views: 2688

Answers (2)

Christophe Moine
Christophe Moine

Reputation: 1076

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

Upvotes: 0

christian
christian

Reputation: 180

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.

Upvotes: 2

Related Questions