Recently, the server is abnormal, the website can not be accessed normally.

After investigation is a PHP problem.

When you restart phP-fPM, it returns to normal. A minute later it broke down again. Check the PHP log file /usr/local/php/var/logWARNING: [pool WWW] server reached pm.max_children setting (5), consider raising it number of child processes reaches the maximum value. The number of PHP processes to set. You need to modify it in the php-fpm.conf file. Look at the/usr /localPid = run/ PHP -fpm.pid = run/ PHP -fpm.pid = run/ PHP -fpm.pid = run/ PHP -fpm.pidPid: var/run/php-fpm. Pid: var/run/php-fpm. Pid: var/run/php-fpm
 
error_log = log/php-fpm.log
Error log, default installation directory var/ log/php-pm.log
 
log_level = notice
# Error level: Alert, error, Warning, notice, debug Default: notice.
 
emergency_restart_threshold = 60
emergency_restart_interval = 60s
# Indicates that phP-FPM gracefully restarts phP-FPM if the number of PHP-CGI processes with SIGSEGV or SIGBUS errors within the value set by emergency_REstart_interval exceeds emergency_restart_threshold. The default values for these two options are generally kept.
 
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 unit: s(seconds). Default value: 0.
 
daemonize = yes
The default value is yes, and can be changed to no for debugging purposes. In FPM, you can run multiple process pools using different Settings. These Settings can be set individually for each process pool.Listen = 127.0.0.1:9000# FPM listener port, which is the address handled by PHP in nginx. Available format is: 'IP: port', 'port', '/ path/to/Unix/socket. Each process pool needs to be set.
 
listen.backlog = -1
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Backlog meaning reference:http://www.3gyou.cc/?p=41 listen. Allowed_clients = 127.0.0.1Set any to unrestricted IP. If you want to set nginx on other hosts to be able to access this FPM process, listen to set the cost of the accessible IP. The default value is any. Each address is separated by commas. If not set or empty, any server is allowed to request a connection
 
listen.owner = www
listen.group = www
listen.mode = 0666
The Unix socket is set to TCP.
 
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? Options include 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 underturn argument:
pm.max_children #, the maximum number of child processes
pm.start_servers #, the number of processes at startup
pm.min_spare_servers If the number of idle processes is smaller than this value, create a new child process
pm.max_spare_servers #, ensure the maximum number of idle processes. If the number of idle processes is greater than this value, it will be cleaned up
 
pm.max_requests = 1000
Set the number of requests to be served by each child process before it spawns. This is useful for third-party modules that may have memory leaks. If set to '0', requests are 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 will be used by monitoring
 
ping.path = /ping
#FPM monitor page ping url. If the ping page is not set, you cannot access the ping page. This page is used externally to check whether the FPM is alive and ready to respond to requests. Note that it must begin with a slash (/).
 
ping.response = pong
Used to define the response to a ping request. Return HTTP 200 text/plain text. Default value: pong.
 
request_terminate_timeout = 0
Set timeout timeout for a single request. This option may be useful if the 'max_execution_time' in the php.ini setting is not aborted for some special reason. Setting it to '0' means' Off '. Try changing this option when 502 errors occur frequently.
 
request_slowlog_timeout = 10s
When a request is made for this timeout, the corresponding PHP call stack information is written to the slow log in its entirety. Set it to '0' for 'Off'
 
slowlog = log/$pool.log.slow
Slow slowlog_timeout (request_slowlog_timeout
 
rlimit_files = 1024
# set the rlimit limit for the file open descriptor. Default value: system defined Value The default value is 1024. You can run the ulimit -n command to view and ulimit -n 2048 command to change the value.
 
rlimit_core = 0
# Set core rlimit maximum value. Available value: 'unlimited', 0, or a positive integer. Default value: system-defined value.
 
chroot =
The Chroot directory at startup. The directory defined needs to be an absolute path. If it is not set, chroot is not used.
 
chdir =
# set the boot directory, automatically Chdir to this directory during startup. The directory defined needs to be an absolute path. Default: current directory, or/directory (chroot)
 
catch_workers_output = yes
Redirect runtime stdout and stderr to the main error log file. If not, stdout and stderr will be redirected to /dev/null according to FastCGI rules. Default value: null.Copy the code

Add the following configuration to the php-fpm.conf file based on the above configuration:

pm.max_children = 100
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 100
pm.max_requests = 500Copy the code

1. Php-fpm optimization parameters introduction

They are: PM, Pm.max_children, Pm.start_Servers, Pm.min_spare_Servers, and Pm.max_spare_Servers.

PM: Indicates which method to use. There are two values to choose from: static or dynamic. In older versions, Dynamic is called apache-like. Pay attention to the description of the configuration file.

The meanings of the following four parameters are:

Pm. max_children: number of php-fpm processes started in static mode Pm. start_servers: number of php-FPM processes started in dynamic mode pm.min_spare_servers: Pm. max_spare_Servers: Specifies the maximum number of php-FPM processes in dynamic modeCopy the code

The difference between:

If dm is set to static, only pm.max_children is valid. The system will start the php-fpm process to set the number. If dm is set to dynamic, the pm.max_children parameter is invalid and the last three parameters take effect. The system starts the pm. start_SERVERS phP-fPM process at the beginning of the phP-FPM run, and then dynamically adjusts the number of phP-fPM processes between Pm. min_spare_Servers and PM. max_spare_Servers based on system requirements

2. Specific server configuration

What is the best execution for our server? In fact, just like Apache, running PHP programs have more or less memory leaks after execution. This is why a PHP-FPM process starts out with around 3M of memory, rising to 20-30m after running for a while.

For servers with large memory, such as 8GB or more, specifying static max_children is actually more appropriate because it does not require additional process number control and increases efficiency.

Since the phP-FPM process also has a time lag when it is switched on and off frequently, it is better to turn it on static with enough memory. The amount of memory can also be calculated according to the memory /30M, for example, 8GB memory can be set to 100, so that the memory consumption of PHP-FPM can be controlled between 2G-3G. If the memory is a little bit smaller, say 1 GB, specifying a static number of processes is better for server stability.

This ensures that phP-FPM only gets enough memory and allocates the remaining memory to other applications, making the system run more smoothly. For a server with small memory, such as a 256-megabyte VPS, even with a 20 Megabyte memory, 10 PHp-CGI processes will consume 200 megabytes of memory, and the system should crash quite normally.

Therefore, the number of phP-fpm processes should be controlled as much as possible. Once you have a rough idea of how much memory other applications are using, giving it a small, static number will make the system more stable. Or use dynamic mode, because dynamic mode will end the redundant process, can reclaim some memory, so it is recommended to use it on the server or VPS with less memory. The specific maximum number is based on memory /20M.

For example, for 512MB VPS, it is recommended that pm.max_spare_servers be set to 20. For pm.min_spare_Servers, it is recommended that the value be set based on the load of the server, for example, if only the PHP environment is deployed on the server, the appropriate value is between 5 and 10.

Local Server Configuration

1. Basic server information:

Hard disk: 30 GB data disk and 20 GB system disk

Memory: 1.5 G

CPU: dual –

System: CentOS 6.3 64-bit

Bandwidth: 2 mbit/s

2. Deployed applications

Git, SVN, Apache, Tomcat, PHP, Nginx, Mysql, JDK

3. Optimized parameters

pm = dynamic
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.max_requests = 500Copy the code

Set the number of requests to service each child process before respawning. This is useful for third-party modules that may have memory leaks. If set to ‘0’, requests are always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.

This configuration means that a PHp-CGI process restarts automatically when the number of requests it has processed reaches 500.

But why restart the process?

In our projects, we often use PHP third-party libraries. These libraries often have memory leaks, and if you do not restart the PHp-CGI process on a regular basis, memory usage will increase.

Therefore, php-fpm, as the php-CGI manager, provides a monitoring function to restart the php-CGI process when the number of requests reaches a specified number, ensuring that the memory usage does not increase.

Because of this mechanism, in highly concurrent sites, error 502 is often caused, I suspect, by phP-FPM’s poor handling of the request queue from NGINX. However, since the old project used PHP 5.3.2, I don’t know if this problem still exists in PHP 5.3.3.

Our current solution is to make this value as large as possible to minimize the number of PHP-CGI respawn and improve overall performance. In our own actual production environment, memory leaks are not obvious, so we set this value very large (204800). You should set this value according to your actual situation, not blindly increase.