After getting my Raspberry Pi Zero W I decided I wanted a way to shutdown or reboot it without having to ssh into it.
First, I installed Apache2 and enabled cgi-bin:sudo apt-get install apache2
I am unfamiliar with nano, so I needed to change my default editor to vim:sudo update-alternatives --set editor /usr/bin/vim.tiny
With the new editor I was able to use the command visudo to edit the /etc/sudoers file. I added this line:www-data ALL = NOPASSWD: /sbin/shutdown
After a reboot, this line allows the default web server identity to use the shutdown command without needing a password.
This means a brilliant fellow can possibly create a URL into my system to shut it down, but a.) the Raspberries are not public facing, and b.) I am not using Raspberries for mission critical applications. If it gets weird too many times, I can always undo it. Right now I find the convenience to be worth the risk.
I chose to use perl for the cgi-bin scripts. Shutdown.pl looks like this:
Reboot.pl looks like this:
Put these files in the cgi-bin directory, usually /usr/lib/cgi-bin and make them executable by whichever web identity your server is using.
Finally, create an html document that has links to those scripts and you are set. Maybe something like this:
<html> <head> <title>System Maintenance</title> </head> <body style="padding: 10px; background-color: cornsilk;"> <p /> <h1 style="text-align: center; color: darkblue;">Reboot or Shutdown the Berry</h1> <p /> <h2>Click a link below to reboot or shutdown the system.</h2> <p /> <table> <tr> <td> <a href="/cgi-bin/reboot.pl">Click here to reboot.</a> </td> <td> </td> <td> <a href="/cgi-bin/shutdown.pl">Click here to shutdown.</a> </td> </tr> </table> </body> </html>