Skip to content

Instantly share code, notes, and snippets.

@ndunks ndunks/stats.php
Last active Jun 21, 2018

Embed
What would you like to do?
PHP-FPM status wrapper for each serverpilot web apps
<?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
You can’t perform that action at this time.