PHP exec() 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11147643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP exec() not working properly
提问by MattS
I am having difficulty with the PHP exec() function. It seems to not be calling certain functions. For instance, the code echo exec('ls');produces no output whatsoever (it should, there are files in the directory). That main reason this is a problem for me is that I'm trying execute a .jar from a PHP exec() call.
我在使用 PHP exec() 函数时遇到困难。它似乎没有调用某些函数。例如,代码不echo exec('ls');产生任何输出(应该,目录中有文件)。这对我来说是个问题的主要原因是我正在尝试从 PHP exec() 调用中执行 .jar。
As far as I know I'm calling the java program properly, but I'm not getting any of the output. The .jar can be executed from the command line on the server. (For the record, it's an apache server).
据我所知,我正在正确调用 java 程序,但没有得到任何输出。.jar 可以从服务器上的命令行执行。(为了记录,它是一个 apache 服务器)。
My php for the .jar execute looks like this:
我的 .jar 执行 php 如下所示:
$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);
All I get for output from this exec() call is Array().
我从这个 exec() 调用中得到的输出是Array().
I have had success with exec() executing 'whoami'and 'pwd'. I can't figure out why some functions are working and some aren't. I'm not the most experienced person with PHP either, so I'm not too sure how to diagnose the issue. Any and all help would be appreciated.
我在 exec() 执行'whoami'和'pwd'. 我不明白为什么有些功能可以工作,有些则不行。我也不是最有经验的 PHP 人,所以我不太确定如何诊断问题。任何和所有帮助将不胜感激。
采纳答案by MattS
I have found the issue - SELinux was blocking PHP from accessing certain functions. Putting SELinux into permissive mode has fixed the issues (although, I'd rather not have to leave SELinux in permissive mode; I'd rather find a way of allowing certain functions if I can).
我发现了问题 - SELinux 阻止 PHP 访问某些功能。将 SELinux 置于许可模式已解决了这些问题(尽管我不想让 SELinux 处于许可模式;如果可以,我宁愿找到一种允许某些功能的方法)。
回答by AnonGeek
The reason why you are not able to execute ls is because of permissions.
无法执行 ls 的原因是权限问题。
If you are running the web server as user A, then you can only lsonly those directories which have permissions for user A.
如果您以用户 A 的身份运行 Web 服务器,那么您只能ls那些对用户 A具有权限的目录。
You can either change the permission of the directory or you can change the user under which the server is running by changing the httpd.conffile(i am assuming that you are using apache).
您可以更改目录的权限,也可以通过更改httpd.conf文件来更改运行服务器的用户(我假设您使用的是 apache)。
If you are changing the permissions of the directory, then make sure that you change permissions of parent directories also.
如果要更改目录的权限,请确保也更改父目录的权限。
To change the web server user, follow following steps:
要更改 Web 服务器用户,请执行以下步骤:
Open the following file:
打开以下文件:
vi /etc/httpd/conf/httpd.conf
Search for
搜索
User apache
Group apache
Change the user and group name. After changing the user and group, restart the server using following command.
更改用户名和组名。更改用户和组后,使用以下命令重新启动服务器。
/sbin/service httpd restart
Then you will be able to execute all commands which can be run by that user.
然后,您将能够执行该用户可以运行的所有命令。
EDIT:
编辑:
The 'User' should be a non-root user in httpd.conf. Apache by default doesnot serve pages when run as root. You have to set user as a non-root user or else you will get error. If you want to force apache to run as root, then you have to set a environment variable as below:
“用户”应该是 httpd.conf 中的非 root 用户。以 root 身份运行时,Apache 默认不提供页面。您必须将用户设置为非 root 用户,否则会出错。如果你想强制 apache 以 root 身份运行,那么你必须设置一个环境变量,如下所示:
env CFLAGS=-DBIG_SECURITY_HOLE
Then you have to rebuild apache before you can run it as root.
然后你必须重建apache才能以root身份运行它。
回答by Andrey Vorobyev
I have a solution: command runs from console, but not from php via exec/system/passthru. The issue is the path to command. It works with the absolute path to command
我有一个解决方案:命令从控制台运行,而不是通过 exec/system/passthru 从 php 运行。问题是命令的路径。它适用于命令的绝对路径
So that:
以便:
wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
becomes:
变成:
/usr/local/bin/wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
And now, it's works from php via exec
Where command binary you can see via whereis wkhtmltopdf
现在,它可以通过 exec 在 php 中运行,您可以通过其中的命令查看 whereis wkhtmltopdf
回答by Denes Borsos
Tore my hair out trying to work out why PHP exec works from command line but not from Apache. At the end, I found the following permissions:
试图弄清楚为什么 PHP exec 可以从命令行工作而不是从 Apache 工作,我把我的头发撕了。最后,我找到了以下权限:
***getsebool -a | grep httpd*** ---->
**httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off**
USE: setsebool -P httpd_ssi_exec 1
使用: setsebool -P httpd_ssi_exec 1
回答by Bryan Hoekstra
Your problem is not an execution issue but the syntax of the exec command. The second argument is always returned as an array and contains a single line of the output in each index. The return value of the exec function will contain the final line of the commands output. To show the output you can use:
您的问题不是执行问题,而是 exec 命令的语法。第二个参数始终作为数组返回,并在每个索引中包含一行输出。exec 函数的返回值将包含命令输出的最后一行。要显示输出,您可以使用:
foreach($output as $line) echo "$line\n";
foreach($output as $line) echo "$line\n";
See http://php.net/manual/en/function.exec.phpfor details. You can also get the command's exit value with a third argument.
有关详细信息,请参阅http://php.net/manual/en/function.exec.php。您还可以使用第三个参数获取命令的退出值。

