π

HTTP Status Endpoint for UptimeKuma for PHP websites like WordPress

So after I saw TechnoTim’s latest video showcasing a project called Uptime Kuma, he shows how to setup and use Uptime Kuma for monitoring websites and network services running under TCP. One of the problems with monitoring a website, such as one powered with WordPress, if you don’t use decent caching, the uptime check can put a bit of a load on your server, and it will have a longer response time.

There’s two obvious answers. Setup and use caching or make a dedicated status endpoint for your website. This post looks at the second option, which will also work if you’re developing a website as you don’t want to use a cache while you’re still working on things, yet don’t want to cause unnecessary load on your server for a simple uptime check.

I wrote a simple PHP script for the task

<?php 
header('Content-Type: application/json');
$check_key = $_GET['key'];
if ($check_key == "SEKRET") { 
$http_status_code = http_response_code();
$current_time = date("c");
$response["success"] = true;
$response["http-status-code"] = $http_status_code;
$response["server-time"] = $current_time;
} else {
$response["success"] = false;
$response["authentication"] = "failed";
}
echo json_encode($response);
?>

You can add this to your server, and even set a secret key in case you don’t want other people to be able to access it. You would access it with status.php?key=SEKRET (in the case of my example). If like me, you’d like to make the URLs a bit prettier, you can use htaccess to setup a rewrite rule to hide the php file extension.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^status$ /status.php?key=none [QSA,L]
RewriteRule ^status/([0-9a-zA-Z-]*) /status.php?key=$1 [QSA,L]
</IfModule>

You need to add this somewhere in the rewrite section of your .htaccess file, somewhere near the top.

Then you can add the URL to Uptime Kuma, such as example.net/status/SEKRET (if you haven’t changed your key!) with a keyword, and search for “success”:true

Happy uptime! 🙂

Related Posts

2 thoughts on “HTTP Status Endpoint for UptimeKuma for PHP websites like WordPress

  1. Hey, thanks for posting this. It really helps make sure my site is up. I had one question though. You show the URL for Uptime Kuma as:
    example.net/status/SEKRET
    Shouldn’t it be:
    example.net/status?key=SEKRET
    Doing it the way shown here always gives me an authentication=”false”

    1. Hi Mike,

      That’s what the additions to .htaccess are for (although this is a fairly ‘Apache’ specific way of doing it),

      The first one just redirects /status to status.php with no key, so it always fails authentication.

      The second, (which rewrites the request from /status/SEKRET to /status.php?key=SEKRET) passes through any letter, number and the ‘-‘ character after /status/ to status.php?key=

      But you are quite right, you don’t need to use the redirector, I just wanted to make the URLs pretty. You can use it just by using /status.php?key=YourSekret if you wanted (or don’t have modrewrite enabled).

      If you haven’t enabled modrewrite (for Apache on Debian you can use a2enmod rewrite to enable it). If you don’t have any other rewrites in your htaccess file, you need to enable it using something like…

      Edit: I’ve modified it in the main article since WordPress was eating some of the code. Technology is fun isn’t it? :p

      Hope this helps!
      Kind regards,
      jcx

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve to post *

jcx.life