如何使用LAMP服务器通过php站点关闭我的系统? [英] How to shutdown my system via a php site using LAMP server?

查看:100
本文介绍了如何使用LAMP服务器通过php站点关闭我的系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止的代码

This is the code i have so far

站点:

<?php
if ($_POST['action']=="Shutdown") {
    $test=shell_exec("touch /scripts/shutdown.sh");
}
?>

shutdown.sh:

shutdown.sh:

#!/bin/sh
if [ -f "/tmp/shutdown" ]
then
   rm -f /tmp/shutdown
   /sbin/shutdown now
fi

我从 http://ubuntuforums.org/大致了解了这个想法,但是,当我在本地主机上运行php脚本时,它没有显示吗?

I roughly got the idea from http://ubuntuforums.org/ But, when i run the php script on my local-host, it doesn't show up?

问题:如何使用LAMP服务器通过php网站关闭系统?

提前谢谢!

推荐答案

touch仅更新文件的修改时间,不执行脚本.

touch only updates the file's modification time, it doesn't execute the script.

您需要做的是为用户提供PHP/Web服务器进程正在运行,以执行shutdown命令的权限,该权限通常保留给root用户,而不是普通的Web进程.最好的方法可能是通过sudoers文件,从而向用户(可能是www-data左右)授予通过exec()执行sudo shutdown的权限,而无需输入密码提示.阅读系统的man sudoers配置.请特别小心,不要授予超出此范围的权限,否则会破坏您的sudoers配置.

What you need to do is to give the user the PHP/web server process is running as the permission to execute the shutdown command, a permission typically reserved for root and not ordinary web processes. The best way is probably through the sudoers file, giving the user (probably www-data or so) the permission to execute sudo shutdown via exec() without password prompt. Read the man sudoers configuration for your system. Be extremely careful not to give permissions beyond this or to otherwise screw up your sudoers configuration.

sudoers文件需要一个条目,如:

The sudoers file needs an entry like:

www-data  ALL=NOPASSWD:  /sbin/shutdown

同样,请继续阅读man sudoers,以确保您在这里做什么.仅使用visudo编辑sudoers文件以确保它不会混乱.

Again, read up on man sudoers to be sure what you're doing here. Only use visudo to edit the sudoers file to make sure it doesn't mess up.

然后:

exec('sudo /scripts/shutdown.sh');

这篇关于如何使用LAMP服务器通过php站点关闭我的系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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