PHP 通过浏览器访问时无法执行本地程序
PHP cannot execute local programs when accessed through a browser
我对 PHP 和一般的 Web 开发有点陌生,我正在尝试从 php 执行一个程序。我设置了虚拟机并正确托管了它,但是当网站尝试加载页面时,它说程序无法执行:“权限被拒绝”。我尝试了一切,包括:
- 为 /var/www/html
中的每个文件添加执行权限
- setfacl -m u:apache:rwx /var/www/
- chown -R apache:apache /var/www/
更多上下文:
- 我在软呢帽linux
此处index.php:
<html>
<body> <p>
<?php
exec("whoami && ./program 2>&1", $out);
foreach ($out as $element) {
echo $element;
echo "<br>";
}
?>
</p>
</body>
</html>
当我从浏览器 运行 时,这是输出:
[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 和一般的 Web 开发有点陌生,我正在尝试从 php 执行一个程序。我设置了虚拟机并正确托管了它,但是当网站尝试加载页面时,它说程序无法执行:“权限被拒绝”。我尝试了一切,包括:
- 为 /var/www/html 中的每个文件添加执行权限
- setfacl -m u:apache:rwx /var/www/
- chown -R apache:apache /var/www/
更多上下文:
- 我在软呢帽linux
此处index.php:
<html>
<body> <p>
<?php
exec("whoami && ./program 2>&1", $out);
foreach ($out as $element) {
echo $element;
echo "<br>";
}
?>
</p>
</body>
</html>
当我从浏览器 运行 时,这是输出:
[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