PHP 执行 system、exec 等函数发生错误

往往在调用system、exec 等函数时,要么没有反应,要么出错:

原因很多,以下是抄别人的:

====================================================================================

错误分析:

1、Warning: system() has been disabled for security reasons

该错误是由于服务器安全配置将system函数关闭了,编辑php.ini查找 disable_functions 将等号后面的 system 去掉即可。

2、Warning: system() [function.system]: Unable to fork

该错误是由于当前站点的运行帐号无法访问cmd.exe,找到windows\system32\cmd.exe 属性,安全,添加当前站点的运行帐号权限,或 users组权限即可。

3、在web访问下执行没有任何响应也不报错,使用php cli命令行执行却可以正常返回结果

该错误一般由于WINDOWS系统路径引起,你的程序中大概使用了绝对路径去访问程序比如:

system('D:\server\DLL\IECapt.exe --url=http://tech.cncms.com --out="D:\web\tt.fei.cn\htdocs\ttt\tech.png"');

此时用命令行执行结果OK,但用WEB访问就是空白也没错误。

修正方法,将文件的访问路径改为相对即可:

将IECapt.exe 复制到你的站点程序当前目录下,并将代码改为:

system('IECapt.exe --url=http://www.baidu.com --out=t/tech.png');

再次访问页面,OK,结果出来了。

====================================================================================

但是当我在执行的时候:

system("/sbin/ifconfig >/home/ago/Desktop/webshell.log",$retval);  // return 2   Misuse of shell builtins (according to Bash documentation)

system("ifconfig >/home/ago/Desktop/webshell.log",$retval);            // return 127  "command not found"

网上找了很多教程和解决办法:

要写文件的话,确保有权限操作文件夹


I am using this php code:

exec("unrar e file.rar",$ret,$code);
and getting an error code of illegal command ie 127 ... but when I am using this command through ssh its working ... because unrar is installed on the server ... so can anyone guess why exec is not doing the right stuff?
=====================================================================

Try using the direct path of the application (/usr/bin/unrar of whatever), it sounds like php can't find the application

=====================================================================

If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found.

=====================================================================

I found the problem. The problem was my security-paranoid OpenBSD. When upgrading from 3.1 to 3.2 they added:

Apache runs chroot'd by default. To disable this, see the new -u option.
The chroot prevented Apache from accessing anything outside of a directory, so I moved everything into the apache directory including netpbm. Everything was accessible and executable, but I guess it was still in some sort of "safe mode" because the exec() always returned 127.

Anyway, running httpd with the -u option went back to the less secure non chroot'd apache startup, which allowed the exec() to work again.
=====================================================================


ERROR-CODE
Linux        http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF
        http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/exitcodes.html
windows        http://www.hiteksoftware.com/knowledge/articles/049.htm

=====================================================================

"Remember to use the full path (IE '/usr/local/bin/foo' instead of 'foo') when using passthru, otherwise you'll get an exit code of 127 (command not found).
Remember, you'll also get this error if your file does not have executable permission."

"If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found."

=====================================================================

  环境:PHP+linux下的apache2
  
  php中的exec函数可以执行很多命令(脚本)使用比较方便,但是在linux下需要给与权限。修改/etc/sudoer文件,做两处改动:
  1)
   在文件中添加apache的用户,我用的apache2的用户名是www-data,有的是daemon、httpd等(查看一下自己的apache的配置文件即可)。添加的格式:www-data ALL=(ALL) NOPASSWD: ALL(格式在/etc/sudoer文件中有,使用 visudo)。
  2)
   注释掉default requiretty.(大部分时候,exec不能正确执行都是由这一行没有注释掉引起的)
 
  后记:之所以发文是因为每次总是忘记了第二条。导致在网上搜索,不过好像没有人真正解释过。希望此文能提醒自己,帮助别人吧。......


============================================================================================

因为sudo配置信息必须保证php网页执行用户(我这里是apache)具备合适的权限执行svn 命令。

为此,修改sudo配置文件,直接键如visudo命令编辑配置文件:

1. 注释Defaults requiretty
Defaults requiretty修改为 #Defaults requiretty, 表示不需要控制终端。
否则会出现sudo: sorry, you must have a tty to run sudo

2. 增加行 Defaults visiblepw
否则会出现 sudo: no tty present and no askpass program specified

3. 赋予apache用户执行svn权限
如,增加行:apache ALL=(ALL) NOPASSWD: /usr/bin/svn
注:NOPASSWD可以使在命令执行时不需要交互输入apache用户的密码

============================================================================================
再试了各种方法之后,发现都不行,最后我把网站根目录从 /var/www迁移到了/home/test ,下面,竟然莫名就好了,sudoer文件都没有修改

没有更多推荐了,返回首页