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

标签 php python linux apache

我正在使用 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 脚本在 raspberry pi 上的命令行运行时完美运行。但是在 apache 服务器上运行时,什么也没有发生。我在这里做错了什么?跟权限有关系吗?我是 linux 和 php 的新手,请记住这一点。

为什么 Python 在本地运行而在服务器上运行不正常?

谢谢!

最佳答案

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

关于php - 使用 PHP 从 Apache 服务器运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40498208/

相关文章:

php - OctoberCMS:如何防止将提交的表单保存到数据库中?

javascript - 动态更新网页上的值

php - 从数组读取但无法找到匹配项

python - 无法解析功能 : goog:chromeOptions from invalid argument: unrecognized chrome option: prefs

python 脚本不从 cron 运行

python - 需要帮助连接类中的 Connections

Linux/银光问题

python3.3在linux中找不到libpython3.3m.so(pip-3.3)

php - 可以在公共(public)服务器上安装 sql 吗?

linux - 将数值结果从单个文本文件分离到 linux 中的多个文件