简体   繁体   English

Raspberry PI:PHP使用sudo调用python脚本

[英]Raspberry PI: PHP call python script with sudo

i'm running Nginx on my Raspberry PI with PHP 5.4.14 (fpm-fcgi). 我在Raspberry PI上使用PHP 5.4.14(fpm-fcgi)运行Nginx。 It all works well. 一切都很好。 But in one script i make a shell call using shell_exec. 但是在一个脚本中,我使用shell_exec进行了shell调用。

echo shell_exec("sudo python " . $file);

it works well if i use the console with 如果我将控制台与

php test.php

but if i use the browser to call this file the php-file will be executed but i don't get any output by the shell_exec (not even an error message). 但是,如果我使用浏览器来调用该文件,则将执行php文件,但shell_exec不会得到任何输出(甚至没有错误消息)。

So i hope you could help me fix this problem. 因此,我希望您能帮助我解决此问题。

The called python file makes use of the GPIO so sudo is required, right? 调用的python文件使用GPIO,因此需要sudo,对吗?

Here is my python code: 这是我的python代码:

#!/usr/bin/env python
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print "Error Importing GPIO. Did your forgot the superuser privilieges?"

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

GPIO.setup(3, GPIO.OUT)

GPIO.output(3, not GPIO.input(3))

print "New LED state is: ", GPIO.input(3)

#GPIO.cleanup()

for future reference: 备查:

I found a - i my eyes - not really nice solution: 我找到了-我的眼睛-不是很好的解决方案:

sudo echo "apache ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

Found here . 这里找到。

Mm, i have try with this two simple example: 嗯,我尝试使用以下两个简单示例:

/Scripts/test.py /Scripts/test.py

#!/usr/bin/env python
print (str('hello world'))

/var/www/test.php /var/www/test.php

<?php
echo shell_exec('/Scripts/test.py');
?>

via a browser, show me "hello world" 通过浏览器,向我显示“你好,世界”

Perhaps try this: 也许试试这个:

chmod a+x your_py_file.py

Hope that help.. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM