12

This is needed because I need to implement a load balancer in GCP for a word press instance.

GCP will only forward a request to the wordpress instance if it passes the health test.

Here is a typical of definition of a health check screen

enter image description here

It expects the 'request path' to return 200. Otherwise the target system is considered down.

However if I create a page in wordpress e.g. /index.php/newpage, it will always return 301.

While the wordpress site is up and running, it fails the health check.

Is there any way I can define a page in wordpress that will return 200? Hopefully it will not require additional plugin because I would like avoid maintaining extra plugin.

The last resort to me is to add a url to the php server that will return 200. However the status of this url may not 100% reflect the health of the wordpress site (which may go down due to mysql issue for example). If it is indeed the last resort, what is the best way to implement it? I am not proficient with php developement.

The version of wordpress is 4.6.2

1
  • 2
    Why don't you simply use the root page of the site for this? Jan 17, 2017 at 10:36

6 Answers 6

9

You can use your WordPress installation front page for the health check. This is the most reliable indicator that the WordPress site is available.

2
  • 4
    The accepted answer lacks clarity by talking about the "front page". One might assume this could simply be represented by using the root path, /. However, at least on the official Wordpress Docker container we're using, this issues a 301 redirect. To get around this, use /index.php which returns an 200 HTTP response code. Sep 8, 2017 at 6:15
  • 2
    It depends on how the WordPress has been installed. If friendly URLs are configured correctly, / URL is processed with index.php, without any 301 redirect. I have no experience on the official WordPress container. The clarification still has merit, thanks for it! Sep 8, 2017 at 8:32
7

For what it's worth, I had to set-up a WordPress site in a container and get a health check to succeed before I could even get traffic directed to it. The suggestions for the "front page" work only AFTER WordPress has been installed through the browser. Prior to that they return a 302 status code to /wp-admin/install.php. I noodled around in the container folders and decided on using /wp-includes/images/blank.gif as the health check path. This allowed the health check to succeed and traffic started routing to the container. From there I installed WordPress through the browser and was able to change the health check path back to /.

4

While this is an old post, I wanted to chime in on this because this topic is still relevant and the accepted answer is not ideal in my use case. I wouldn't suggest others do this; especially for a high traffic system.

My problem with the accepted answer is that it's suggested to use the home page as the health check. Depending on your site, this could be fine but I don't see a need for the DB to be queried on a health check. Even if the DB connection fails the instance is still technically healthy. e.g. If your RDS cluster goes down, the home page will show a DB error which will give a 500 so you would be triggering your auto-scaling for no need. The health check only needs to check the app and make sure it's setup properly.

My home page has many queries and the health check alone will actually put unnecessary load on the RDS cluster with my multiple instance setup. I could specify a lighter page but this still is not ideal as the health check should ensure the instance is setup properly and I don't want a project specific check added to every system I spin up. Although there are use cases for that too.

I use this simple health check (_healthCheck.php):

<?php
/**
 * Check if basic WordPress structure is in place
 * This is to be used for load balancer health checks
 */

// Checks
define('WORDPRESS_CONFIG', __DIR__ . DIRECTORY_SEPARATOR . 'wp-config.php');
define('WORDPRESS_DIRECTORY', __DIR__ . DIRECTORY_SEPARATOR . 'wordpress');
define('WP_CONTENT_DIRECTORY', __DIR__ . DIRECTORY_SEPARATOR . 'wp-content');


// Health Check Flags
$_healthCheckStatus = true;
// Will stop the while loop
$_healthCheckCompleted = false;

// Just to be safe
try {
    // DoWhile loop here to simplify kicking out on a false $_healthCheckStatus
    do {
        // Check if wp-config exists
        if (!file_exists(WORDPRESS_CONFIG)) {
            $_healthCheckStatus = false;
        }

        // Make sure we have required directories
        $_healthCheckStatus = _dirIsValidAndNotEmpty(WORDPRESS_DIRECTORY);
        $_healthCheckStatus = _dirIsValidAndNotEmpty(WP_CONTENT_DIRECTORY);

        // Checks are complete, kick out the loop
        $_healthCheckCompleted = true; // Just say no to infinity and beyond
    } while (false === $_healthCheckCompleted && true === $_healthCheckStatus);
} catch (\Exception $e) {
    // Health check fails
    $_healthCheckStatus = false;
}

// If a bad healthcheck, return 404 to tell the load balancer we suck
if (false === $_healthCheckStatus) {
    header("HTTP/1.0 404 Not Found");
    ?>
    <html>
    <body><h1>Health is bad</h1></body>
    </html>
    <?php
    die();
} else {
    ?>
    <html>
    <body><h1>Health appears good</h1></body>
    </html>
    <?php
}

/**
 * Validates a directory and ensures it's not empty
 * @param string $dir
 * @return bool
 */
function _dirIsValidAndNotEmpty($dir) {
    // Make sure we have a directory
    if (is_dir($dir)) {
        // Make sure it's not empty
        $_dirIsNotEmpty = (new \FilesystemIterator($dir))->valid();
        if ($_dirIsNotEmpty) {
            return true;
        }
    }

    return false;
}

A few things to note:

  • I always install WordPress in the wordpress directory.
    • This makes it much easier to maintain a project in Git as you can simply ignore the wordpress directory.
  • Since I have WordPress in it's own directory, I like to use my own wp-content to simplify project management.
  • You can easily modify this for your needs.

This health check only checks the system to ensure it is setup properly. No need to query the DB as this is not necessary for the health check. That is a config issue that should be handled before you setup your system in the first place.

2

In case it helps, I have created a very basic WordPress plugin that exposes a health check endpoint mapped to /health.

You can find the Health Endpoint plugin in the following link: https://wordpress.org/plugins/health-endpoint/

1

Edited:

I have used /wp-admin/images/wordpress-logo.svg (on previous versions it worked with /favicon.ico) Sounds more like a hack but if wordpress doesn't work you get 5xx error otherwise 200=already installed, 200=not installed.

2
  • 1
    The installation wizard is actually a 301 redirect. So it would fail if not setup
    – Jeremy
    Mar 18, 2020 at 17:26
  • genius 🙌 ive been messing around with these damn pods for hours. Was able to test on other WP sites: wpbeginner.com/wp-admin/images/wordpress-logo.svg
    – David
    Jan 12, 2022 at 0:19
1

I was looking also for a way to ping backend to check if wordpress is healthy with using minimum resources, I found that sending a ping to wp-load.php at your wordpress root installation is very helpful, it return 200 response with an empty file. if for example there is an error in your code or wordpress is in maintenance mode it will return 503 error.

I got the idea from this post: ELB Health Check Target for WordPress Networks

In the post he gives an example how to use it with wordpress multisites as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .