使用apache/php以root身份运行shell脚本


Running shell script as root using apache/php

我想使用由php执行的shell脚本来更改目录的权限,因此它在apache用户下运行。不幸的是,有些目录的权限设置为root

有没有办法将此脚本作为root运行,或者我可以考虑其他替代方案?

您要查找名为sudo的工具:https://en.wikipedia.org/wiki/Sudo但最有可能的是,你应该重新思考你的应用程序架构,而不是使用sudo,因为一旦你的httpd被破坏,攻击者就可以很容易地获得root权限。

我最近发布了一个项目,该项目允许PHP获得真正的Bash shell并与之交互。在这里获取:https://github.com/merlinthemagic/MTS

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

    $shell    = 'MTS'Factories::getDevices()->getLocalHost()->getShell('bash', true);
    $return1  = $shell->exeCmd("/path/to/script");
    //the return will be a string containing the return of the script
    echo $return1;

或者您可以简单地绕过脚本,直接在PHP中通过shell发出原始命令。这种方法将允许您处理异常。