简体   繁体   English

使用PHP从Apache服务器运行Python脚本

[英]Running Python script from Apache server using PHP

I'm working on a project with a raspberry pi to turn on and off an LED strip using a mobile device. 我正在使用树莓派开发一个项目,以使用移动设备打开和关闭LED灯条。

The last issue I'm running into is getting the scripts to run from the apache server being hosted on the raspberry pi. 我遇到的最后一个问题是让脚本从树莓派上托管的apache服务器运行。

<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>&nbsp;
      <button class="btn" name="LightOFF">Light OFF</button><br><br>
   </form>
</html>

The Python script works perfectly when being run out of the command line on the raspberry pi. 当在树莓派上的命令行中运行时,Python脚本运行完美。 But when run on the apache server, nothing happens. 但是,在apache服务器上运行时,什么也没有发生。 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. 我对linux和php很陌生,请记住这一点。

Why does the Python work locally but not on the server? 为什么Python可以在本地工作,但不能在服务器上工作?

Thanks! 谢谢!

对于任何偶然发现此问题的人,只需在文件路径前面添加sudo即可解决问题。

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

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