PHP无法从apache执行命令,但可以从CLI(Mac OS X,PHP 5.3)执行

时间:2013-12-17 06:56:11

标签: php macos apache shell

我想从PHP执行shell命令,在apache下运行。我制作脚本,显示有关环境的所有信息。 PHP脚本:

<?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;
    }

我将从_www用户的命令行执行它,它将起作用: 命令:

sudo -u _www php index.php

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

但是如果从浏览器运行它,编译失败,但不存在错误或输出。

浏览器输出:

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 */

它怎么样?我在每次执行之前更改了文件,每次都需要编译。用户是一样的,“哪个”命令有效,dir和文件有权限,咖啡文件有效。也许有一些apache或php.ini设置会锁定一些shell命令的执行?

1 个答案:

答案 0 :(得分:0)

请检查PHP是否具有shell_exec访问权限。默认设置shell_exec被阻止,您已编辑php.ini以取消阻止它并重新启动服务并执行操作。

注意:提供shell访问权限可能很危险,因为它是黑客通知您破解计算机的公开邀请。