【发布时间】:2016-04-27 12:50:48
【问题描述】:
我正在尝试通过 XMLHttpRequest 从 Javascript 文件执行命令。
一旦我创建了按钮(我可以毫无问题地做到这一点),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;
}
而我正在测试的 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"
?>
函数 RestartService 被执行,因为我可以看到 1.5 秒的旋转。问题是从 PHP 文件执行命令。我认为这是 apache2 配置或文件权限的问题,因为如果我从 Linux shell 执行命令:
php -r "echo exec('/usr/local/bin/AD33x-file.sh restart');"
文件执行正确..
/usr/local/bin 路径中的文件权限设置为 755。 我还检查了 /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 =
但我没有看到任何“shell_exec”...
其实这是我的/etc/apache2/apache2.conf文件(去掉了cmets和白线):
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
也许我遗漏了一些细节? 从 PHP 和 apache2 执行命令的正确方法是什么?
【问题讨论】:
-
我认为您没有权限使用正在运行的 apache 用户启用/禁用服务。
-
如果您更改 Apache 用户,您可能会遇到安全问题。检查我的答案,它可能会帮助你。
标签: php apache2 shell-exec