[英]apache2 & PHP: execute a command from shell_exec
我正在嘗試通過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 s。 問題是從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文件(刪除了注釋和白線):
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用戶啟用/禁用服務的權限。
您可能要使用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.