Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec() not working properly

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.

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

My php for the .jar execute looks like this:

$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);

All I get for output from this exec() call is 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.

like image 363
MattS Avatar asked Jun 21 '12 22:06

MattS


People also ask

Why is exec() not working in PHP on OpenBSD?

In the default configuration from OpenBSD, PHP runs into a chroot. So the exec () command will not work. You will get a 127 (command not found) result code. The reason is, the shell (/bin/sh) is missing in chroot, but the exec () command requires the shell. (I have noticed this on OpenBSD 7.0 with PHP 8.0.11.)

Why exec() command does not work?

So the exec () command will not work. You will get a 127 (command not found) result code. The reason is, the shell (/bin/sh) is missing in chroot, but the exec () command requires the shell. (I have noticed this on OpenBSD 7.0 with PHP 8.0.11.) result_code -1 could mean "Maximum number of file descriptors reached".

What are the examples of Exec() in PHP?

Examples. Example #1 An exec () example. <?php. // outputs the username that owns the running php/httpd process. // (on a system with the "whoami" executable in the path) $output=null; $retval=null; exec('whoami', $output, $retval); echo "Returned with status $retval and output:\n";

Why can't I exec rsync in PHP?

If rsync isn't in one of the listed folders, then PHP won't exec it. You can ask your host to put a symlink to rsync in the exec_dir path, and then exec it from that location; but it depends on their policy as to whether they'll allow it.


4 Answers

The reason why you are not able to execute ls is because of permissions.

If you are running the web server as user A , then you can only ls only those directories which have permissions for user 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.conf file(i am assuming that you are using 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:

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:

env CFLAGS=-DBIG_SECURITY_HOLE

Then you have to rebuild apache before you can run it as root.

like image 199
AnonGeek Avatar answered Sep 21 '22 21:09

AnonGeek


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

like image 34
MattS Avatar answered Sep 20 '22 21:09

MattS


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

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

like image 30
Andrey Vorobyev Avatar answered Sep 20 '22 21:09

Andrey Vorobyev


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:

***getsebool -a | grep httpd*** ----> 
    **httpd_setrlimit --> off
    httpd_ssi_exec --> off
    httpd_sys_script_anon_write --> off**

USE: setsebool -P httpd_ssi_exec 1

SEE: https://linux.die.net/man/8/httpd_selinux

like image 24
Denes Borsos Avatar answered Sep 21 '22 21:09

Denes Borsos