簡體   English   中英

PHP無法從Apache執行命令,但可以從CLI(MaxOS X,PHP 5.3)執行

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

我想從Apache下執行的PHP執行shell命令。 我制作腳本,該腳本顯示有關環境的所有信息。 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 */

怎么可能 我在每次執行前都要更改文件,每次都需要編譯。 用戶相同,“哪個”命令有效,目錄和文件具有權限,coffee文件有效。 也許有一些apache或php.ini設置可以鎖定某些shell命令的執行?

請檢查PHP是否具有shell_exec訪問權限。 默認設置shell_exec被阻止,您已經編輯了php.ini以取消阻止它,然后重新啟動服務並執行操作。

注意:提供外殼程序訪問權限可能很危險,因為這是對黑客進行黑客入侵的公開邀請。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM