基本上,我想在本地运行以下脚本,以便开始停止重启并获取服务状态。它必须显示每个命令按钮的输出。 我使用php 5.6来运行此代码。
以下是代码:
<?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 ?>
答案 0 :(得分:0)
// 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']];
}
答案 1 :(得分:0)
您很可能会遇到权限问题,因为网络服务器用户www-data
将不拥有重新启动服务的权限。
所以,除非你允许,否则它不起作用。 The previous code有效是因为您使用root用户通过SSH登录。
好的,如果您确实添加了允许www-data
来重启服务,那么您的代码将如下所示。使用exec()
执行cmd。
<?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>
您会注意到Could not restart service! Status code: 1
会失败 - 因为www-data
没有权限。你应该阅读https://serverfault.com/questions/69847/linux-how-to-give-a-user-permission-to-restart-apache如何解决这个问题,同时要注意它会打开错误代码不断重启apache和DOS的可能性。
就我个人而言,我不会直接按照您的意愿去做,而是设置一个由root用户运行的任务,然后将操作(重启,停止等)放入队列中运行。
希望有所帮助。