PHP通过浏览器访问时无法执行本地程序

标签 php web exec

总的来说,我对 PHP 和 web 开发有点陌生,我正在尝试从 php 执行一个程序。我设置了虚拟机并正确托管了它,但是当网站尝试加载页面时,它说程序无法执行:“权限被拒绝”。我尝试了一切,包括:

  1. 为/var/www/html 中的每个文件添加执行权限
  2. setfacl -m u:apache:rwx/var/www/
  3. chown -R apache:apache/var/www/

更多上下文:

  1. 我在 fedora linux 上

这里是 index.php:

<html>
<body>                                                                                                                                                                                                                                          <p>
<?php
        exec("whoami && ./program 2>&1", $out);

        foreach ($out as $element) {
                echo $element;
                echo "<br>";
        }
?>
</p>

</body>
</html>

当我从浏览器运行它时,这是输出:
browser output

[fedora@fedora html]$ curl localhost
<html>
<body>

<p>
apache<br>sh: ./program: Permission denied<br></p>

</body>
</html>

互联网上的大多数提示都告诉您确保用户“apache”具有执行该文件的权限。我已经完成并通过执行以下命令进行了验证。

[fedora@fedora html]$ sudo -u apache bash
bash-5.0$ ls -la
total 48
drwxrwxrwx. 2 apache apache  4096 Oct 25 11:22 .
drwxrwxrwx+ 4 apache apache  4096 Oct 25 09:51 ..
-rw-rw-rw-. 1 apache apache   158 Oct 25 11:04 index.php
-rwxrwxrwx+ 1 apache apache 20648 Oct 25 10:02 program
-rwxr-xr-x. 1 apache apache    23 Oct 25 10:57 p.sh
bash-5.0$ ./program
test
test
test
test
test
test
test
test
test
bash-5.0$ php index.php
<html>
<body>

<p>
apache<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br></p>

</body>
</html>
bash-5.0$

我通过 sudo 进入了 apache 用户并且能够读取目录中的每个文件并通过 shell 执行它们。之后,我运行了 php index.php,它完美地执行了脚本并输出了预期的输出。

我完全不知道为什么它不能在浏览器中执行这个脚本,任何帮助将不胜感激!

编辑: 我检查了我的 php.ini 文件,没有禁用的功能

...
disable_functions =
...

最佳答案

我检查了我的 SELinux 配置,脚本没有设置正确的权限。当它们需要为 httpd_sys_script_exec_t 时,它们被设置为 httpd_sys_content_t。

简单的修复是:

chcon -R -t httpd_sys_script_exec_t program

关于PHP通过浏览器访问时无法执行本地程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64523152/

相关文章:

php - 如何在独立于主线程的后台运行 php 函数?

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - 连接 MySQL 信息和 2 个表

http - 重用连接时必须重新发送哪些 HTTP header 字段?

unity-game-engine - 如何使用 Unity 和 Vuforia 创建增强现实 Web 应用程序?

PHP exec导致程序崩溃

php - 延迟 20 秒运行第二个 php 文件

php - codeigniter 表单验证错误消息不显示

php - 遍历学说集合占用太多内存

javascript - $ ('#' + somestring)。有什么不工作吗?