Home:ALL Converter>linux server restart in php script

linux server restart in php script

Ask Time:2011-07-11T16:00:47         Author:hmbarit

Json Formatter

I would like to know if it is possible to restart the Linux server using PHP script? In related to changing IP Address from Static to DHCP, I need to reboot the system so that it will take effect.

i tried this code:

system("/usr/bin/reboot");

error message is :

reboot: must be useruser

here'e the another:

system('/etc/init.d/network restart');

the error is:

Shutting down loopback interface: [FAILED] Bringing up loopback interface: [FAILED]

Hope you can help me in this.

Thank you!

Regards to all.

Author:hmbarit,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6647289/linux-server-restart-in-php-script
fyr :

You can restart it if the program-users-context of your interpreter, webserver has the rights to execute these commands. A webserver or php interpreter should not be run as root. You may use sudo, sudoers in order to escalate privileges in these both cases.",
2011-07-11T08:05:49
qbert220 :

You will need to use sudo like this:\n\nsystem(\"sudo /usr/bin/reboot\");\n\n\nin your /etc/sudoers add the following:\n\napache ALL=(ALL) NOPASSWD: /usr/bin/reboot\n\n\nWhere apache is the username under which the PHP script runs.\n\nBe aware of the security impication of doing this - anyone with access to PHP scripts on the server to reboot the server.",
2011-07-11T08:13:17
symcbean :

\n In related to changing IP Address from Static to DHCP, I need to reboot the system\n\n\nNo you don't. This is not Microsoft Windows. But the command for remapping the network interfaces varies from distribution to distribution - and you don't say which this is. Similarly, access the reboot, shutdown, init and telinit commands varies by distribution.\n\n\n I am working in local server\n\n\nSo why not just do it via ssh or at the console?",
2011-07-11T08:21:47
Spudley :

Since you have stated that you're a newbie to Linux, I feel that it's worth pointing out that it's much much less common to need to reboot a Linux box compared to a Windows one.\n\nYou shouldn't need to reboot even after updating core software packages. Even if something crashes badly, you can ususally recover without a reboot.\n\nYou haven't stated why you'd want to be doing a reboot, but rebooting the whole box really should be an absolute last resort. In fact, rebooting simply to clear an issue is consdered very bad practice for a Linux administrator because it tends to wipe out evidence of what caused the problem, and does nothing to prevent the problem from recurring.\n\nOn Linux, most issues that would require a Windows box to be rebooted only require the individual program or service to be restarted.\n\nFinally, a note on security: Doing major system operations such as this via a PHP program is bad security practice because it exposes root level functionality to non-root users. I assume (well, I hope!) that you're planning to lock down access to this PHP page, but even the best secured web page should not be considered secure enough to be running root-level operations.\n\nIn short, my advice is that you shouldn't do this. If you must do it, @qbert220's answer should work, but please don't do it.\n\n[EDIT]\nWith specific regard to changing the IP address from DHCP to static, this should not require a server reboot in Linux. You simply need to restart the networking interface.\n\nOnce you've changed the config, something like this should be enough to restart your network interface with the new IP address in place:\n\nsudo /etc/init.d/networking restart\n\n\nYou haven't specified what variety of Linux you're using, but here's a link to a page which details how to do it from the commandline in Ubuntu.\n\nIt does require root priviledges though, so you would need to use sudo to achieve it and to add your web user to the sudoers list, which as I said before is really not great from a security point of view.",
2011-07-11T13:01:13
yy