PHP-FPM Introduction and Configuration

Source: Internet
Author: User
Tags http 200 stop script

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?
  1. The This configuration was for PHP 5.3.10 for heavy loaded servers
  2. [Global]
  3. Error_log = syslog
  4. Log_level = Error
  5. Rlimit_files = 655360
  6. Rlimit_core = 0
  7. [MyApp]
  8. user = Nobody
  9. Group = Nobody
  10. Listen = Fpm.sock
  11. Listen.allowed_clients = 127.0.0.1
  12. PM = dynamic
  13. Pm.max_children = 1000
  14. Pm.start_servers = 20
  15. Pm.min_spare_servers = 10
  16. Pm.max_spare_servers = 30
  17. Pm.max_requests = 500
  18. ; System max Load (Request number) = Max_children * max_requests = 500,000
  19. Pm.status_path =/fpm_status
  20. Ping.path =/status
  21. Ping.response = OK
  22. Slowlog =/data/phplogs/phpfpm_slow.log
  23. Request_slowlog_timeout = 3s
  24. ; listen.backlog = 262144
  25. ; The timeout for serving a single request after which the worker process would
  26. ; Be killed. This option should is used when the ' max_execution_time ' ini option
  27. ; Does not a stop script execution for some reason. A value of ' 0 ' means ' off '.
  28. ; Available units:s (econds) (default), M (inutes), H (ours), or D (ays)
  29. ; Default value:0
  30. ; request_terminate_timeout = 0
  31. Rlimit_files = 655360
  32. Rlimit_core = 0
  33. security.limit_extensions =. php. html
  34. 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?
    1. server {
    2. Listen 80;
    3. server_name xxx;
    4. #在这里插入, set to global variable
    5. Set $fpm _sock ' Unix:/var/run/myapp/fpm.sock ';
    6. ...
    7. }
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?
    1. Location =/{
    2. Include Fastcgi_params;
    3. Root xxx/apps/fnt;
    4. Fastcgi_pass $FPM _sock;
    5. Fastcgi_index index.php;
    6. Fastcgi_param Script_filename xxx/apps/fnt/index.php;
    7. }
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?
  1. server {
  2. Listen 8360;
  3. server_name localhost;
  4. Location/fpm_status {
  5. if ($arg _sid = ") {
  6. rewrite ^/fpm_list last;
  7. }
  8. Include Fastcgi_params;
  9. Fastcgi_pass unix:/var/run/$arg _sid/fpm.sock;
  10. }
  11. #列出所有正在运行的fpm-sock
  12. location/fpm_list {
  13. Default_type text/html;
  14. Content_by_lua "
  15. Ngx.print ('
  16. local cmd = [[/bin/ls-1/var/run/| grep rgapp-| awk ' {print \ ' <p><a href=?sid=\ ' $1\ ' >\ ' $1\ ' </a>\ '} ']
  17. Local F = io.popen (cmd, ' R ')
  18. Local log = F:read (' *a ')
  19. F:close ()
  20. Ngx.print (log)
  21. ";
  22. }
  23. ...
  24. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.