Reputation: 41
I have a problem. I want to restart ubuntu with php code but I can not. I have tried all of codes from the internet like
<?php
shell_exec("/usr/sbin/reboot");
exec("/usr/sbin/reboot");
system("/usr/sbin/reboot");
?>
and
<?php
shell_exec("shutdown -r");
exec("shutdown -r");
system("shutdown -r");
?>
But all of them do nothing. Please help me. I need this code.
Upvotes: 4
Views: 3357
Reputation: 77359
If the PHP code is being executed by the webserver, it is running under the user "www-data" in ubuntu.
Probably www-data has no right to shutdown, which is a sane default.
You can give www-data sudo rights to shutdown without providing a password and call reboot using sudo.
Take a good look at man sudoers
and be sure to understand the security implications before editing the sudo config: this will effectively give any PHP script the right to shutdown the server (untested).
# /etc/sudoers (edit using the visudo command)
Cmnd_Alias SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias HALT = /usr/sbin/halt
Cmnd_Alias REBOOT = /usr/sbin/reboot
Host_Alias LOCALHOST = 127.0.0.1
www-data LOCALHOST = NOPASSWD: SHUTDOWN, HALT, REBOOT
In PHP you can use:
shell_exec("/usr/bin/sudo /usr/sbin/reboot");
Upvotes: 2
Reputation: 64
I don't recommend to giving root access to the PHP user
Check here: https://stackoverflow.com/a/5226760/2708670
Upvotes: 0