无法使用 PHP 的 shell_exec 调用 bash 文件

问题描述 投票:0回答:1

我正在管理一台 Rocky Linux 8 服务器。 我希望能够从网页执行 bash 文件。

以下是有关结构的信息:

  • PHP 文件位于 /var/www/mywebsite.com/index.php 中(apache:apache CH644)
  • 我的 bash 文件位于 /root/test.sh (root:root CH755)
  • 在 php.ini 文件中,shell_exec 已从disable_functions 中删除

这是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 配置错误...

非常感谢!

php bash shell-exec
1个回答
0
投票

我通过在具有 apache:apache 权限的 web 文件夹中添加 bash 文件找到了解决方案。使用 shell_exe 并在 Web 项目之外调用 bash 文件不起作用。

我使用的结构如下:

  • /var/www/mywebsite.com/app/test.sh
  • /var/www/mywebsite.com/www/index.php

这将保护我的 bash 文件,因为无法从网络访问文件夹“app”。另外,在我的 index.php 中对我有用的良好语法是:

$test = shell_exec('sh ../app/test.sh');
echo $test;

我希望它能帮助其他人。

感谢您的帮助!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.