PHP-FPM has a very useful built-in status page.
You can access it over web and also write scripts to monitor your PHP-FPM sites health remotely.
Enabling PHP-FPM Status Page
Edit PHP-FPM Config
In file /etc/php5/fpm/pool.d/www.conf
, find pm.status_path
variable.
vim +/pm.status_path /etc/php5/fpm/pool.d/www.conf
Uncomment that line (if its commented).
Default value is /status
. You can change it to something else. May be you can add pool-prefix if you are running multiple PHP pools.
pm.status_path = /status
Edit Nginx config
Next, in Nginx config for example.com
add a location block like below:
location = /status {
access_log off;
allow 127.0.0.1;
allow 1.2.3.4#your-ip;
deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
location = /ping {
access_log off;
allow 127.0.0.1;
allow 1.2.3.4#your-ip;
deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
Do not forget to replace your IP address. For security reasons, its better to keep your PHP-FPM status page private.
Reload PHP-FPM and Nginx config for changes to take effect.
PHP-FPM Status Sample
Default PHP-FPM Status Sample
Now, open http://example.com/status
in browser to see summarised stats like below
pool: www process manager: dynamic start time: 17/May/2013:13:54:02 +0530 start since: 886617 accepted conn: 1619617 listen queue: 0 max listen queue: 0 listen queue len: 0 idle processes: 28 active processes: 2 total processes: 30 max active processes: 31 max children reached: 0 slow requests: 0
Below is meaning of different values
- pool – the name of the pool. Mostly it will be www.
- process manager – possible values static, dynamic or ondemand. We never use static. Trying ondemand is on todo list.
- start time – the date and time FPM has started or reloaded. Reloading PHP-FPM (
service php5-fpm reload
) reset this value. - start since – number of seconds since FPM has started
- accepted conn – the number of request accepted by the pool
- listen queue – the number of request in the queue of pending connections. If this number is non-zero, then you better increase number of process FPM can spawn.
- max listen queue – the maximum number of requests in the queue of pending connections since FPM has started
- listen queue len – the size of the socket queue of pending connections
- idle processes – the number of idle processes
- active processes – the number of active processes
- total processes – the number of idle + active processes
- max active processes – the maximum number of active processes since FPM has started
- max children reached – number of times, the process limit has been reached, when pm tries to start more children. If that value is not zero, then you may need to increase max process limit for your PHP-FPM pool. Like this, you can find other useful information to tweak your pool better way.
- slow requests – Enable php-fpm slow-log before you consider this. If this value is non-zero you may have slow php processes. Poorly written mysql queries are generally culprit.
Full PHP-FPM Status Sample
If you want detailed stats, you can pass argument ?full
to status page URL. It will become http://example.com/status?full
In additional to pool-level summary we have seen above, this will show extra details for per process. A sample is below:
pid: 1419692 state: Idle start time: 27/May/2013:20:06:12 +0530 start since: 287 requests: 32 request duration: 188927 request method: GET request URI: /feed.php?uid=12997446135571490564 content length: 0 user: - script: /var/www/example.com/htdocs/feed.php last request cpu: 5.29 last request memory: 524288
Below is meaning of different values
- pid – the PID of the process. You can use this PID to kill a long running process.
- state – the state of the process (Idle, Running, …)
- start time – the date and time the process has started
- start since – the number of seconds since the process has started
- requests – the number of requests the process has served
- request duration – the duration in µs of the requests
- request method – the request method (GET, POST, …)
- request URI – the request URI with the query string
- content length – the content length of the request (only with POST)
- user – the user (PHP_AUTH_USER) (or ‘-‘ if not set)
- script – the main PHP script called (or ‘-‘ if not set)
- last request cpu – the %cpu the last request consumed. it’s always 0 if the process is not in Idle state because CPU calculation is done when the request processing has terminated
- last request memorythe max amount of memory the last request consumed. it’s always 0 if the process is not in Idle state because memory calculation is done when the request processing has terminated
Note: If the process is in Idle state, then informations are related to the last request the process has served. Otherwise informations are related to the current request being served.
PHP-FPM Status Page Output formats
By default the status page output is formatted as text/plain. Passing either ‘html’, ‘xml’ or ‘json’ in the query string will return the corresponding output syntax.
Examples for summary status page:
- http://example.com/status
- http://example.com/status?json
- http://example.com/status?html
- http://example.com/status?xml
Example for detailed status page:
- http://example.com/status?full
- http://example.com/status?json&full
- http://example.com/status?html&full
- http://example.com/status?xml&full
You can use json or xml format to process status page output programatically. HTML is useful when viewing detailed status report.
getting bad gateway error
Can you check
error.log
? Most likely in/var/log
I’m also getting a 502 Bad Gateway error.
This is the output from the error.log file:
2014/01/09 08:09:52 [error] 7497#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: magento, request: “GET /status HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “www.mydomain.co.uk”
Check if PHP FPM is running. Also check if both PHP and Nginx config are modified and both services restarted/reloaded.
Yes PHP FPM is running.
Just to clarify, I edited the default website config file in Nginx with the location code: /etc/nginx/sites-available/default
I have restarted both PHP-FPM and nginx
What is default domain name (server_name)?
If default is IP, then you need use http://ip-address/status
If its some domain like, example.com, you need use http://example.com/status
I generally copy location block in main site’s nginx config.
It’s a domain name http://www.explosivenutrition.co.uk
Please clarify where to ‘use’ the domain name:
“If its some domain like, example.com, you need use http://example.com/status”
Also, I’m not sure what you mean by the main site’s nginx config. Which file is this exactly?
Thanks for your help.
It depends on your server layout. By our conventions – http://rtcamp.com/wordpress-nginx/tutorials/conventions/
Your server must have a file called
/etc/nginx/sites-available/explosivenutrition.co.uk
in which you will paste location block.If your layout is different, you just need to figure out correct
server{..}
block to addlocation{..}
codes I provided in article.There isn’t a specific one for the domain name, instead it’s just set as ‘default’ – /etc/nginx/sites-available/default
I forgot to mention the domain name is running a Magento site, not WordPress – does that make any difference?
Assuming you are on Ubuntu:
Install package –
apt-get install libfcgi0ldbl
Then copy-paste following codes:
If you do not see status output on command-line, then there is something wrong with PHP-FPM (either config or build).
Thanks. If I try that I get the following:
“Could not connect to 127.0.0.1:9000”
I’ve noticed in the same nginx config file that there are references to a different port: fastcgi_pass 127.0.0.1:9001;
However, when I try using 9001 instead I get a ‘file not found’ error.
php-frm is definitely installed and working. It keeps causing the CPU to max out at 100% occasionally, which I why I’m trying to set-up the status access to see what’s going on.
Make sure PHP-FPM and Nginx agree on socket values.
Ref – https://rtcamp.com/tutorials/php/fpm-sysctl-tweaking/
Also https://rtcamp.com/tutorials/linux/increase-open-files-limit/ and https://rtcamp.com/tutorials/linux/sysctl-conf/ might help to reduce CPU load.
The PHP-FPM config is already set to ‘listen = 127.0.0.1:9000’
Which Nginx file are you referring to by this “Next, open Nginx virtual-host config file(s).”
Is that the same file where I added the ‘location’ block?
Make sure nginx also uses 9000. About config files, it depends on your layout.
Did you try this – https://rtcamp.com/tutorials/php/directly-connect-php-fpm/ ?
The nginx config files have a number of refernces to port 9001 instead: fastcgi_pass 127.0.0.1:9001;
So I changed the php-fpm http://www.conf to: listen = 127.0.0.1:9001
They all now match, but instead of the bad gateway I’m getting a 404 file not found error.
If PHP and Nginx ports are matching then something else must be wrong.
If you can start afresh, try going with easyengine – https://rtcamp.com/easyengine
No problem getting it to work. Now that I/we can see the stats, what exactly are we on the lookout for?
While the definitions are given for each term, it is not exactly clear what good numbers are and what bad numbers are, nor what to do should we encounter bad numbers. Like if ‘x’ term = ‘xyz’ then increase this value etc..
Most values are just informative.
We try to make sure that “max children reached” remains 0 (zero).
Also you can be concerned about “slow requests” if you have enabled PHP-FPM slowlog – https://rtcamp.com/tutorials/php/fpm-slow-log/
Very nice description. Thank you
Hello,
After following the above instructions, I receive the following error upon restarting nginx:
nginx: [emerg] no port in upstream “php” in /etc/nginx/nginx.conf:72
I’m running this on Arch Linux ARM. I tried replacing “php” with “127.0.0.1:9000” in the offending line, which eliminated the error; however, the resulting php-fpm status page was simply blank. (I tried “127.0.0.1:9000” because it’s used in another “location” block statement, “fastcgi_pass 127.0.0.1:9000;“).
Any ideas how to fix this?
buzz
Just open file /etc/nginx/nginx.conf and jump to line 72
Most likely you will find
upstream {..}
block there. You can delete it and try again.Hello Rahul,
Line 72 of the /etc/nginx/nginx.conf file is:
fastcgi_pass php;
This is the line from your instructions above. The “php” element in this statement is causing problems. Why?
buzz
Use
fastcgi_pass 127.0.0.1:9000;
For
fastcgi_pass php;
to work you need to haveupstream php
block elsewhere.I will update this article.
Hello, slow requests doesn’t appear in status after I turn on slow log:
slowlog = /var/log/php5/slow.log
request_slowlog_timeout = 10s
Probably I need something else to do, to se it ?
It could be because every request is getting completed in less than 10s.
Try setting a value to 1s. If that doesn’t work, then there could be an issue with config.
Also check if file
/var/log/php5/slow.log
exists physically on disk.Hi, thanks for this neat tip. I was not aware if this neat PHP5-FPM feature.
Small remark: There seem to be missing some trailing “;” signs in the nginx site conf code. Plus it might be helpful to mention the use of “unix:/var/run/php5-fpm.sock” for those that followed other tutorials on this site 🙂
By the way, what that “ping” in
location ~ ^/(status|ping)$
for?If your goal is to check if PHP-FPM is replying to request only, you can open
/ping
path and see if it returnspong
.It may sound funny but if you have external process monitoring your FPM, such a short reply can reduce bandwidth. Though I think, in practise
/status
output will also fit in single packet. May be FPM need to do some calculation to return/status
but for/ping/
it returns a predefined string only.Cool ! Never thought of such usage. Very useful 🙂
I updated article for missing
;
.I will update cross-check all tutorials for
unix:/var/run/php5-fpm.sock
.Our internal convention is to have
upstream { }
block for PHP backend so we keep adding/removing FPM pools and sometimes use mix of tcp/ip and unix-based sockets.I will try to stick to one method and update all articles soon.
Hi ,
‘full’ option is not working. I am just getting the basic stats.Any ideas why?
Here is my location section
Thanks.
I think following line is not needed:
Please use my config from article above.
Thanks for the set of good instructions. It also got the PHP-FPM ping/pong test working so that I could set up an alert on Copperegg.com to warn if it goes down.
Hi, i did everything as this tutorial but when i request /status i get just a BLANK page.
I have restarted all services and the logs of nginx and php-fpm doesn’t contain anything related … don’t know what to do
Try removing
access_log off;
and see if your hits to/status
are reaching nginx.Most likely it could be mismatch with
pm.status_path = /status
in/etc/php5/fpm/pool.d/www.conf
Try this one
location ~ ^/(status|ping|apc_info.php)$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass backend;
allow 127.0.0.1;
deny all;
}
For those who are getting a gateway error upon using this guide, this is due to PHP5-FPM unable to create the log files needed, so make sure that you create the folders and files for your logs as per http://www.conf and php5-fpm.conf before you complete this guide.
For example for the following paramters:
access.log = /var/log/php5-fpm/access.log in http://www.conf
slowlog = /var/log/php5-fpm/slow.log in http://www.conf
error_log = /var/log/php5-fpm/daemon.log in php5-fpm.log
You would need to create the folders yourself for /var/log/php5-fpm/ as the default parameters link to a non-existing path for some reason on ubuntu.
You cannot get php restart error after following this article which is related to log files.
This article doesn’t change any log file path so how can it complain about folder creation?
Looks like you have mixed instructions from multiple sources.
That’s what I thought, but the default path log/php5-fpm.log was inaccessible, and the error the guys are complaining about in the comments disappeared after I fixed it to a path that exists.
That path has nothing to do with this article.
Anyway, default `log/php5-fpm` refers to `/var/log` which is created by OS. I did not asked to change log path in this article.
Only line which is sensitive in this article is: `fastcgi_pass 127.0.0.1:9000;`
If somebody has FPM listening on other port/socket, they need to change this line.
Then it might be that my php5-fpm did not have access to create the file php5-fpm.log, which is why it worked when I created it.