95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?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";
|