PHP-FPM Configuration 101 (php-fpm.conf, www.conf)

PHP FastCGI Process Manager (PHP-FPM)

PHP-FPM Configuration 101FPM (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-

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 :)

Note: Following configuration values is used in VPS with Debian Linux [512 MB, 1 CPU core] along with NGINX and MySQL deployed, system is running smooth.

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:

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:

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:

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)

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:

listen = 127.0.0.1:9000

Need more pool for application specific

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:

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

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

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:

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)

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

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:

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 0 means off

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:

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 0
Configuration:

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:

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:

env[HOSTNAME] = $HOSTNAME
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Recommendation for PHP-FPM monitoring

Note:

  • 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
  • By default the status page only outputs short status. Passing full in the query string will also return status for each pool process

For example:

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:

pm.status_path = /status

Just for Handy Use – Putting together

php-fpm.conf  – php-fpm configuration 101

emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10
events.mechanism = epoll

Pool config (www.conf)  – php-fpm configuration 101

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.