使用PHP从Apache服务器运行Python脚本 [英] Running Python script from Apache server using PHP
问题描述
我正在与树莓派合作开发一个项目,以使用移动设备打开和关闭LED灯条.
I'm working on a project with a raspberry pi to turn on and off an LED strip using a mobile device.
我遇到的最后一个问题是让脚本从树莓派上托管的apache服务器运行.
The last issue I'm running into is getting the scripts to run from the apache server being hosted on the raspberry pi.
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<?php
if (isset($_POST['LightON'])) {
exec("sudo python /home/pi/lightOn.py");
}
if (isset($_POST['LightOFF'])) {
exec("sudo python /home/pi/lightOff.py");
}
?>
<form method="post">
<button class="btn" name="LightON">Light ON</button>
<button class="btn" name="LightOFF">Light OFF</button><br><br>
</form>
</html>
当在树莓派上的命令行中运行时,Python脚本运行完美.但是,当在apache服务器上运行时,什么也没有发生.我在这里做错了什么?它与权限有关吗?我对linux和php很陌生,请记住这一点.
The Python script works perfectly when being run out of the command line on the raspberry pi. But when run on the apache server, nothing happens. What am I doing wrong here? Does it have something to do with permissions? I'm very new to linux and php, please keep that in mind.
为什么Python可以在本地工作,但不能在服务器上工作?
Why does the Python work locally but not on the server?
谢谢!
推荐答案
对于任何偶然发现此问题的人,只需在文件路径前面添加 sudo
即可解决此问题.
For anyone that stumbles upon this, the problem was simply fixed by adding sudo
in front of the file-path.
这篇关于使用PHP从Apache服务器运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!