【问题标题】:Running Python script from Apache server using PHP使用 PHP 从 Apache 服务器运行 Python 脚本
【发布时间】:2017-03-22 17:54:29
【问题描述】:

我正在使用 raspberry pi 进行一个项目,以使用移动设备打开和关闭 LED 灯条。

我遇到的最后一个问题是让脚本从托管在树莓派上的 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>

Python 脚本在运行树莓派上的命令行时可以完美运行。但是当在 apache 服务器上运行时,什么也没有发生。我在这里做错了什么?它与权限有关吗?我对linux和php很陌生,请记住这一点。

为什么 Python 可以在本地工作,而不能在服务器上工作?

谢谢!

【问题讨论】:

    标签: php python linux apache


    【解决方案1】:

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

    【讨论】:

    • 我认为最好给 apache 用户(通常是 www-data)适当的权限以直接运行它。
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多