Skip to content

Instantly share code, notes, and snippets.

@ndunks
Last active June 21, 2018 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndunks/8ac2d32c2d4ac50d51a4adae55fb77d6 to your computer and use it in GitHub Desktop.
Save ndunks/8ac2d32c2d4ac50d51a4adae55fb77d6 to your computer and use it in GitHub Desktop.
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