apache2& PHP:从shell_exec执行命令

时间:2016-04-27 12:50:48

标签: php apache2 shell-exec

我正在尝试通过XMLHttpRequest从Javascript文件执行命令。

一旦我创建了按钮(我可以毫无问题地做到这一点),Javascript代码中的函数是:

<?php
//error_reporting(E_ALL);
$fname = "/usr/local/bin/AD33x-"
if(isset($_POST))
{
    $service = $_POST["service"];
    $daemon = "$fname"."$service".".sh";

    //if(file_exists($daemon))
    {
        shell_exec("$daemon restart > /dev/null 2> /dev/null &");
    }
}

return "ok"
?>

虽然我正在测试的php文件只是:

php -r "echo exec('/usr/local/bin/AD33x-file.sh restart');"

执行 RestartService 功能,因为我可以看到旋转1.5秒。问题是从PHP文件执行命令。我认为这是apache2配置或文件权限的问题,因为如果我从Linux shell执行命令:

apache2/php.ini:315:disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, cli/php.ini:315:disable_functions =

文件正确执行..

/ usr / local / bin 路径中的文件权限设置为 755 。 我还检查了&#39; disable_functions &#39;来自 / etc / php

的php.ini文件中的标签
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>
<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>
<Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AuthType Basic
        AuthUserFile "/var/www/html/current/.htpasswd"
        AuthName "Authorization Required"
        Require valid-user
        Order allow,deny
        Allow from all
</Directory>

AccessFileName "/var/www/html/current/.htaccess"

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

但我没有看到任何&#39; shell_exec&#39;那里......

实际上,这是我的 /etc/apache2/apache2.conf 文件(删除了注释和白线):

for tr in table.find_all("tr")[:22]:

也许我错过了一些细节? 从PHP和apache2执行命令的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我认为您无权使用正在运行的apache用户启用/禁用服务。

您可能希望使用ssh2_exec来执行此操作,即:

$connection = ssh2_connect('your.server.com', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'command here');