About process number management of php-fpm
This article mainly introduces the process number management of php-fpm, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
PHP-FPM
Let’s first understand some noun concepts:
CGI
is Common Gateway Interface (Common Network Management Protocol)
, used to allow interactive programs and Web The protocol for server communication. It is responsible for processing URL requests, starting a process, taking the data sent by the client as input, and the web server collects the output of the program and adds appropriate headers, and then sends it back to the client.
FastCGI
is an enhanced version of the protocol based on CGI
. Instead of creating a new process to serve requests, it uses persistent processes and created child processes to handle a series of Processes, these processes are managed by the FastCGI server, with less overhead and higher efficiency.
PHP-FPM
is an implementation of PHP
FastCGI Process Manager (FastCGI Process Manager)
, used to replace PHP FastCGI
Most of the additional features are suitable for high-load websites. Supported functions such as:
Advanced process management function for smooth stop/start
Slow logging script
Dynamic/static sub-process generation
Configuration file based on php.ini
PHP-FPM
It has been integrated into the PHP source code after 5.4, providing a better PHP process management method, which can effectively control memory and processes, and smoothly reload PHP configuration. If you need to use it, just bring the -enable-fpm
parameter when ./configure
, and use PHP-FPM
to control FastCGI
Process:
// 支持start/stop/quit/restart/reload/logrotate参数 // quit/reload是平滑终止和平滑重新加载,即等现有的服务完成 ./php-fpm --start
PHP-FPM
Configuration
PHP-FPM
The configuration file is php-fpm.conf
, In this configuration file we need to know some parameters. All the sub-processes below refer to the php-fpm
process, which can be viewed on the terminal via ps aux | grep php
.
Display
php-fpm: pool www
represents the work sub-process (actually processing the request)Display
php-fpm: process master
represents the master main process (responsible for managing the work sub-process)
Global configuration
Look firstPHP-FPM
The most important global configuration part:
emergency_restart_threshold
If the number of parameter settings is received within the emergency_restart_interval
set time SIGSEGV
or SIGBUS
exit signal, FPM
will restart. The default value is 0, which means turning off this function.
emergency_restart_interval
Set the smooth restart interval to help solve the problem of shared memory usage in the accelerator. Available units are s (default)/m/h/d
, and the default value is 0, which means off.
process.max
FPM
The maximum number of child processes that can be created, it is using multiple pm = dynamic
When configuring the php-fpm pool
process pool, control the global number of child processes. The default value is 0, which means no limit.
Process Pool Configuration
The rest of the configuration of PHP-FPM
is an area named Pool Definitions
. The configuration settings in this area are each PHP-FPM
Process pool, the process pool is a series of related sub-processes. This part starts with [process pool name]
, such as [www]
.
It can be explained that ps aux | grep php
shows php-fpm: pool www
.
#pm
##pm refers to
process manager, which specifies how the process manager controls the number of child processes. It is required and supports 3 values:
static
: Use a fixed number of child processes, specified by
pm.max_childrendynamic
: Dynamically adjust the number of child processes based on the following parameters. There must be at least one child process
-
pm.max_chidren
: The maximum number of child processes that can survive at the same time
pm.start_servers
: The number of child processes created at startup, default The value is
min_spare_servers max_spare_servers - min_spare_servers) / 2- ##pm.min_spare_servers
: The minimum number of idle child processes, if not enough, new The child processes will be automatically created
- pm.max_spare_servers
: The maximum number of idle child processes. If exceeded, some child processes will be killed
- ondemand
: The child process will not be created at startup and will only be created when a new request arrives. The following two parameters will be used:
- pm.max_children
- pm.process_idle_timeou
t The idle timeout of the child process. If no new requests can be served after the timeout, it will be killed
pm.max_requests
每一个子进程的最大请求服务数量,如果超过了这个值,该子进程会被自动重启。在解决第三方库的内存泄漏问题时,这个参数会很有用。默认值为0,指子进程可以持续不断的服务请求。
PHP-FPM
配置优化
PHP-FPM
管理的方式是一个master
主进程,多个pool
进程池,多个worker
子进程。其中每个进程池监听一个socket
套接字。具体的图示:
其中的worker
子进程实际处理连接请求,master
主进程负责管理子进程:
1. `master`进程,设置1s定时器,通过`socket`文件监听 2. 在`pm=dynamic`时,如果`idle worker`数量`pm.max_spare_servers`,杀死多余的空闲子进程 4. 在`pm=ondemand`时,如果`idle worker`空闲时间>`pm.process_idle_timeout`,杀死该空闲进程 5. 当连接到达时,检测如果`worker`数量>`pm.max_children`,打印`warning`日志,退出;如果无异常,使用`idle worker`服务,或者新建`worker`服务
保障基本安全
我们为了避免PHP-FPM
主进程由于某些糟糕的PHP代码挂掉,需要设置重启的全局配置:
; 如果在1min内有10个子进程被中断失效,重启主进程 emergency_restart_threshold = 10 emergency_restart_interval = 1m
进程数调优
每一个子进程同时只能服务一次连接,所以控制同时存在多少个进程数就很重要,如果过少会导致很多不必要的重建和销毁的开销,如果过多又会占用过多的内存,影响其他服务使用。
我们应该测试自己的PHP进程使用多少内存,一般来说刚启动时是8M左右,运行一段时间由于内存泄漏和缓存会上涨到30M左右,所以你需要根据自己的预期内存大小设定进程的数量。同时根据进程池的数量来看一个进程管理器的子进程数量限制。
测试平均PHP子进程占用的内存:
$ps auxf | grep php | grep -v grep work 26829 0.0 0.0 715976 4712 ? Ss Jul11 0:00 php-fpm: master process (./etc/php-fpm.conf) work 21889 0.0 0.0 729076 29668 ? S 03:12 0:20 \_ php-fpm: pool www work 21273 0.0 0.0 728928 31380 ? S 03:25 0:21 \_ php-fpm: pool www work 15114 0.0 0.0 728052 29084 ? S 03:40 0:19 \_ php-fpm: pool www work 17072 0.0 0.0 728800 34240 ? S 03:54 0:22 \_ php-fpm: pool www work 22763 0.0 0.0 727904 20352 ? S 11:29 0:04 \_ php-fpm: pool www work 38545 0.0 0.0 727796 19484 ? S 12:34 0:01 \_ php-fpm: pool www // 共占用的内存数量 $ps auxf | grep php | grep -v grep | grep -v master | awk '{sum+=$6} END {print sum}' 162712 // 所有的子进程数量 $ ps auxf | grep php | grep -v grep | grep -v master | wc -l 6
可以看到第6列,每一个子进程的内存占用大概在19-34M之间(单位为KB)。平均的内存占用为162712KB/6 = 27.1M
。
查看服务器总的内存大小
$ free -g total used free shared buffers cached Mem: 157 141 15 0 4 123 -/+ buffers/cache: 13 143 Swap: 0 0 0
可以看出我的服务器总得内存大小是157G(-g采用了G的单位)。
进程数限制
此时如果我们分配全部的内存给PHP-FPM
使用,那么进程数可以限制在157000/27 = 5814
,但是由于我的服务器同时服务了很多内容,所以我们可以向下调整到512个进程数:
process.max = 512 pm = dynamic pm.max_children = 512 pm.start_servers = 16 pm.min_spare_servers = 8 pm.max_spare_serveres = 30
防止内存泄漏
由于糟糕的插件和库,内存泄漏时有发生,所以我们需要对每一个子进程服务的请求数量做限制,防止无限制的内存泄漏:
pm.max_requests = 1000
重启
如果上面的配置都按照你的实际需求和环境配置好了,不要忘记重启PHP-FPM
服务。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of About process number management of php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
