Home >Backend Development >PHP Tutorial >Detailed explanation of php configuration php-fpm startup parameters and configuration_PHP tutorial

Detailed explanation of php configuration php-fpm startup parameters and configuration_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:35985browse

Agree several directories

/usr/local/php/sbin/php-fpm
/usr/local/php/etc/php-fpm.conf
/usr/local/php/etc/php.ini
1. Startup parameters of php-fpm

Copy code The code is as follows:

#Test php- fpm configuration
/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

#Close 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 setting, the default is 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 levels are: alert (required Process immediately), error (error situation), warning (warning situation), notice (general important information), debug (debugging information). Default: notice.

emergency_restart_threshold = 60
emergency_restart_interval = 60s
#Indicates that if the number of php-cgi processes with SIGSEGV or SIGBUS errors exceeds emergency_restart_threshold within the value set by emergency_restart_interval, php-fpm will restart gracefully. These two options generally remain at their default values.

process_control_timeout = 0
#Set the timeout for the child process to accept the reuse signal of the main process. Available units: s (seconds), m (minutes), h (hours), or d (days) Default Unit: s (seconds). Default value: 0.

daemonize = yes
#Execute fpm in the background, the default value is yes, if it is for debugging, it can be changed to no. In FPM, it is possible to run multiple process pools with different settings. These settings can be set individually for each process pool.

listen = 127.0.0.1:9000
#fpm listening port, which is the address processed by php in nginx. Generally, the default value is sufficient. Available formats are: 'ip:port', 'port', '/path/to/unix/socket'. Each process pool needs to be set.

listen.backlog = -1
#backlog Number, -1 means unlimited, determined by the operating system, just comment out this line. Backlog meaning reference: http://www.3gyou.cc/?p=41

listen.allowed_clients = 127.0.0.1
# Allow access to the IP of the FastCGI process, set any to not restrict IP, if To set up nginx on other hosts to also access this FPM process, the listen must be set to a local IP that can be accessed. The default value is any. Each address is separated by a comma. If 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 to access, just comment here.

user = www
group = www
# Account and group to start the process

pm = dynamic #For dedicated servers, pm can be set to static.
#How to control the child process, the options are static and dynamic. If static is selected, a fixed number of child processes is specified by pm.max_children. If dynamic is selected, it is determined by the following parameters:
pm.max_children #, the maximum number of child processes
pm.start_servers #, the number of processes at startup
pm.min_spare_servers #, to ensure the minimum number of idle processes , if the idle process is less than this value, create a new sub-process
pm.max_spare_servers # to ensure the maximum number of idle processes. If the idle process is greater than this value, clean it up

pm.max_requests = 1000
#Set the number of requests served before each child process is reborn. This is very useful for third-party modules that may have memory leaks. If set to '0', requests will always be accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value : 0.

pm.status_path = /status
#The URL of the FPM status page. If not set, the status page cannot be accessed. Default value: none. Munin monitoring will use

ping.path = /ping
#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. Please note that it must start with a slash ( /).

ping.response = pong
# Used to define the return response of the ping request. The returned text/plain format text is HTTP 200. Default value: pong.

request_terminate_timeout = 0
#Set the timeout abort time for a single request. This option may be useful for scripts where the 'max_execution_time' in the php.ini setting does not abort running scripts for some special reasons. Set to '0' to mean 'Off'. When it occurs often You can try changing this option when receiving a 502 error.

request_slowlog_timeout = 10s
#When a request is set for the timeout period, the corresponding PHP call stack information will be completely written to the slow log. Setting it to '0' means 'Off'

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

rlimit_files = 1024
#Set the rlimit limit of the file open descriptor. Default value: The system-defined value of the default openable handle is 1024, which can be viewed using ulimit -n and modified with ulimit -n 2048.

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

chroot =
#Chroot directory at startup. The defined directory needs to be an absolute path. If not set, chroot will not be used.

chdir =
#Set the startup directory, and it will be automatically Chdir to this directory at startup . The defined directory needs to be an absolute path. Default value: current directory, or / directory (when chrooting)

catch_workers_output = yes
#Redirect stdout and stderr during the running process to the main error log file. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI rules. Default value: empty.

3. Common errors and solutions

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

If the remote resource requested by file_get_contents responds too slowly, file_get_contents will always be stuck there without timing out. We know that max_execution_time in php.ini can set the maximum execution time of PHP scripts, but in php-cgi (php-fpm), this parameter will not take effect. What can really control the maximum execution time of PHP scripts is the request_terminate_timeout parameter in the php-fpm.conf configuration file.

The default value of request_terminate_timeout is 0 seconds, which means that the PHP script will continue to execute. In this way, when all php-cgi processes are stuck in the file_get_contents() function, this Nginx+PHP WebServer can no longer handle new PHP requests, and Nginx will return "502 Bad Gateway" to the user. Modifying this parameter is necessary to set the maximum execution time of a PHP script, but it only treats the symptoms rather than the root cause. For example, if it is changed to 30s, if file_get_contents() is slow to obtain web page content, this means that 150 php-cgi processes can only handle 5 requests per second, and it is also difficult for WebServer to avoid "502 Bad Gateway". The solution is to set request_terminate_timeout to 10s or a reasonable value, or add a timeout parameter to file_get_contents.

Copy code The code is as follows:

$ctx = stream_context_create(array(
'http' => array(
'timeout' => 10 //Set a timeout in seconds
)
));

file_get_contents($str, 0, $ctx);

2. Improper configuration of the max_requests parameter may cause intermittent 502 errors:
Copy code The code is as follows:

pm.max_requests = 1000

Set the number of requests served before each child process is reborn. Useful for third-party modules that may have memory leaks. If set to '0' Then it will always accept requests. It is equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.
This configuration means that when the number of requests processed by a PHP-CGI process accumulates to 500, the process will be automatically restarted.
But why restart the process?
Generally in projects, we will use some third-party libraries of PHP to some extent. These third-party libraries often have memory leak problems. If the PHP-CGI process is not restarted regularly, it will inevitably cause continuous memory usage. increase. Therefore, PHP-FPM, as the manager of PHP-CGI, provides such a monitoring function to restart the PHP-CGI process that has requested a specified number of times to ensure that the memory usage does not increase.
It is precisely because of this mechanism that 502 errors are often caused in high-concurrency sites. I guess the reason is that PHP-FPM does not handle the request queue coming from NGINX well. However, I am still using PHP 5.3.2. I don’t know if this problem still exists in PHP 5.3.3.
Our current solution is to set this value as large as possible to reduce the number of times PHP-CGI re-SPAWNs as much as possible, while also improving overall performance. In our own actual production environment, we found that the memory leak was not obvious, so we set this value very large (204800). Everyone should set this value according to their actual situation and cannot blindly increase it.
Having said that, the purpose of this mechanism is only to ensure that PHP-CGI does not occupy excessive memory. Why not deal with it by detecting memory? I very much agree with what Gao Chunhui said, restarting the PHP-CGI process by setting the peak intrinsic usage of the process would be a better solution.

3, php-fpm’s slow log, debug and exception troubleshooting artifact:
request_slowlog_timeout sets a timeout parameter, slowlog sets the storage location of the slow log

Copy the code The code is as follows:

tail -f /var/log/www.slow.log

The above command is enough See a php process that is executing too slowly.
You can see the common problems of excessive network reading and slow Mysql query. If you follow the prompt information to troubleshoot the problem, you will have a clear direction.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825014.htmlTechArticleAgree several directories/usr/local/php/sbin/php-fpm /usr/local/php/etc /php-fpm.conf /usr/local/php/etc/php.ini 1. Copy the code for the startup parameters of php-fpm as follows: #Test the php-fpm configuration...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn