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:

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:

Define process_control_timeout Value

Purpose: Time limit for child processes to wait for a reaction on signals from master
Default Value:
Configuration:

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)


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:

Need more pool for application specific

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:

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

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

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:

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)

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

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:

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

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:

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:

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:

Pass Environment variables to PHP-FPM process

Purpose: All $VARIABLEs are taken from the current environment.
Default Value: clean env
Configuration:


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:

Purpose: The URI to view the FPM status page
Default Value: Not defined
Configuration:


Just for Handy Use – Putting together

php-fpm.conf  – php-fpm configuration 101

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


Reference: PHP-FPM Documentation and My Learning from Implementation.

  • Pingback: blog.ngen.com.ar » Blog Archive » Configuración segura de un webserver con nginx + php-fpm()

  • Bruno Rodrigues

    Thanks for your article, it is awesome!
    How am I supposed to load php-fpm.conf?

    http://www.conf is already located at /etc/php5/fpm/pool.d/www.conf.

    • @disqus_eTMckUvCfx:disqus – When you install php-fpm, default configuration will present. Then you may have customize those configuration. This article provides information around configuration, so you can choose appropriately.

      php-fpm supports multiple website pool with different config. For example: sample1.com-www.conf, sample2.com-www.conf. Then when you start php-fpm service both site configuration becomes active.

      Typically following command used to start php-fpm service Debian/ubuntu:
      sudo /etc/init.d/php5-fpm start
      OR
      sudo service php-fpm start

      Cheers,
      Jeeva