往往在調用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檔案都沒有修改