PHP脚本无法正常工作 [英] PHP script wouldn't work

查看:116
本文介绍了PHP脚本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在本地运行以下脚本,以便开始停止重新启动并获取服务状态。并且必须显示每个命令按钮的输出。
我使用php 5.6来运行此代码。



下面是代码:

 <?php 
//定义cmds
$ commands = [
'stop_apache'=> [
'description'=> ‘Stop Apache2’,
‘cmd’=> ‘systemctl stop apache2’
],
‘restart_apache’=> [
'description'=> ‘Restart Apache2’,
‘cmd’=> ‘systemctl restart apache2’
],
‘start_apache’=> [
'description'=> ‘启动Apache2’,
‘cmd’=> ‘systemctl start apache2’
],
‘status_apache’=> [
'description'=> ‘Status Apache2’,
‘cmd’=> ‘systemctl status apache2’
],
];

//如果($ _SERVER [’REQUEST_METHOD'] ===‘POST’){
$ error = [];
$ result =‘’;

//验证输入
是否(empty($ _ POST [’service’])){
$ error = [
‘service’=> ‘需要的服务类型!’
];
} elseif(!array_key_exists($ _ POST [’service’],$ commands)){
$ error = [
‘service’=> ‘服务无效!’
];
}
}
?>
< form action = method = post>
<?php if(!empty($ error)):?>
< h3>错误< / h3>
< pre><?= print_r($ error,true)?>< / pre>
<?php endif?>
<?php foreach($ commands as $ key => $ command):?>
<按钮类型=提交 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 ,将没有具有重新启动服务的权限。



因此,除非您允许,否则它将无法正常工作。 以前的代码有效是因为您正在登录



好,因此如果您添加允许 www-data 重新启动服务,您的代码如下所示。通过使用 exec()

 <?php 
//定义cmds
$ commands = [
'stop_apache'=> [
'description'=> ‘Stop Apache2’,
‘cmd’=> ‘systemctl stop apache2’
],
‘restart_apache’=> [
'description'=> ‘Restart Apache2’,
‘cmd’=> ‘systemctl restart apache2’
],
‘start_apache’=> [
'description'=> ‘启动Apache2’,
‘cmd’=> ‘systemctl start apache2’
],
‘status_apache’=> [
'description'=> ‘Status Apache2’,
‘cmd’=> ‘systemctl status apache2’
],
];

//如果($ _SERVER [’REQUEST_METHOD'] ===‘POST’){
$ error = [];
$ result =‘’;

//验证输入
是否(empty($ _ POST [’service’])){
$ error = [
‘service’=> ‘需要的服务类型!’
];
} elseif(!array_key_exists($ _ POST [’service’],$ commands)){
$ error = [
‘service’=> ‘服务无效!’
];
}

if(empty($ error)){
exec($ commands [$ _ POST ['service']] ['cmd'],$ output,$ status_code );
if($ status_code === 0){
$ result =服务已重新启动;
} else {
$ error =’无法重新启动服务!状态码:’。$ status_code;
}
}
}
?>
< form action = method = post>
<?php if(!empty($ error)):?>
< h3>错误< / 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>

您会注意到它会失败,并显示无法重新启动服务!状态代码:1 -因为 www-data 没有权限。您应该阅读 https://serverfault.com/questions / 69847 / linux-how-to-give-a-user-permission-to-restart-apache 上的解决方法,也请注意,这可能会导致错误代码不断重启apache和DOSing的可能性。 / p>

个人而言,我不会按照您的意愿直接执行此操作,而是设置一个由root用户运行的任务,然后放置操作(重新启动,停止



希望有帮助。


Basically, I want to run below script locally in order start stop restart and get status of service. And it must show output of each command button. I am using php 5.6 in order to run this code.

Below is the code:

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

解决方案

You are most likely going to encounter permission issues because the web server user www-data, will not have permissions to restart the service.

So unless you allow that, it's not going to work. The previous code which works is because you are logging in over SSH with root user.

Ok so if you did add allow www-data to restart the service, your code would be like the following. By using exec() to execute the 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>

You will notice it will fail with Could not restart service! Status code: 1 - Because www-data wont have permissions. You should read https://serverfault.com/questions/69847/linux-how-to-give-a-user-permission-to-restart-apache on how to fix that, also be aware it opens up the possibility of bad code constantly restarting apache and DOSing.

Personally, I would not do it directly as you want to do, but instead set up a task which is run by root user, and you place the operations (restart, stop etc) in a queue to run.

Hope that helps.

这篇关于PHP脚本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆