简体   繁体   English

apache2&PHP:从shell_exec执行命令

[英]apache2 & PHP: execute a command from shell_exec

I am trying to execute a command from a Javascript file through XMLHttpRequest. 我正在尝试通过XMLHttpRequest从Javascript文件执行命令。

Once I created the button (something I can do without problems), the function from the Javascript code is: 创建按钮后(我可以做的没有问题),Javascript代码的功能是:

function RestartService(service)
{
    var target = document.getElementById('page');
    var spinner = new Spinner(opts).spin(target);

    var data = new FormData();
    data.append('service', service);

    var xhReq = new XMLHttpRequest();
    xhReq.open("POST", "/rservice.php", false);
    xhReq.send(data);
    var serverResponse = xhReq.responseText;

    timeout = setTimeout(
        function ()
        {
            spinner.stop();
        }, 1500);

    return serverResponse;
}

while the php file I am testing is just: 而我正在测试的php文件只是:

<?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"
?>

The function RestartService is executed because I can see the spin for 1.5 s. 执行RestartService函数是因为我可以看到旋转了1.5 s。 The problems is executing the command from the PHP file. 问题是从PHP文件执行命令。 I think this is a matter of apache2 configuration, or file permissions, because if I execute, from Linux shell, the command: 我认为这是apache2配置或文件权限的问题,因为如果我从Linux shell执行以下命令,则:

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

The file is executed correctly.. 该文件已正确执行。

The file permissions in /usr/local/bin path are set to 755 . / usr / local / bin路径中的文件许可权设置为755 I also checked for ' disable_functions ' tag in php.ini files from /etc/php 我还在/ etc / php的 php.ini文件中检查了' disable_functions '标记

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 =

but I don't see any 'shell_exec' there... 但我在那里看不到任何'shell_exec'...

Actually, this is my /etc/apache2/apache2.conf file (with comments and white lines removed): 实际上,这是我的/etc/apache2/apache2.conf文件(删除了注释和白线):

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

Perhaps there is some detail I am missing ? 也许我缺少一些细节? What is the right way to execute command from PHP and apache2 ? 从PHP和apache2执行命令的正确方法是什么?

I don't think you have permissions to enable/disable a service using the running apache user. 我认为您没有使用正在运行的apache用户启用/禁用服务的权限。

You may want to use ssh2_exec to do this, ie: 您可能要使用ssh2_exec来执行此操作,即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM