What is PHP-FPM?
The full name is the PHP fastcgi process Manager, the PHP fastcgi processes manager, which dynamically evokes the CGI process and destroys it to reach dynamic tuning CGI numbers compared to the fastcgi static arousal CGI,FPM, based on the pressure of the access. This allows for efficient use of memory. In addition, there are other advantages, such as FPM can also be smooth overloaded PHP configuration, because FPM uses Unix-socket to communicate with the server, so there is no need to configure the CGI port; fpm has better status output and Slowlog logs, You can give more error details when you are 502.
PHP-FPM Configuration
Since the php5.3.3 version has been integrated with FPM, is no longer a third-party package, if the use of php5.3.3 previous version you have to install the FPM package, for the php5.2.x version, FPM configuration file or XML format, php5.3.x start FPM configuration file has been supported INI The formula.
The following is an INI-formatted configuration file
[Plain]View Plaincopyprint?
- The This configuration was for PHP 5.3.10 for heavy loaded servers
- [Global]
- Error_log = syslog
- Log_level = Error
- Rlimit_files = 655360
- Rlimit_core = 0
- [MyApp]
- user = Nobody
- Group = Nobody
- Listen = Fpm.sock
- Listen.allowed_clients = 127.0.0.1
- PM = dynamic
- Pm.max_children = 1000
- Pm.start_servers = 20
- Pm.min_spare_servers = 10
- Pm.max_spare_servers = 30
- Pm.max_requests = 500
- ; System max Load (Request number) = Max_children * max_requests = 500,000
- Pm.status_path =/fpm_status
- Ping.path =/status
- Ping.response = OK
- Slowlog =/data/phplogs/phpfpm_slow.log
- Request_slowlog_timeout = 3s
- ; listen.backlog = 262144
- ; The timeout for serving a single request after which the worker process would
- ; Be killed. This option should is used when the ' max_execution_time ' ini option
- ; Does not a stop script execution for some reason. A value of ' 0 ' means ' off '.
- ; Available units:s (econds) (default), M (inutes), H (ours), or D (ays)
- ; Default value:0
- ; request_terminate_timeout = 0
- Rlimit_files = 655360
- Rlimit_core = 0
- security.limit_extensions =. php. html
- php_admin_value[' date.timezone '] = ' Asia/shanghai '
This configuration was for PHP 5.3.10 for heavy loaded servers[global]error_log = Sysloglog_level = Errorrlimit_files = 65 5360rlimit_core = 0[myapp]user = Nobodygroup = Nobodylisten = fpm.socklisten.allowed_clients = 127.0.0.1pm = Dynamicpm.max _children = 1000pm.start_servers = 20pm.min_spare_servers = 10pm.max_spare_servers = 30pm.max_requests = 500; System max Load (Request number) = Max_children * Max_requests = 500,000pm.status_path =/fpm_statusping.path =/statuspin G.response = Okslowlog =/data/phplogs/phpfpm_slow.logrequest_slowlog_timeout = 3s;listen.backlog = 262144; The timeout for serving a single request after which the worker process would; Be killed. This option should is used when the ' max_execution_time ' ini option; Does not a stop script execution for some reason. A value of ' 0 ' means ' off '.; Available units:s (econds) (default), M (inutes), H (ours), or D (ays); Default value:0;request_terminate_timeout = 0rlimit_files = 655360rlimit_core = 0security.limit_extensions =. phP. htmlphp_admin_value[' date.timezone '] = ' Asia/shanghai '
Parameter description:
Error_log string
The location of the error log. Default: Installation path #install_prefix#/log/php-fpm.log.
Log_level string
The error level. The available levels are: alert (which must be processed immediately), error (Error condition), warning (warning condition), notice (general important information), debug (debug information). Default: Notice.
Emergency_restart_threshold int
FPM restarts if the child process receives a SIGSEGV or Sigbus exit information number for that parameter setting within the time set by Emergency_restart_interval. 0 means ' Turn off this feature '. Default value: 0 (off).
Emergency_restart_interval Mixed
The emergency_restart_interval is used to set the interval between smooth restarts. Doing so helps to solve the problem of using shared memory in the accelerator. Available units: s (seconds), M (min), H (Hours), or D (Day). Default unit: s (seconds). Default value: 0 (off).
Process_control_timeout Mixed
Sets the time-out period for the child process to accept the primary process multiplexing signal. Available units: s (seconds), M (min), H (Hours), or D (day) default units: s (seconds). Default value: 0.
Daemonize Boolean
Set FPM to run in the background. Set ' no ' to keep FPM running in the foreground for debugging. Default value: Yes.
Run configuration section
In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool.
Listen string
Sets the address that accepts the FASTCGI request. The available formats are: ' Ip:port ', ' Port ', '/path/to/unix/socket '. Each process pool needs to be set.
Listen.backlog int
Sets the half-connection queue length for listen (2). ' -1′ means no limit. Default value:-1.
Listen.allowed_clients string
Sets the server IPV4 address that is allowed to connect to fastcgi. Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in PHP FastCGI (5.2.2+). Only works on TCP snooping. Each address is separated by commas. If it is not set or empty, any server is allowed to request a connection. Default value: Any.
Listen.owner string
If used, represents the permission to set UNIX sockets. In Linux, read and write permissions must be set to be used for Web server connections. In many BSD-derived systems, permissions can be ignored to allow free connections. Default value: The user who is running the lease, with a permission of 0666.
Listen.group string
See Listen.owner.
Listen.mode string
See Listen.owner.
User string
The FPM process runs as a UNIX user. Must be set.
Group string
The FPM process runs the UNIX user group. If not set, the default user's group is used.
PM string
Sets how the Process manager manages child processes. Available values: static, dynamic. Must be set.
The number of static– child processes is fixed (pm.max_children).
The number of dynamic– child processes is dynamically set based on the following configuration: Pm.max_children, Pm.start_servers, Pm.min_spare_servers, Pm.max_spare_servers.
Pm.max_children int
The number of child processes, which is created when PM is set to static, and when the PM is set to dynamic represents the maximum can be created. Must be set.
This option sets a limit on the number of requests that can be served at the same time. Similar to the maxclients settings in Apache mpm_prefork and the Php_fcgi_children environment variables in normal PHP fastcgi.
Pm.start_servers in
Sets the number of child processes created at startup. Used only when the PM is set to dynamic. Default value: Min_spare_servers + (Max_spare_servers–min_spare_servers)/2.
pm.min_spare_servers int
Sets the minimum number of idle service processes. Used only when the PM is set to dynamic. Must be set.
pm.max_spare_servers int
Sets the maximum number of idle service processes. Used only when the PM is set to dynamic. Must be set.
pm.max_requests int
Sets the number of requests for the service before each child process is reborn. is useful for third-party modules that may have a memory leak. If set to ' 0′, the request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.
Pm.status_path string
The URL of the FPM status page. If not set, the status page cannot be accessed. Default value: None.
Ping.path string
The ping URL of the FPM monitoring page. If not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Note that you must start with a slash (/).
Ping.response string
Used to define the return of the ping request accordingly. Returns the Text/plain format text for HTTP 200. Default value: Pong.
Request_terminate_timeout Mixed
Sets the time-out for a single request to abort. This option may be useful for scripts in php.ini settings where ' Max_execution_time ' is not aborted for some special reason. Set to ' 0′ ' to indicate ' Off '. Available units:s (econds) (default), M (inutes), H (ours), or D (ays). Default value:0.
Request_slowlog_timeout Mixed
When a request for this setting time-out expires, the corresponding PHP call stack information is written to the slow log. Set to ' 0′ ' to indicate ' Off '. Available units: s (seconds) (default), m (min), H (Hours), or D (Day). Default value: 0.
Slowlog string
Logging of slow requests. Default value: #INSTALL_PREFIX #/log/php-fpm.log.slow.
Rlimit_files int
Sets the Rlimit limit for the file open descriptor. Default value: System-defined value.
Rlimit_core int
Sets the maximum limit value for the core rlimit. Available values: ' Unlimited ', 0, or positive integer. Default value: System-defined value.
Chroot string
The Chroot directory at startup. The defined directory needs to be an absolute path. If not set, Chroot is not used.
ChDir string
Sets the startup directory, which is automatically chdir to the directory when it starts. The defined directory needs to be an absolute path. Default value: Current directory, or/directory (chroot).
Catch_workers_output Boolean
REDIRECT StdOut and stderr to the primary error log file during the run. If not set, stdout and stderr will be redirected to/dev/null according to the FASTCGI rules. Default value: Empty.
Nginx Configuration
If you use Unix-socket to match with Nginx, the configuration in Nginx should be changed accordingly.
1. Declare the global FPM variable below the server_name of the Ngnix configuration
[Plain]View Plaincopyprint?
- server {
- Listen 80;
- server_name xxx;
- #在这里插入, set to global variable
- Set $fpm _sock ' Unix:/var/run/myapp/fpm.sock ';
- ...
- }
server { listen; server_name xxx; #在这里插入, set $fpm _sock ' unix:/var/run/myapp/fpm.sock ' for global variables ...}
2. Then transform all fastcgi_pass fields
[Plain]View Plaincopyprint?
- Location =/{
- Include Fastcgi_params;
- Root xxx/apps/fnt;
- Fastcgi_pass $FPM _sock;
- Fastcgi_index index.php;
- Fastcgi_param Script_filename xxx/apps/fnt/index.php;
- }
Location =/{ include fastcgi_params; Root xxx/apps/fnt; Fastcgi_pass $fpm _sock; Fastcgi_index index.php; Fastcgi_param script_filename xxx/apps/fnt/index.php; }
FPM Status
Configure in Nginx.conf
[Plain]View Plaincopyprint?
- server {
- Listen 8360;
- server_name localhost;
- Location/fpm_status {
- if ($arg _sid = ") {
- rewrite ^/fpm_list last;
- }
- Include Fastcgi_params;
- Fastcgi_pass unix:/var/run/$arg _sid/fpm.sock;
- }
- #列出所有正在运行的fpm-sock
- location/fpm_list {
- Default_type text/html;
- Content_by_lua "
- Ngx.print ('
- local cmd = [[/bin/ls-1/var/run/| grep rgapp-| awk ' {print \ ' <p><a href=?sid=\ ' $1\ ' >\ ' $1\ ' </a>\ '} ']
- Local F = io.popen (cmd, ' R ')
- Local log = F:read (' *a ')
- F:close ()
- Ngx.print (log)
- ";
- }
- ...
- }
server { listen 8360; server_name localhost; Location/fpm_status { if ($arg _sid = ") { rewrite ^/fpm_list last; } Include Fastcgi_params; Fastcgi_pass unix:/var/run/$arg _sid/fpm.sock; } #列出所有正在运行的fpm-sock location/fpm_list { default_type text/html; Content_by_lua " ngx.print ('
Because there are multiple applications in the application pool, the SID parameter is used to see the FPM status of the specified application.
View FPM status via address Http://xxx:8360/fpm_status?sid=myapp
PHP-FPM Introduction and Configuration