Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the number of php fpm processes

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article to share with you is about how to modify the number of php fpm processes, Xiaobian think it is very practical, so share it with you to learn, I hope you can gain something after reading this article, not much to say, follow Xiaobian to see it.

How to modify the number of php fpm processes: 1. View the php log file "/usr/local/php/var/log";2. Open the "php-fpm.conf" file;3. Add parameters such as "pm.max_spare_servers" to set the maximum number of php-fpm processes.

Operating environment: Windows 7 system, PHP5.3, Dell G3 computer.

How to modify the number of PHP FPM processes?

PHP-FPM process number setting

Recently, the server is abnormal, and the website cannot be accessed normally. This is a PHP problem.

When php-fpm is restarted, it returns to normal. A minute later the fault occurred again. The requested URL/usr/local/php/var/log was not found on this server.

WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

The maximum number of child processes has been reached.

To set the number of php processes. It needs to be modified in the php-fpm.conf file.

The requested URL/etc/php-fpm/was not found on this server.

pid = run/php-fpm.pid#pid setting, default in the installation directory var/run/php-fpm.pid, it is recommended to enable error_log = log/php-fpm.log#error log, default in the installation directory var/log/php-fpm.loglog_level = notice#error level. Available levels are alert, error, warning, notice, debug. Default: notice.emergency_restart_threshold = 60emergency_restart_interval = 60s#indicates that php-fpm will restart gracefully if the number of php-cgi processes with SIGV or SIGBUS errors within the emergency_restart_interval setting exceeds emergency_restart_threshold. These two options generally remain at their default values. process_control_timeout = 0 #Sets the timeout for child processes to receive multiplexed signals from the main process. Available units: s(seconds), m(minutes), h(hours), or d(days) Default units: s(seconds). Default value: 0.daemonize = yes#background execution fpm, default value is yes, if for debugging can be changed to no. In FPM, you can 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, that is, the address handled by php in nginx, the default value is generally OK. Available formats are: 'ip: port',' port','/path/to/unix/socket'. Each process pool needs to be set.listen.backlog = -1#backlog number,-1 means no limit, determined by the operating system, this line is commented out. Backlog meaning reference: www.3gyou.cc/? p= 41liste.allowed_clients = 127.0.0.1#Allow access to FastCGI process IP, set any to unrestricted IP, if you want to set nginx of other hosts can also access this FPM process, listen to set the cost can be accessed IP. The default value is any. Each address is separated by commas. If it is not set or empty, it allows any server to request to connect to listen.owner = wwwlisten.group = wwwlisten.mode = 0666#unix socket setting option. If it is accessed by tcp mode, comment here. user = wwwgroup = www#accounts and groups that start processes pm = dynamic #pm can be set to static for dedicated servers.# How to control child processes, 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 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, a new child process pm.max_spare_servers #is created, to ensure the maximum number of idle processes. If the idle process is greater than this value, this is cleaned up pm.max_requests = 1000#Set the number of service requests before each child process is reborn. This is useful for third party modules that may have memory leaks. If set to '0' always accept requests. Equivalent to PHP_FCGI_MAX_REQUESTS environment variable. Default: 0.pm.status_path = /status#URL of FPM status page. If it is not set, the status page cannot be accessed. Default: none. munin monitoring uses ping URLs to ping.path = /ping#FPM monitoring pages. If it is not set, you cannot access the ping page. This page is used to externally check if FPM is alive and available to respond to requests. Note that it must begin with a slash (/). ping.response = pong#used to define the ping request return corresponding. Returns text in HTTP 200 format. Default: pong.request_terminate_timeout = 0#Sets timeout for individual requests. This option may be useful for scripts whose 'max_execution_time' setting in php.ini does not abort for some special reason. Set to '0' means 'Off'. Try changing this option when 502 errors occur frequently. request_slowlog_timeout = 10s #When a request for this timeout setting is completed, the corresponding PHP call stack information is written to the slowlog. Set to '0' for 'Off'slowlog = log/$pool.log.slow#Slow request log, with request_slowlog_timeout using rlimit_files = 1024#to set the rlimit for file open descriptors. Default value: The default openable handle is 1024, which can be viewed by ulimit -n and modified by ulimit -n 2048. rlimit_core = 0 #Sets the maximum limit for core rlimit. Available values: 'unlimited', 0 or positive integer. Default values: System-defined values.chroot =#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, which is automatically Chdir to at boot. The directory defined needs to be an absolute path. Default: current directory, or/directory (chroot) catch_workers_output = yes#Redirect stdout and stderr in progress to the main error log file. If not set, stdout and stderr will be redirected to/dev/null according to FastCGI rules. Default: null.

According to the analysis of the above configuration, add the following configuration to the php-fpm.conf file:

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

1. Introduction to php-fpm optimization parameters

They are pm, pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.

pm: indicates which way to use, there are two values to choose from, static or dynamic.

In older versions, dynamic was called apache-like. This should be noted in the description of the configuration file.

The following four parameters mean:

pm.max_children: Number of php-fpm processes opened statically

pm.start_servers: Number of php-fpm processes starting in dynamic mode

pm.min_spare_servers: Minimum number of php-fpm processes in dynamic mode

pm.max_spare_servers: Maximum number of php-fpm processes in dynamic mode

Difference:

If dm is set to static, then only pm.max_children will actually take effect. The system starts the set number of php-fpm processes.

If dm is set to dynamic, then the pm.max_children parameter is disabled and the next three parameters take effect.

The system will start pm.start_servers php-fpm processes at the start of php-fpm,

The php-fpm process count is then dynamically adjusted between pm.min_spare_servers and pm.max_spare_servers according to system requirements

2. Server specific configuration

Which execution method is better for our server? In fact, just like Apache, PHP programs run more or less with memory leaks after they complete execution.

This is why a php-fpm process starts out with only about 3 MB of memory and grows to 20-30 MB after a while.

For servers with large memory (say 8 gigabytes or more), specifying static max_children is actually more appropriate because it does not require additional process count control and improves efficiency.

Because php-fpm processes are often switched on and off with delays, static effects are better when memory is large enough. The quantity can also be obtained according to memory/30M, for example, 8GB memory can be set to 100,

Then php-fpm memory consumption can be controlled in 2G-3G appearance. If the memory is slightly smaller, say 1 gigabyte, then specifying a static number of processes is more conducive to server stability.

This ensures that php-fpm only gets enough memory, and allocates a small amount of memory to other applications to use, which will make the system run more smoothly.

For servers with small memory, such as VPS with 256M memory, even if calculated according to a 20M memory amount, 10 php-cgi processes will consume 200M memory, and the system crash should be very normal.

Therefore, you should try to control the number of php-fpm processes as much as possible. After roughly knowing the memory occupied by other applications, you can specify a static small number for it to make the system more stable. Or in a dynamic way,

Dynamic mode is recommended for use on servers with less memory or VPS because it can recycle and free up some memory by ending redundant processes. The maximum number is based on memory/20 MB.

For example, for 512M VPS, it is recommended that pm.max_spare_servers be set to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server. For example, if only php environment is deployed on the server, the appropriate value is between 5 and 10.

undefined

This server configuration

1. Basic server information:

Hard disk: data disk 30G, system disk 20G

Memory: 1.5G

CPU: Dual Core

System: CentOS 6.3 64 bit

Bandwidth: Exclusive 2M

2. Deployed applications

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

3. Optimized parameters

pm = dynamicpm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers = 8 pm.max_requests = 500

Sets the number of requests served before each child process is reborn. This is useful for third party modules that may have memory leaks. If set to '0' always accept requests. Equivalent to PHP_FCGI_MAX_REQUESTS environment variable. Default: 0.

This configuration means that when a PHP-CGI process accumulates 500 requests, it automatically restarts the process.

But why restart the process?

In general, in the project, we will use some PHP third-party libraries more or less, these third-party libraries often have memory leakage problems, if the PHP-CGI process is not restarted regularly, it will inevitably cause memory usage to grow. Therefore, PHP-FPM, as the manager of PHP-CGI, provides such a monitoring function to restart the PHP-CGI process with a specified number of requests to ensure that memory usage does not increase.

Because of this mechanism, 502 errors are frequently encountered in high concurrency sites, and I suspect that PHP-FPM is not handling the request queue from NGINX properly. However, I am currently using PHP 5.3.2, I do not know if this problem still exists in PHP 5.3.3.

Our solution at this point is to set this value as high as possible to minimize the number of PHP-CGI re-SPAWN while improving overall performance. In our own actual production environment, memory leaks are not obvious, so we set this value very high (204800). Everyone should set this value according to their actual situation, not blindly increase it.

The above is how to modify the number of php fpm processes, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report