7

Setup:

  • Raspberry Pi
  • nginx web server
  • PHP5 & PHP-CLI installed

My Python script "lights.py" is really cool because it turns on/off lights in my living room through a relay connected to an Arduino Uno [then connected to the Pi via USB]. I know I could've used the Pi's GPIO pins, I just didn't. That doesn't matter here anyway.

I wanted to be able to activate the script from a web browser through the site hosted by my Pi, so I have /var/wwww/test/lights.php containing this code:

<?php
 exec('python lights.py');
?>

Simple, no? Well, when I browse to that page in a browser, nothing shows up (expected) but the lights don't change state (unexpected). However, at the command line, logged in as user Pi, I can run "php /var/wwww/test/lights.php" and it works just fine!

I imagine this is because nginx seems to use this user called www-data to do stuff, so maybe its a permissions issue? Now I'm wandering into unknown territory for me ... I tried "su - www-data" to see if I could attempt the script as that user, but it asks for a password, which I never set up (and a blank password didn't work).

Any help here is greatly appreciated.

UPDATE - Here is "ls -la /var/www/test/lights*"

-rw-r--r-- 1 www-data root  37 Feb  1 23:56 /var/www/test/lights.php
-rwxr-xr-x 1 www-data root 129 Feb  1 23:51 /var/www/test/lights.py

SECOND UPDATE - Check it:

pi@raspberrypi ~ $ sudo su - www-data
$ pwd
/var/www
$ php ./test/lights.php
python: can't open file 'lights.py': [Errno 2] No such file or directory
$ python ./test/lights.py
Traceback (most recent call last):
  File "./test/lights.py", line 4, in <module>
    ser = serial.Serial('/dev/ttyACM0', 9600)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in __init__
    self.open()
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 276, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0'

That '/dev/ttyACM0' device is my Arduino connection, so it appears the user "www-data" doesn't have access to run that Python script, since it can't output to that file. Am I able to "chmod" that device?

THIRD UPDATE - I feel like we're almost at the end!

Using "ls -g /dev/ttyACM0" I found that it is owned by group "dialout." Using "grep dialout /etc/group" I found that only user "Pi" is assigned to it. Therefore, I added www-data to that group with "usermod -a -G dialout www-data"

Now, check it:

pi@raspberrypi ~ $ sudo su - www-data
$ cd test
$ php lights.php
$ python lights.py
$

Both the PHP and Python script work, and the lights went on and off! BUT loading the web page "lights.php" from a browser still doesn't do anything!

FOURTH UPDATE I changed the PHP file to this:

<?php

exec('python lights.py', $output, $return);

print "Output:";
print $output;
print "<br />";
print "Return:";
print $return;

?>

From what I gather, that's the proper way to get some debug info from the exec() statement. Here's the output when I refresh the webpage:

Output:Array
Return:1

Does that help me at all?

4

5 回答 5

3

None of the given answers helped me... in fact, my updates to my posts log what was ultimately the answer, which I found through my own trial/error and Googling.

I needed to assign user www-data into the dialout group in order to have access to /dev/ACM0, and a reboot was required (of the whole server, not just nginx for some reason).

I don't know why everybody kept answering that this was a path issue, WHEN I KEPT SAYING THE PYTHON SCRIPT RAN FINE FROM SHELL, AND THE PHP FILE RAN FINE FROM PHP-CLI.

Anyway, that's what solved it in the end. Thanks to everyone for trying.

于 2013-02-05T18:02:33.817 回答
3

PHP 可能在您的网站配置中配置为以安全模式运行。在安全模式下运行时,某些功能exec会被禁用。尝试禁用安全模式。

另一种可能性是lights.py在您的 Web 服务器运行的目录中找不到该脚本。要解决这个问题,请使用脚本的完整路径,例如exec("/usr/bin/python /home/armani/lights.py").

另一种可能性是,当您登录时,Web 服务器正在以与您不同的用户身份运行(可能是nouser)。并且该用户可能无权访问某些资源。

而且可能还有更多这样的可能错误。请提供有关您遇到的错误的更多信息(尝试在 PHP 中启用调试以获取回溯)。

于 2013-02-03T00:03:26.753 回答
1

您将必须检查哪个是当前工作目录,如果它不是您想要更改的目录,或者更简单,只需使用 python 执行具有完整路径的脚本

exec('python /var/www/test/lights.py');

于 2013-02-05T15:38:58.773 回答
1

您的 light.py 文件的 chmod 是什么?您可以尝试以下命令吗?

chmod +x lights.py
于 2013-02-03T00:00:16.623 回答
1

请检查您的路径是否正确,php 脚本将从 /var/wwww/test 目录中运行。lights.py 文件是否也位于该目录中?如果不是,请指定文件的完整路径

于 2013-02-03T00:04:47.187 回答