如何使用php从Apache执行根文件夹中的脚本


How to execute scripts in root folder from Apache using php

>我需要运行一个python脚本,在此之前我需要激活一个虚拟环境才能运行脚本,它位于根文件夹中。这是执行此操作的命令(不使用源命令)

exec(root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py).

如何从 Apache 执行。我在 php 中收到 126 错误。我已经给出了实际的路径。

126 错误表示 - "调用的命令无法执行。权限或命令不可执行的问题。

我该怎么做。实际路径在根文件夹中,我无法将其移动到根目录之外。

<?php
  $op=exec('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool     /box_upgrade.py',$output,$return);
  echo "Dir returned $return, and output:'n";
  var_dump($output);
  echo $return;  
  echo $op;
    ?>

有什么建议吗?谢谢。

我最近发布了一个项目,允许PHP获取并与真正的Bash shell交互(如果需要,则作为root),它解决了exec()和shell_exec()的限制。在这里获取:https://github.com/merlinthemagic/MTS

下载后,您只需使用以下代码:

$shell    = 'MTS'Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py');
//the return will be a string containing the return of the command
echo $return1;

在安全性方面,它比以root身份运行apache要好得多。但是让PHP靠近根目录总是很棘手的。

我构建的项目通过以下两种方式之一实现根 bash shell:

1)你允许apache有权sudo python。

2) 每次需要具有根设置的 shell 时,您都将根凭据传递给对象。

选择你的毒药。 :)阅读文档。