简体   繁体   English

Excute python script on Raspberry PI from Apache Laravel PHP controller

[英]Excute python script on Raspberry PI from Apache Laravel PHP controller

I have a python script on a raspberry in /home/pi/Test.py which could be called from a laravel php controller.我在 /home/pi/Test.py 的覆盆子上有一个 python 脚本,可以从 laravel php31404C103F2CDEF2C6 调用

On the Raspberry there is Raspian installed, Apache, PHP 7.3, Python在 Raspberry 上安装了 Raspian,Apache,PHP 7.3,Python

I tried it via cmdline我通过命令行尝试过

python Test.py 4444 > works python Test.py 4444 > 作品

via local php server通过本地 php 服务器

php artisan serve 

/localhost:8000/setValue/666 > works, returnvalue 0 /localhost:8000/setValue/666 > 有效,返回值 0

Then i deployed the application to the Apache2 directory然后我将应用程序部署到 Apache2 目录

/var/www/html/Project

There i called the Service again在那里我再次打电话给服务

localhost:80/Project/setValues/666 > nothing, returnvalue 1

here is my code这是我的代码

chdir('/home/pi');
$cmd('python Test.py ' . $param);
exec($cmd,$out,$status);
return $status

Executing this from the php debug server this returns "0", from apache "1"从 php 调试服务器执行这个返回“0”,从 apache 返回“1”

i tried我试过了

exec ()
shell_exec()
passthrough()

Everything the same - works from the local server, not from apache.一切都一样 - 在本地服务器上工作,而不是 apache。

I changed the owner to www-data and changed the rights to 775 in both the python script and laravel project, i also added the www-data as sudoer but nothing worked.我将所有者更改为 www-data 并将 python 脚本和 laravel 项目中的权限更改为 775,我还将 www-data 添加为 sudoer 但没有任何效果。

I think is has to do something with executable rights but what else to do?我认为必须对可执行权限做一些事情,但还能做什么?

EDIT: I found a note in the Apache error.log编辑:我在 Apache error.log 中找到了一条注释

GPIO.setup (DATA, GPIO.OUT) RuntimeError: not running on a RPi! GPIO.setup (DATA, GPIO.OUT) RuntimeError: 不在 RPi 上运行!

it seems that www-data can not access the GPIO settings, the user "pi" although can which means - how to i enable www-data to have the same rights of the user "pi"?似乎 www-data 无法访问 GPIO 设置,用户“pi”虽然可以这意味着 - 我如何启用 www-data 以拥有与用户“pi”相同的权限?

I found the answer after struggling for hours:经过几个小时的努力,我找到了答案:

first of all add www-data to gpio group首先将 www-data 添加到 gpio 组

>sudo adduser www-data gpio > not working

and then i upgraded the firmware with然后我升级了固件

> sudo rpi-update

Finally it worked from the Apache Server.最后它在 Apache 服务器上运行。 Thanks to this Posting https://raspberrypi.stackexchange.com/questions/40105/access-gpio-pins-without-root-no-access-to-dev-mem-try-running-as-root and this User https://raspberrypi.stackexchange.com/users/13650/joan it worked.感谢这个帖子 https://raspberrypi.stackexchange.com/questions/40105/access-gpio-pins-without-root-no-access-to-dev-mem-try-running-as-root和这个用户https: //raspberrypi.stackexchange.com/users/13650/joan它工作。 Thanks a lot!非常感谢!

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

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