In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to configure php-fpm startup parameters for php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to configure php-fpm startup parameters for php".
Agree on several directories
/ usr/local/php/sbin/php-fpm
/ usr/local/php/etc/php-fpm.conf
/ usr/local/php/etc/php.ini
First, the startup parameters of php-fpm
The copy 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
# turn off 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`
Second, the detailed explanation of the important parameters of php-fpm.conf
The copy code is as follows:
Pid = run/php-fpm.pid
# pid setting. Default is the var/run/php-fpm.pid in the installation directory. It is recommended to enable it.
Error_log = log/php-fpm.log
# error log, default is the var/log/php-fpm.log in the installation directory
Log_level = notice
# error level. Available levels are: alert (must be dealt with immediately), error (error condition), warning (warning condition), 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 within the value set by emergency_restart_interval exceeds emergency_restart_threshold, 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 main process multiplexing signal. Available units: s (seconds), m (minutes), h (hours), or d (days) default units: s (seconds). Default value: 0.
Daemonize = yes
# fpm is executed in the background. The default value is yes, which can be changed to no for debugging. In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool.
Listen = 127.0.0.1 9000
# fpm listening port, that is, the address processed by php in nginx. The default value is generally fine. The available formats are: 'ip:port',' port','/ path/to/unix/socket'. Each process pool needs to be set up.
Listen.backlog =-1
The number of # backlog.-1 means unlimited, which is 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, and set the any to unrestricted IP. If you want to set the nginx of other hosts to access this FPM process, you need to set a cost-accessible IP at the listen. The default value is any. Each address is separated by a comma. 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, you can simply 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 child processes, the options are static and dynamic. If static is selected, pm.max_children specifies a fixed number of child processes. If dynamic is selected, it is determined by the following on parameter:
Pm.max_children #, maximum number of child processes
Pm.start_servers #, number of processes at startup
Pm.min_spare_servers # to keep the number of idle processes to a minimum, and if the idle processes are less than this value, create a new child process
Pm.max_spare_servers #, which guarantees the maximum number of idle processes. If the idle processes are greater than this value, clean up.
Pm.max_requests = 1000
# set the number of requests for services before each child process is reborn. It is very useful for third-party modules that may have memory leaks. If set to'0', the request will be accepted all the time. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.
Pm.status_path = / status
# URL of the FPM status page. 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 it is not set, the ping page cannot be accessed. This page is used to externally detect whether the FPM is alive and can respond to requests. Please note that it must begin with a slash (/).
Ping.response = pong
# used to define the response of a ping request. Returns text in text/plain format as 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 in the php.ini setting where 'max_execution_time' does not stop running for some special reason. Set to'0' means' Off'. You can try to change this option when 502 errors occur frequently.
Request_slowlog_timeout = 10s
When a timeout for this setting is requested, the corresponding PHP call stack information is fully written to the slow log. Set to'0' means' Off'
Slowlog = log/$pool.log.slow
# logging of slow requests for use with request_slowlog_timeout
Rlimit_files = 1024
# sets the rlimit limit for file opening descriptors. Default: the system-defined value defaults to open handle 1024, which can be viewed using ulimit-n and modified by ulimit-n 2048.
Rlimit_core = 0
# set the maximum limit 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 it is not set, chroot is not used.
Chdir =
# set the startup directory, which will be automatically Chdir at startup. The defined directory needs to be an absolute path. Default value: current directory, or / directory (on chroot)
Catch_workers_output = yes
# redirect the stdout and stderr during the run to the main error log file. If not set, stdout and stderr will be redirected to / dev/null according to the rules of FastCGI. Default value: empty.
Third, common mistakes and solutions
1. Resource problems caused by request_terminate_timeout
If the value of request_terminate_timeout is set to 0 or for too long, it may cause resource problems for file_get_contents.
If the remote resource requested by file_get_contents is too slow, the file_get_contents will stay 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, but in php-cgi (php-fpm), this parameter does not work. What really controls the maximum execution time of the PHP script is the request_terminate_timeout parameter in the php-fpm.conf configuration file.
The default value for request_terminate_timeout is 0 seconds, which means that the PHP script will be executed forever. In this way, when all php-cgi processes are stuck in the file_get_contents () function, the WebServer of this Nginx+PHP can no longer handle new PHP requests, and Nginx will return "502 Bad Gateway" to the user. It is necessary to modify this parameter to set the maximum execution time of a PHP script, but it does not cure the symptoms. For example, if it is changed to 30s, if file_get_contents () is slow to get web content, this means that there are 150 php-cgi processes that can only process 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.
The copy 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 parameter may cause an intermittent 502 error:
The copy code is as follows:
Pm.max_requests = 1000
Sets the number of requests for services before each child process is reborn. It is very useful for third-party modules that may have memory leaks. If set to'0', requests are always accepted. 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 an PHP-CGI process accumulates to 500, the process is automatically restarted.
But why restart the process?
Generally, in a project, we will use some PHP third-party libraries more or less. These third-party libraries often have memory leaks. If the PHP-CGI process is not restarted periodically, the memory usage is bound to increase. Therefore, PHP-FPM, as the manager of PHP-CGI, provides such a monitoring function to restart PHP-CGI processes whose requests reach a specified number of times to ensure that memory usage does not increase.
Because of this mechanism, 502 errors are often caused in highly concurrent sites. I guess the reason is that PHP-FPM does not properly handle the request queue from NGINX. However, I am currently using PHP 5.3.2, and I don't know if this problem still exists in PHP 5.3.3.
At present, our solution is to set this value as high as possible to minimize the number of PHP-CGI re-SPAWN, 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 to be very large (204800). Everyone should set this value according to their own actual situation and should not increase it blindly.
Then again, the purpose of this mechanism is only to ensure that PHP-CGI can only take up memory separately, so why not deal with it by detecting memory? I very much agree with Gao Chunhui that it would be a better solution to restart the PHP-CGI process by setting the peak intrinsic usage of the process.
3The slow log, debug and exception troubleshooting artifact of phpmurfpm:
Request_slowlog_timeout sets a timeout parameter, and slowlog sets the location of slow logs.
The copy code is as follows:
Tail-f / var/log/www.slow.log
The command above shows the slow execution of the php process.
You can see the frequent problems of excessive network reading and slow Mysql query, so there is a very clear direction to troubleshoot the problem according to the prompt information.
At this point, I believe you have a deeper understanding of "how to configure php-fpm startup parameters in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.