An error occurs when PHP executes functions such as system and exec.

Source: Internet
Author: User
Tags php cli

When calling functions such as system and exec, either no response or an error occurs:

There are many reasons for this:

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

Error analysis:

1,Warning: System () has been disabled for security reasons

This error occurs because the system function is disabled in the server security configuration. Edit php. ini to search for disable_functions and remove the system after the equal sign.

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

The error is caused by the running account number of the current site being unauthorized. EXE. Find the windows \ system32 \ cmd.exe attribute and add the Running Account or users group permissions of the current site.

3. No response is returned and no error is reported during web access. You can use the php cli command line to return the result normally.

This error is generally caused by the WINDOWS system path. Your program uses an absolute path to access the program, for example:

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

At this time, the command line is used to execute the result OK, but the WEB access is blank and there is no error.

Correction Method: Change the file access path to relative:

Copy iecapt.exe to the current directory of your website program and change the code:

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

Visit the page again. OK. The result is displayed.

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

But when I am executing:

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 have found many tutorials and solutions online:

To write a file, make sure you have the permission to operate the folder.

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."

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

Environment: apache2 in PHP + linux
  
The exec function in php can execute many commands (scripts) For ease of use, but you need to grant permissions in linux. Modify the/etc/sudoer file and make two changes:
1)
Add apache users to the file. The user name of apache2 I use is www-data, some are daemon and httpd (check your apache configuration file ). Added format: www-data ALL = (ALL) NOPASSWD: ALL (the format is included in the/etc/sudoer file, and use javasdo ).
2)
Comment out default requiretty. (most of the time, exec cannot be correctly executed because this line is not commented out)
 
Postscript: the reason for posting a document is that the second article is always forgotten every time. As a result, it seems that no one has actually explained it. Hope you can remind yourself of this article and help others .......

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

Because the sudo configuration information must ensure that php Web page execution users (I am using apache here) have the appropriate permissions to execute svn commands.

To solve this problem, modify the sudo configuration file and press the relevant do command to edit the configuration file:

1. Comment Defaults requiretty
Defaults requiretty is changed to # Defaults requiretty, indicating that the terminal does not need to be controlled.
Otherwise sudo: sorry, you must have a tty to run sudo

2. Add Defaults visib1_w
Otherwise, sudo: no tty present and no askpass program specified will appear.

3. Grant the apache user the svn permission.
For example, add apache ALL = (ALL) NOPASSWD:/usr/bin/svn
Note: NOPASSWD eliminates the need to interactively enter the apache user password during command execution.

========================================================== ========================================================== ================
After trying various methods, I found that none of them worked. Finally, I migrated the root directory of the website from/var/www to/home/test. The following is an inexplicable solution. The sudoer file has not been modified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.