PHP FastCGI Process Manager (PHP-FPM)
FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. PHP-FPM has two major portion of configuration and also uses php.ini to load PHP configuration-
- Global Directives (php-fpm.conf)
- Pool Directives (default is www.conf, you can name it site purpose eg. blog.conf, forum.conf, etc.)
Objective of PHP-FPM Configuration 101 article is to provide purpose, insight and recommended configuration, which will help you from day one in optimal way.
Know the purpose, take it, tune it 🙂
Let’s begin with Global Directives.
Global Directives (php-fpm.conf)
Define emergency_restart_threshold Value
Purpose: If this number of child processes exit with Segmentation, page fault, and access violation (SIGSEGV or SIGBUS) within the time interval set by
emergency_restart_interval then FPM will restart
Default Value: 0 means Off
Configuration:
1 |
emergency_restart_threshold = 10 |
Define emergency_restart_interval Value
Purpose: Interval of time used to determine when a graceful restart will be initiated. This can be useful to work around accidental corruptions in an accelerator’s shared memory
Default Value: 0
Available Units: s(econds), m(inutes), h(ours), or d(ays)
Configuration:
2 |
emergency_restart_interval = 1m |
Define process_control_timeout Value
Purpose: Time limit for child processes to wait for a reaction on signals from master
Default Value:
Configuration:
3 |
process_control_timeout = 10 |
You know the Platform, define Events Notification Mechanism for FPM
Purpose: Choosen mechanism used by FPM for events notification
Supported Mechanism(s):
select (any POSIX os)
poll (any POSIX os)
epoll (linux >= 2.5.44)
kqueue (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
/dev/poll (Solaris >= 7)
port (Solaris >= 10)
4 |
events.mechanism = epoll |
Pool Directives (default name is www.conf)
Multiple pools of child processes may be started with different listening ports and different management options. The name of the pool will be used in logs and stats. There is no limitation on the number of pools which FPM can handle. So system limit is the FPM limit.
Define Listen mode, FPM supports Unix Socket and TCP Socket
Purpose: It used to mode connecting mechanism of PHP request from frontend server (like nginx, etc)
Default Value: TCP Socket
Configuration:
1 |
listen = 127.0.0.1:9000 |
Need more pool for application specific
1 2 |
listen = 127.0.0.1:9001 listen = 127.0.0.1:9002 |
Define Backlog limit
Purpose: The backlog argument defines the maximum length to which the queue of pending connections
Default Value: 128 for Linux / -1 for on FreeBSD and OpenBSD (-1 means unlimited)
Configuration:
4 |
listen.backlog = -1 |
Define Process Manager controll mechanism of Child process
Possible Values: static, dynamic, ondemand
Describing Values: widely used values are static and dynamic
- static – a fixed number ( pm.max_children) of child processes
- dynamic – the number of child processes are set dynamically based on the following directives. With this process management, there will be always at least 1 children
- ondemand – no children are created at startup. Children will be forked when new requests will connect
Configuration: This is a mandatory value
5 |
pm = dynamic |
Define Process Manager Maximum children limit
Purpose: The number of child processes to be created when pm is set to ‘static’ and the maximum number of child processes when pm is set to
dynamic or
ondemand
Note: Equivalent to the ApacheMaxClients directive with
mpm_prefork
Default Value: 5
Configuration: This value is mandatory
6 |
pm.max_children = 5 |
Define support directives for Process Manager definition
Purpose: Child process management configuration for FPM, this configuration directives is applicable, when pm is set to
dynamic
Configuration:
7 8 9 |
pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 |
Define Maxmium Requests limit for child process
Purpose: The number of requests each child process should execute before respawning (recreation of child process)
Default Value: 0 (zero)
Configuration: For endless request processing specify 0 (zero)
10 |
pm.max_requests = 10000 |
Define Request Slowlog Time out
Purpose: The timeout for serving a single request after which a PHP backtrace will be dumped to the
slowlog file.
Default Value: 0 (zero)
Configuration: A value of
0s means
off
11 |
request_slowlog_timeout = 5s |
Define Script Slow log
Purpose: Logging slow requests information into slowlog
Default Value: not defined
Note: slowlog is mandatory if
request_slowlog_timeout is set
Configuration:
12 |
slowlog = /var/log/$pool.log.slow |
Define Request Terminate Time Limit
Purpose: The timeout for serving a single request after which the worker process will be killed. This option should be used when the
max_execution_time ini option does not stop script execution for some reason
Default Value: 0 (zero)
Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
Configuration: A value of
means
off
13 |
request_terminate_timeout = 300s |
Define Open File Descripter Limit
Purpose: This directive allows to override a system defined limit for Open File descriptor for PHP-FPM
Default Value: System defined value
Alternate (System defined): soft & hard nofile limit at system level
Configuration:
14 |
rlimit_files = 131072 |
Define Max Core limit
Purpose: A process can set its soft RLIMIT_CORE resource limit to place an upper limit on the size of the core dump file that will be produced if it receives a “core dump” signal
Default Value: System defined value
Possible Values:
unlimited or an integer greater or equal to
Configuration:
15 |
rlimit_core = unlimited |
Define Whether to catch workers output
Purpose: Redirect worker stdout and stderr into main error log. If not set, stdout and stderr will be redirected to
/dev/null according to FastCGI specs
Default Value: no
Consideration: On high loaded environment, this can cause some delay in the page process time (several ms)
Configuration:
16 |
catch_workers_output = yes |
Pass Environment variables to PHP-FPM process
Purpose: All
$VARIABLEs are taken from the current environment.
Default Value: clean env
Configuration:
17 18 19 20 |
env[HOSTNAME] = $HOSTNAME env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp |
Recommendation for PHP-FPM monitoring
For example:
1 2 3 4 |
http://example.bar/status http://example.bar/status?json http://example.bar/status?full http://example.bar/status?json&full |
Purpose: The URI to view the FPM status page
Default Value: Not defined
Configuration:
1 |
pm.status_path = /status |
Just for Handy Use – Putting together
php-fpm.conf – php-fpm configuration 101
1 2 3 4 |
emergency_restart_threshold = 10 emergency_restart_interval = 1m process_control_timeout = 10 events.mechanism = epoll |
Pool config (www.conf) – php-fpm configuration 101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
listen = 127.0.0.1:9000 listen.backlog = -1 pm = dynamic pm.max_children = 9 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 4 pm.max_requests = 10000 request_slowlog_timeout = 5s slowlog = /var/log/$pool.log.slow request_terminate_timeout = 300s rlimit_files = 131072 rlimit_core = unlimited catch_workers_output = yes env[HOSTNAME] = $HOSTNAME env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp |
Reference: PHP-FPM Documentation and My Learning from Implementation.