通过Web浏览器(PHP)重新启动Linux服务器
在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
如果主机名是不是'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');