How to automatically reboot your server when it's down (DigitalOcean & UptimeRobot)

Prerequisites

You'll need two servers, the one that you want to monitor, and the one that will be notified when the first one is down.

In this short tutorial, I'm using DigitalOcean servers and UptimeRobot to monitor, but it will work the same way if you use something else, as long as you have API access to your server and a webhook feature for the monitoring.

DigitalOcean API token

UptimeRobot webhook

Go to your settings and create a new contact. Select the type "Web-Hook".

I'm using ngrok to test this on local. The endpoint I chose is /api/webhook/uptimerobot and I've added a custom key parameter that will help to verify the origin. UptimeRobot will automatically add all the parameters at the end of this URL, so make sure to end it with a &.

Add this contact to your monitor(s) and you're done.

Laravel configuration

I'm creating a fresh Laravel 5.7 application but it can be easily implemented into any existing app.

'Rebooter' being the (poorly chosen) name of my demo app, here are the basic commands to create an 'up and running' Laravel application:

UptimeRobot will send you the name of your monitor, while DigitalOcean wants your server ID, so we'll have to find a way to make these two work.
You could add your server ID as a custom parameter in the UptimeRobot webhook, or you could create a correspondence table.
I'm choosing the latter and I'm creating a config file with the list of my servers.

We have a few validations at the top to make sure all the variables exist, then we verify the key from UptimeRobot and if the alert type is 'Down', we reboot our server.
That's it, really simple right?

Now everytime your server is down, DigitalOcean will reboot it for you.