Php configuration of php-fpm startup parameters and configuration details

Source: Internet
Author: User
Tags http 200

Specify Directories

/Usr/local/php/sbin/php-fpm
/Usr/local/php/etc/php-fpm.conf
/Usr/local/php/etc/php. ini
I. startup parameters of php-fpm
Copy codeThe Code is as follows:
# Test the configuration of php-fpm
/Usr/local/php/sbin/php-fpm-t
/Usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php. ini-y/usr/local/php/etc/php-fpm.conf-t

# Start php-fpm
/Usr/local/php/sbin/php-fpm
/Usr/local/php/sbin/php-fpm-c/usr/local/php/etc/php. ini-y/usr/local/php/etc/php-fpm.conf

# Disable php-fpm
Kill-INT 'cat/usr/local/php/var/run/php-fpm.pid'

# Restart php-fpm
Kill-USR2 'cat/usr/local/php/var/run/php-fpm.pid'

2. Detailed explanation of important parameters of php-fpm.conf
Copy codeThe Code is as follows:
Pid = run/php-fpm.pid
# Pid settings, default var/run/php-fpm.pid in the installation directory, it is recommended to enable

Error_log = log/php-fpm.log
# Error log, var/log/php-fpm.log in the installation directory by default

Log_level = notice
# Error level. available level: alert (must be handled immediately), error (error), warning (warning), notice (important information), debug (debugging information ). default Value: notice.

Emergency_restart_threshold = 60
Emergency_restart_interval = 60 s
# Indicates the number of php-cgi processes with SIGSEGV or SIGBUS errors within the value set by emergency_restart_interval. If the number of php-cgi processes exceeds emergency_restart_threshold, php-fpm will be restarted elegantly. These two options are generally kept by default.

Process_control_timeout = 0
# Set the timeout time for the sub-process to receive the multiplexing signals of the main process. available unit: s (seconds), m (minutes), h (hours), or d (days) default unit: s (seconds ). default Value: 0.

Daemonize = yes
# Run fpm in the background. The default value is yes. You can change it to no for debugging. In FPM, you can use different settings to run multiple process pools. These settings can be set separately for each process pool.

Listen = 127.0.0.1: 9000
# Fpm listening port, that is, the address processed by php in nginx. Generally, the default value is enough. Available formats: 'IP: port', 'Port', '/path/to/unix/socket'. Each process pool needs to be set.

Listen. backlog =-1
# Number of backlogs.-1 indicates no limit. It is determined by the operating system. This line can be commented out. The meaning of backlog reference: http://www.3gyou.cc /? P = 41

Listen. allowed_clients = 127.0.0.1
# Allow access to the IP address of the FastCGI process. set any to an unlimited IP address. If you want to set nginx of other hosts to access this FPM process, set the IP address that can be accessed at a cost at listen. The default value is any. Each address is separated by commas (,). If it is not set or empty, any server is allowed to request a connection.

Listen. owner = www
Listen. group = www
Listen. mode = 0666
# Unix socket setting options. If you use tcp access, comment it out here.

User = www
Group = www
# Accounts and groups for starting processes

Pm = dynamic # for dedicated servers, pm can be set to static.
# The options for controlling sub-processes include static and dynamic. If static is selected, a fixed number of sub-processes is specified by pm. max_children. If dynamic is selected, it is determined by the following parameters:
Pm. max_children #, maximum number of child Processes
Pm. start_servers #, number of processes at startup
Pm. min_spare_servers # ensures the minimum number of idle processes. If the number of idle processes is smaller than this value, a new sub-process is created.
Pm. max_spare_servers #. ensure the maximum number of idle processes. If the number of idle processes exceeds this value, clean it.

Pm. max_requests = 1000
# Set the number of service requests before each sub-process is reborn. it is very useful for third-party modules that may have memory leaks. if it is set to '0', the request is always accepted. equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. default Value: 0.

Pm. status_path =/status
# FPM status page url. If it is not set, the status page cannot be accessed. Default Value: none. munin monitoring will use

Ping. path =/ping
# Ping URL of the FPM monitoring page. if this parameter is not set, the ping page cannot be accessed. this page is used to check whether FPM is alive and can respond to requests. note that it must start with a slash (/).

Ping. response = pong
# Defines the response of ping requests. The returned text is in the text/plain format of HTTP 200. Default Value: pong.

Request_terminate_timeout = 0
# Set the timeout duration of a single request. this option may apply to php. the 'max _ execution_time 'in ini settings is not useful for stopping the running script for some special reasons. set to '0' to 'off '. you can try to change this option when 502 errors occur frequently.

Request_slowlog_timeout = 10 s
# When a request times out, the corresponding PHP call stack information is completely written to the slow log. Set it to '0' to 'off'

Slowlog = log/$ pool. log. slow
# Slow request logging, used with request_slowlog_timeout

Rlimit_files = 1024
# Set the rlimit limit for the file opening descriptor. Default Value: the default value of the system defined value is 1024. You can use ulimit-n to view the limit and modify it by ulimit-n 2048.

Rlimit_core = 0
# Set the maximum limit value of the core rlimit. Available values: 'unlimited', 0, or positive integer. Default Value: system defined value.

Chroot =
# The Chroot directory at startup. The defined directory must be an absolute path. If it is not set, chroot is not used.

Chdir =
# Set the startup directory. The directory will be automatically Chdir to this directory at startup. The defined directory must be an absolute path. Default Value: Current Directory, or/directory (when chroot is enabled)

Catch_workers_output = yes
# Redirect stdout and stderr during running to the main error log file. if not set, stdout and stderr will be redirected to/dev/null according to FastCGI rules. default Value: null.

Iii. Common Errors and Solutions

1. resource problems caused by request_terminate_timeout
If the value of request_terminate_timeout is set to 0 or is too long, it may cause resource problems of file_get_contents.

If the response to the remote resource requested by file_get_contents is too slow, file_get_contents will remain stuck there and will not time out. We know that max_execution_time in php. ini can set the maximum execution time of PHP scripts. However, this parameter does not take effect in php-cgi (php-fpm. The request_terminate_timeout parameter in the php-fpm.conf configuration file is really able to control the maximum execution time of PHP scripts.

The default value of request_terminate_timeout is 0 seconds. That is to say, the PHP script will continue to be executed. In this way, when all php-cgi processes are stuck in the file_get_contents () function, the Nginx + PHP WebServer can no longer process new PHP requests, nginx returns "502 Bad Gateway" to the user ". Modify this parameter to set the maximum execution time of a PHP script. For example, if file_get_contents () is changed to 30 s, and the webpage content is slow to be obtained, this means that 150 php-cgi processes can only process 5 requests per second, webServer is also difficult to avoid "502 Bad Gateway ". The solution is to set request_terminate_timeout to 10 s or a reasonable value, or add a timeout parameter to file_get_contents.

Copy codeThe Code is as follows:
$ Ctx = stream_context_create (array (
'Http' => array (
'Timeout' => 10 // set a timeout time, in seconds
)
));

File_get_contents ($ str, 0, $ ctx );

2. If the max_requests parameter is improperly configured, an intermittent 502 error may occur:
Copy codeThe Code is as follows:
Pm. max_requests = 1000

Sets the number of service requests before each sub-process is reborn. it is very useful for third-party modules that may have memory leaks. if it is set to '0', the request is always accepted. equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. default Value: 0.
This configuration means that the process is automatically restarted when the number of requests processed by a PHP-CGI process accumulates to 500.
But why do we need to restart the process?
Generally in the project, we will use more or less some PHP third-party libraries, these third-party libraries often have memory leakage problems, if occasionally restart the PHP-CGI process, will inevitably cause memory usage continues to grow. As a result, the PHP-FPM, as the manager of the PHP-CGI, provides such a monitoring function to restart the PHP-CGI process that requests reach a specified number of times to ensure that memory usage does not grow.
It is precisely because of this mechanism that often leads to 502 errors in highly concurrent sites, I guess the reason is that the PHP-FPM did not handle the Request queue from NGINX. However, I am still using PHP 5.3.2. I don't know if this problem still exists in PHP 5.3.3.
At present, our solution is to set this value as much as possible, as much as possible to reduce the number of PHP-CGI re-SPAWN, but also can improve the overall performance. In our actual production environment, we found that the memory leakage was not obvious, so we set this value to a very large value (204800 ). You must set this value based on your actual situation and do not blindly increase it.
Back then, the purpose of this mechanism is only to ensure that the PHP-CGI is not fully occupied by memory, why not by detecting the memory to deal with it? I agree with Gao chunhui's saying that restarting the PHP-CGI process by setting the internal usage of the process's peak will be a better solution.

3. php-fpm slow logs, debugging and troubleshooting artifacts:
Request_slowlog_timeout sets a timeout parameter, and slowlog sets the storage location of slow logs.
Copy codeThe Code is as follows:
Tail-f/var/log/www. slow. log

The preceding command shows the php Execution process that is too slow.
As you can see, frequent problems such as excessive network reads and slow Mysql queries exist. You can find a clear direction to troubleshoot the problem based on the prompt.

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.