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.