0

I am trying to execute a command from a Javascript file through XMLHttpRequest.

Once I created the button (something I can do without problems), the function from the Javascript code is:

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
//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. The problems is executing the command from the PHP file. I think this is a matter of apache2 configuration, or file permissions, because if I execute, from Linux shell, the command:

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. I also checked for 'disable_functions' tag in php.ini files from /etc/php

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...

Actually, this is my /etc/apache2/apache2.conf file (with comments and white lines removed):

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 ?

2
  • I don't think you have permissions to enable/disable a service using the running apache user. Commented Apr 27, 2016 at 12:58
  • If you change the Apache user you may run into security problems. Check my answer, it may help you. Commented Apr 27, 2016 at 13:04

1 Answer 1

0

I don't think you have permissions to enable/disable a service using the running apache user.

You may want to use ssh2_exec to do this, i.e.:

$connection = ssh2_connect('your.server.com', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'command here');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.