通过 Web 浏览器 (PHP) 重新启动 Linux 服务器 [英] Restarting a Linux Server via Web Browser (PHP)
问题描述
打开并阅读 Google 上的每个结果后,我想是时候在某个地方创建自己的主题了.很抱歉,我需要问一个之前已经问过的问题,我再怎么强调也不为过,但我别无选择,因为没有其他问题可以帮助我实现目标.
After opening and reading every result on Google, I figured it's time to make my own thread somewhere. I am sorry that I need to ask a question that's already been asked before, I cannot stress this enough, but I have no other option as no other question asked has helped me achieve what my goal is.
我正在尝试通过由 HTML(用于按钮/文本)和 PHP(用于执行上述功能)支持的 Web 界面来设置重新启动/执行其他系统功能的方法.
I'm trying to setup a means of rebooting/doing other system functions through a web interface powered by HTML (for the buttons/text) and PHP (for the execution of the aforementioned functions).
我无法让它工作.我读过我需要将网络用户添加到 sudoers 文件中,我已经尝试过了.我在我的服务器上运行 Nginx,在我的情况下如何将用户添加到 sudoers?
I'm unable to get this to work. I've read that I need to add the web user to the sudoers file, and I've tried. I'm running Nginx on my server, how do I add the user to the sudoers in my case?
另外,我知道安全风险.
Also, I'm aware of the security risks.
以下是我目前所拥有的:
The following is what I have so far:
HTML (index.html):
<body>
<h3>Restart</h3>
<p>
<form action="restart.php" method="get">
<input type="submit" value="Press me.">
</form>
</p>
</body>
PHP (restart.php):
<?php
echo "This is a test";
echo "<br>";
echo "<br>";
echo shell_exec('ifconfig');
echo "<br>";
echo "<br>";
echo "Restarting server...";
exec ('/usr/bin/sudo /etc/init.d/portmap restart');
shell_exec("/sbin/reboot");
exec("/sbin/reboot");
system("/sbin/reboot");
?>
请注意,在这里,我只有这么多的事情要尝试执行,所以我确保在其中一项工作时达到目标,如果这有意义的话.IFConfig 只是一个测试,以确保它确实能够执行.
Mind you that here, I only have so many things attempting to execute, so that I make sure I hit the target when one of them works, if that makes sense. The IFConfig is just a test to make sure that it's actually able to execute.
Sudoers:
# User privilege specification
root ALL=(ALL:ALL) ALL
www-data reboot = NOPASSWD: /sbin/reboot
这一切都在 Ubuntu 14.04 LEMP 上.
This is all on Ubuntu 14.04 LEMP.
推荐答案
您可以通过编辑您的 sudoers 文件来做到这一点:
Sudoers:
www-data ALL=(root) NOPASSWD: /sbin/reboot
第一个 ALL 用于主机名,如果您的主机名是不是重启",我建议您保留 ALL,因为它可以在任何主机名中工作.这就是为什么它似乎在您的服务器上不起作用
The first ALL is for the hostname if you're hostname is not 'reboot' I advise you to keep ALL as it will work in any hostname. That's why it doesn't seem to work on your server
restart.php
exec('sudo /sbin/reboot');
或者不编辑您的 sudoers 文件.
- 首先创建一个文件,您将在其中存储您的 root 密码
~/密码 :
myrootpassword
- 在 php 文件中以 root 身份运行第二个你想要的任何命令(不要忘记指定存储密码的文件)
phpfile.php :
exec('sudo -u root -S /sbin/reboot < ~/password');
这篇关于通过 Web 浏览器 (PHP) 重新启动 Linux 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!