我正在管理一台 Rocky Linux 8 服务器。 我希望能够从网页执行 bash 文件。
以下是有关结构的信息:
这是test.sh文件的内容:
#!/bin/bash echo 'hello';
1/ 从我的 root 用户控制台,当我执行“/root/test.sh”时,它工作正常!
2/ 从我的 PHP 文件中,如果我运行以下代码,它也可以工作。
$test = shell_exec('date'); echo $test;
3/ 从我的 PHP 文件中,如果我运行以下代码,它不起作用:
$test = shell_exec('/root/test.sh'); or $test = shell_exec('sh /root/test.sh'); or $test = shell_exec('/usr/bin/sh /root/test.sh'); echo $test;
它不起作用,这太令人沮丧了......
我已经搜索了几个小时,但没有找到任何解决方案。 您能帮我解决这个问题吗? 也许是 Apache 配置错误...
非常感谢!
我通过在具有 apache:apache 权限的 web 文件夹中添加 bash 文件找到了解决方案。使用 shell_exe 并在 Web 项目之外调用 bash 文件不起作用。
我使用的结构如下:
这将保护我的 bash 文件,因为无法从网络访问文件夹“app”。另外,在我的 index.php 中对我有用的良好语法是:
$test = shell_exec('sh ../app/test.sh');
echo $test;
我希望它能帮助其他人。
感谢您的帮助!