nkuhta
nkuhta

Reputation: 11128

PHP can't execute command from apache, but can from CLI (MaxOS X, PHP 5.3)

I want to execute shell command from PHP, running under apache. I make script, that shows all info about enviroment. PHP Script:

<?php
    $root = dirname(__FILE__);
    $coffeeFile = $root . DIRECTORY_SEPARATOR . 'Script.coffee';
    $jsFile = $root . DIRECTORY_SEPARATOR . 'Script.js';
    echo "User: " . exec('whoami') . "\n";
    echo "Which: " . exec('which coffee') . "\n";
    echo "Coffee file perms: " . substr(sprintf('%o', fileperms($coffeeFile)), -4) . "\n"; 
    echo "Js file perms: " . substr(sprintf('%o', fileperms($jsFile)), -4) . "\n"; 
    echo "Dir perms: " . substr(sprintf('%o', fileperms($root)), -4) . "\n"; 

    $command = "coffee -bo $root -c $coffeeFile";
    exec($command, $output);
    if (filemtime($coffeeFile) > filemtime($jsFile)) {
        echo 'compile failed. command: ' . $command . PHP_EOL;
        echo "Output: " . implode("\n", $output) . PHP_EOL;
    } else {
        echo 'compile success. command: ' . $command . PHP_EOL;
    }

It I'll execute it from command line from _www user, it will work: Command:

sudo -u _www php index.php

Output from CLI:

User: _www
Which: /usr/bin/coffee
Coffee file perms: 0777
Js file perms: 0777
Dir perms: 0777
compile success command: coffee -bo /Users/username/htdocs/testcase -c /Users/username/htdocs/testcase/Script.coffee

But if run it from browser, compile fails, but not errors or output is there.

Output in browser:

User: _www
Which: /usr/bin/coffee
Coffee file perms: 0777
Js file perms: 0777
Dir perms: 0777
compile failed. command: coffee -bo /Users/username/htdocs/testcase -c /Users/username/htdocs/testcase/Script.coffee
Output: /* empty array in output */

How it can be? I change my file before every execution, it need to be compiled every time. Users are the same, "which" command works, dir and files have permissions, coffee file is valid. Maybe there are some apache or php.ini settings, that locks execution of some shell commands?

Upvotes: 0

Views: 1144

Answers (1)

Vineet1982
Vineet1982

Reputation: 7918

Kindly check does PHP have shell_exec access given or not. Default setting shell_exec is blocked and you have edit php.ini to unblock it and restart service and do things.

Note: Providing shell access can be dangerous as it is the open invitation to the hackers to hack your machine.

Upvotes: 0

Related Questions