PHP脚本';行不通
基本上,我希望在本地运行下面的脚本,以便启动-停止-重新启动并获取服务状态。它必须显示每个命令按钮的输出。 我使用PHP5.6来运行这段代码 代码如下:PHP脚本';行不通,php,centos,Php,Centos,基本上,我希望在本地运行下面的脚本,以便启动-停止-重新启动并获取服务状态。它必须显示每个命令按钮的输出。 我使用PHP5.6来运行这段代码 代码如下: <?php // define cmds $commands = [ 'stop_apache' => [ 'description' => 'Stop Apache2', 'cmd' => 'systemctl stop apache2' ], '
<?php
// define cmds
$commands = [
'stop_apache' => [
'description' => 'Stop Apache2',
'cmd' => 'systemctl stop apache2'
],
'restart_apache' => [
'description' => 'Restart Apache2',
'cmd' => 'systemctl restart apache2'
],
'start_apache' => [
'description' => 'Start Apache2',
'cmd' => 'systemctl start apache2'
],
'status_apache' => [
'description' => 'Status Apache2',
'cmd' => 'systemctl status apache2'
],
];
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
}
?>
<form action="" method="post">
<?php if (!empty($error)): ?>
<h3>Error</h3>
<pre><?= print_r($error, true) ?></pre>
<?php endif ?>
<?php foreach ($commands as $key => $command): ?>
<button type="submit" name="service" value="<?= $key ?>"><?=
$command['description'] ?></button>
<?php endforeach ?>
</form>
<?php if (!empty($result)): ?>
<pre><?= print_r($result, true) ?></pre>
<?php endif ?>
错误
您很可能会遇到权限问题,因为web服务器用户www-data
将无权重新启动服务
所以除非你允许,否则它不会起作用。这是因为您使用root用户通过SSH登录
好的,如果您确实添加了allowwww-data
来重新启动服务,那么您的代码如下所示。通过使用来执行cmd
错误
您会注意到它将失败,因为无法重新启动服务!状态代码:1
-因为www-data
没有权限。您应该阅读如何解决这个问题,同时也要注意,它可能会导致错误代码不断地重新启动apache并停止运行
就个人而言,我不会像您希望的那样直接执行,而是设置一个由root用户运行的任务,然后将操作(重启、停止等)放入队列中运行
希望有帮助。“不起作用”太笼统了-请描述确切的问题/错误。为什么要执行php而不是简单的
systemctl start apache2
或任何其他命令?在/var/log/apache2/error.log
@llyaBursov中也没有显示任何错误出于某种目的它的要求。我只能通过这种方式存档。我已经添加了,但不会工作,也不会停止服务。只需在输出中显示以下内容:`Array([description]=>Stop Apache2[cmd]=>systemctl Stop Apache2)`您可以调用函数shell_exec(command)。()
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
//you need add this line
else $result = $commands[$_POST['service']];
}
<?php
// define cmds
$commands = [
'stop_apache' => [
'description' => 'Stop Apache2',
'cmd' => 'systemctl stop apache2'
],
'restart_apache' => [
'description' => 'Restart Apache2',
'cmd' => 'systemctl restart apache2'
],
'start_apache' => [
'description' => 'Start Apache2',
'cmd' => 'systemctl start apache2'
],
'status_apache' => [
'description' => 'Status Apache2',
'cmd' => 'systemctl status apache2'
],
];
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
if (empty($error)) {
exec($commands[$_POST['service']]['cmd'], $output, $status_code);
if ($status_code === 0) {
$result = 'Service restarted';
} else {
$error = 'Could not restart service! Status code: '.$status_code;
}
}
}
?>
<form action="" method="post">
<?php if (!empty($error)): ?>
<h3>Error</h3>
<pre><?= print_r($error, true) ?></pre>
<?php endif ?>
<?php foreach ($commands as $key => $command): ?>
<button type="submit" name="service" value="<?= $key ?>"><?= $command['description'] ?></button>
<?php endforeach ?>
</form>
<?php if (!empty($result)): ?>
<pre><?= print_r($result, true) ?></pre>
<?php endif ?>
</form>