4

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.

Kubo2
  • 331
  • 3
  • 16

2 Answers2

2

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");
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
0

I don't recommend to giving root access to the PHP user

Check here: https://stackoverflow.com/a/5226760/2708670

Community
  • 1
  • 1