Linux php exec() 命令权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10236091/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:54:27  来源:igfitidea点击:

php exec() command permission denied

phplinuxpermissionsexec

提问by droughtrain

I have a C++ executable file 'skypeforwarder'. skypeforwarder works if I use command line in Terminal in Mac: henry$ /Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder

我有一个 C++ 可执行文件“skypeforwarder”。如果我在 Mac 的终端中使用命令行,skypeforwarder 可以工作: henry$ /Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder

sh: /Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder: Permission denied

But it always issued 'permission denied' if it is called in php exec();

但如果在 php exec(); 中调用它,它总是发出“权限被拒绝”;

<?php 
echo exec('whoami');

$output = null;

$execBuild = '/Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/';
$execBuild .= 'skypeForwarder';

$n  = exec($execBuild, $output); 

I searched a lot. The problem should be the problem of the php/browser permission in web server. I also tried to change the owner of the file from:

我搜索了很多。问题应该是web服务器的php/browser权限问题。我还尝试从以下位置更改文件的所有者:

-rwxr-xr-x  1 henry  staff  1212716 19 Apr 11:23 skypeForwarder

to

-rwxr-xr-x  1 _www  staff  1212716 19 Apr 11:23 skypeForwarder

It still does not work.

它仍然不起作用。

I set the apache in my mac according to http://foundationphp.com/tutorials/php_leopard.php

我根据http://foundationphp.com/tutorials/php_leopard.php在我的 mac 中设置了 apache

采纳答案by Michael Berkowski

Although the file itself is readable by the web server, the Desktopfolder most likely is not, and the web server therefore cannot traverse into it to locate the executable file. You should move the skypeforwarderbinary file into a location readable by the web server, such as a directory parallel to where you are attempting to serve this PHP script. That directory should not, however, be web-accessible. Protect it with .htaccess or place it above the web DocumentRoot, but it must be readable by the web server.

虽然文件本身可以被 web 服务器读取,但Desktop文件夹很可能不是,因此 web 服务器无法遍历它来定位可执行文件。您应该将skypeforwarder二进制文件移动到 Web 服务器可读的位置,例如与您尝试提供此 PHP 脚本的位置平行的目录。但是,该目录不应可通过网络访问。用 .htaccess 保护它或将它放在 web DocumentRoot 之上,但它必须是 web 服务器可读的。

By default, Desktopon OSX is -rwxr------and it is notadvisable to change permissions on that directory.

默认情况下,Desktop在 OSX 上-rwxr------建议更改该目录的权限。

Moreover, it is very much notadvisable to change the file to be owned and writable by _wwwthe web server user. Instead, it should be readable and executable by the web server, but not writable.

此外,非常建议将文件更改为由_wwwWeb 服务器用户拥有和写入。相反,它应该可由 Web 服务器读取和执行,但不可写入。

chown henry skypeforwarder
chmod 755 skypeforwarder

Standard disclaimer:As always, be extremely cautious when executing system calls from PHP scripts accessible on the web.

标准免责声明:与往常一样,从 Web 上可访问的 PHP 脚本执行系统调用时要格外小心。

回答by PadraigD

Try look at the disable function in php.ini

尝试查看 php.ini 中的禁用功能

disable_functions = exec