php-fpm: preinstall wp-cli, add WP-independent healthcheck
All checks were successful
nginx-build / build (push) Successful in 1m2s
php-fpm-build / build (7.4) (push) Successful in 5m23s

This commit is contained in:
Elijah Duffy
2025-12-09 13:30:27 -08:00
parent 238d3244b2
commit 0d00fd2ac4
4 changed files with 131 additions and 5 deletions

View File

@@ -0,0 +1,94 @@
<?php
// healthcheck.php
// Lightweight readiness probe independent of WordPress code.
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate');
$response = [
'status' => 'ok',
'php_version' => PHP_VERSION,
'checks' => [],
];
$httpStatus = 200;
// Filesystem/webroot probe
$webroot = '/var/www/html';
$fsStatus = [
'name' => 'filesystem',
'status' => 'ok',
'detail' => 'Webroot mounted',
];
if (!is_dir($webroot)) {
$fsStatus['status'] = 'error';
$fsStatus['detail'] = 'Missing /var/www/html mount';
$httpStatus = 503;
} elseif (!is_readable($webroot)) {
$fsStatus['status'] = 'error';
$fsStatus['detail'] = 'Webroot not readable';
$httpStatus = 503;
}
$response['checks'][] = $fsStatus;
// Database probe (optional if env vars missing)
$requiredEnv = ['WORDPRESS_DB_HOST', 'WORDPRESS_DB_USER', 'WORDPRESS_DB_PASSWORD', 'WORDPRESS_DB_NAME'];
$dbConfig = [];
$missingEnv = [];
foreach ($requiredEnv as $var) {
$value = getenv($var);
if ($value === false || $value === '') {
$missingEnv[] = $var;
} else {
$dbConfig[$var] = $value;
}
}
$dbStatus = [
'name' => 'database',
'status' => 'skipped',
'detail' => 'Database env vars missing',
];
if (empty($missingEnv)) {
[$host, $port] = (function (string $rawHost): array {
if (preg_match('/^\[(.+)\](?::(\d+))?$/', $rawHost, $matches)) {
return [$matches[1], isset($matches[2]) ? (int) $matches[2] : 3306];
}
if (substr_count($rawHost, ':') === 1 && strpos($rawHost, '::') === false) {
[$h, $p] = explode(':', $rawHost, 2);
return [$h, (int) $p];
}
return [$rawHost, 3306];
})($dbConfig['WORDPRESS_DB_HOST']);
$mysqli = @mysqli_init();
if ($mysqli !== false) {
@mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 3);
$connected = @$mysqli->real_connect(
$host,
$dbConfig['WORDPRESS_DB_USER'],
$dbConfig['WORDPRESS_DB_PASSWORD'],
$dbConfig['WORDPRESS_DB_NAME'],
$port
);
if ($connected) {
$dbStatus['status'] = 'ok';
$dbStatus['detail'] = 'DB connection OK';
$mysqli->close();
} else {
$dbStatus['status'] = 'error';
$dbStatus['detail'] = sprintf('DB connect failed: %s', $mysqli->connect_error ?? 'unknown error');
$httpStatus = 503;
}
} else {
$dbStatus['status'] = 'error';
$dbStatus['detail'] = 'Unable to init mysqli';
$httpStatus = 503;
}
} else {
$dbStatus['detail'] = 'Missing env: ' . implode(', ', $missingEnv);
}
$response['checks'][] = $dbStatus;
http_response_code($httpStatus);
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";