Intro
The state of South Africa's electricity network is horrible, with me never knowing when they are going to implement what stage of load shedding. This is a problem for I run a HP micro-server for media and backups at home. In the morning I will shutdown the server before going to work and upon arrival back home I will switch it back on. The issue is I do not want to start my PC up to SSH into the server to issue a shutdown command.
Won't it be convenient if I could just fire up my browser on my phone (that is connected to my home WIFI) and open a php page and the server will gracefully shutdown?
Solution
I run Ubuntu 14.04 with lighttpd as the webserver. So firstly we want to let the www-data user have permissions to execute shutdown as root user. Open up a console and type sudo visudo. This opens up nano in my case, with the sudoers file. At the bottom of the file add the following:
www-data ALL=(root) NOPASSWD: /sbin/shutdown -P now
Exit and save. What this line does is it allows the www-data user to execute shutdown as root without entering a password. I have added the -P now arguments to it as well, so www-data can only run shutdown with those specific arguments.
Next create a file in the directory of your webserver, in my case /var/www/, called shutdown.php. Change the permissions of the file to 644.
user@server:/var/www# sudo touch shutdown.php user@server:/var/www# sudo chmod 644 shutdown.php
Edit the file and add the following php code
<?php echo "<head>Shutting down...</head>"; $output = shell_exec('sudo /sbin/shutdown -P now'); echo $output; ?>
Now if you point your browser to http://yoursever/shutdown.php, it will shutdown the server.