Last active
June 21, 2018 02:17
-
-
Save ndunks/8ac2d32c2d4ac50d51a4adae55fb77d6 to your computer and use it in GitHub Desktop.
PHP-FPM status wrapper for each serverpilot web apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Serverpilot PHP-FPM Status wrapper for all apps | |
* Install required package: | |
* $ sudo apt install libfcgi-bin | |
* | |
* Add .htaccess: | |
* RewriteRule ^php\-stats/ stats.php [NC,L] | |
* | |
* Place this script on 'default' web app. | |
* Usage URL: | |
* localhost/php-stats/app-name/status | |
* localhost/php-stats/app-name/ping | |
**/ | |
error_reporting(0); | |
$paths = explode('/', trim($_SERVER['REQUEST_URI'], '/\\\'"') ); | |
$app = preg_replace('/[^a-z\-]/', '-', @$paths[1]); | |
$path= 'status'; | |
// Keep stupid to make it safe | |
if(@$paths[2] == 'ping') | |
{ | |
$path = 'ping'; | |
} | |
$param = []; | |
$param['QUERY_STRING'] = $_SERVER['QUERY_STRING']; | |
$param['SCRIPT_NAME'] = '/php-fpm-' . $path; | |
$param['SCRIPT_FILENAME'] = '/php-fpm-' . $path; | |
$param['REQUEST_METHOD'] = 'GET'; | |
$socket_file = "/srv/users/serverpilot/run/$app.php-fpm.sock"; | |
$output = []; | |
$retval = null; | |
$cmd = ""; | |
if(!file_exists($socket_file)) | |
{ | |
die('Invalid path'); | |
} | |
foreach ($param as $key => $value) { | |
$cmd .= "$key=" . escapeshellarg($value) . ' '; | |
} | |
$cmd .= "cgi-fcgi -bind -connect '$socket_file'"; | |
//echo "$cmd\n"; | |
exec($cmd, $output, $retval); | |
$is_header=true; | |
foreach ($output as $val) | |
{ | |
if(empty($val) && $is_header) | |
{ | |
$is_header = false; | |
}elseif( $is_header ) | |
{ | |
header($val); | |
}else{ | |
echo "$val\r\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment