Home Backend Development PHP Tutorial Understand the PHP process manager php-fpm

Understand the PHP process manager php-fpm

Jul 21, 2020 pm 05:09 PM
php php-fpm

Understand the PHP process manager php-fpm

What is php-fpm

php-fpm is a process manager for PHP. Many work processes under php are managed by the php-fpm process manager.

Related learning recommendations: PHP programming from entry to proficiency

The working principle of php-fpm

The full name of php-fpm is PHP FastCGI process manager. After php-fpm is started, it will first read php.ini, and then read the corresponding conf configuration file. The conf configuration can override the configuration of php.ini.
After starting php-fpm, a master process will be created to listen to port 9000 (configurable). The master process will create several sub-processes based on fpm.conf/www.conf. The sub-processes are used to handle actual business.
When a client (such as nginx) connects to port 9000, the idle child process will accept it by itself. If all child processes are busy, the new connection to be accepted will be put into the queue by the master and wait for fpm The child process is idle; the length of the queue that stores semi-connections waiting to be accepted is configured by listen.backlog.

How to view the php-fpm process and child processes

View all processes related to php-fpm. As shown in the picture below

here pool www are all sub-processes of php-fpm, which is what we often call the work process.

View the sub-processes under php-fpm

Through the above command, we can actually see the processes related to php-fpm. If we need to be more intuitive To view the master process and work process of php-fpm, you can view the process in the following way.
The 5370 here is the master process number of php-fpm. It can be clearly seen through the above command.

Through the above command, you can see that php-fpm is the master process and has 15 sub-processes below. The number of child processes here can be customized by the process. The process configuration is through the following parameters:

pm = dynamic # 动态创建子进程
pm.max_children = 20 # 最大子进程数
pm.start_servers = 15 # 初始化php-fpm进程时,默认的子进程数
Copy after login

php-fpm parameter configuration instructions

php-fpm global configuration parameters

#php-fpm的运行权限。
#以什么用户什么组的权限来运行池fpm。
user = www
group = www

#php-fpm的运行方式,可以使端口,也可以使socke文件。
#如果是端口则是走tcp,如果是socket则直接读socket文件,这样性能相对更好。
listen = 127.0.0.1:9000 

#拥有socket权限的用户,需要和上面的user、group配置相结合。
#如果采用的端口的方式,则不配置。
listen.owner = www
listen.group = www
listen.mode = 0660

#这是php-fpm端口连接的地址。多个用","隔开。默认任意地址都可以连接。
#例如Nginx和php-fpm不在同一台服务器上,这里的值就是Nginx服务的ip地址。
#当Nginx和php-fpm配置在同一台服务器上,则直接写127.0.0.1即可。
listen.allowed_clients = 127.0.0.1 

#pid进程文件存放的位置,当我们启用一个php服务,
#则会自动创建一个该pid文件,其实我们可以直接把该文件理解理解php-fpm的进程号文件,
#两则是等价的。默认为none。
pid = /opt/remi/php72/root/var/run/php-fpm/php-fpm.pid 

#错误日志位置,默认:安装路径 #INSTALL_PREFIX#/log/php-fpm.log。
#如果设置为syslog,log就会发送给syslogd服务而不会写进文件里。
error_log = /opt/remi/php72/root/var/log/php-fpm/error.log 

#PHP限制的文件扩展名
security.limit_extensions = .php .php3 .php4 .php5 .php7

#系统日志标示,如果跑了多个fpm进程,需要用这个来区分日志是谁的。
syslog.ident = php-fpm 

#日记登记,可选:alert, error, warning, notice, debug。
log_level = notice 

#紧急重启阈值,需要与下面emergency_restart_interval参数一起配置。
emergency_restart_threshold = 60 

# 紧急重启阈值的时间范围。在此参数设置的时间内,
# 出现SIGSEGV或SIGBUS的子进程数超过emergency_restart_threshold参数设置的值。
# 那么fpm就会优雅的重启,值是0表示off这个功能,可用的单位有:s秒,m分,h时,d天。
emergency_restart_interval = 60s 

#设置子进程接受主进程复用信号的超时时间。
process_control_timeout = 0 

#当动态管理子进程时,fpm最多能fork多少个进程,0表示无限制,
# 这是所有进程池能启动子进程的总和,谨慎使用。
process.max = 128 

#设置子进程的优先级,在master进程以root用户启动时有效;
#如果没有设置,子进程会继承master进程的优先级,值范围-19(最高)到20(最低),默认不设置。
process.priority = -19 

#设置成no用于调试bug,默认为yes。
daemonize = yes 

#master进程最多能打开的文件数量。默认采用系统设置的值。
rlimit_files = 1024 

#master进程核心rlimit限制值;可选unlimited或>=0的整数,默认为系统的值。
rlimit_core = 0

#事件处理机制,默认自动检测,可选值:select,poll,
#epoll(linux>=2.5.44),kqueue,/dev/poll,port
events.mechanism = epoll 

#fpm想系统发送状态的频率。单位有s,m,h。
#前提是fpm被设置会系统服务。
systemd_interval = 10s
Copy after login

php-fpm's process process pool configuration

#php-fpm的队列长度。
listen.backlog = 65535 

#php进程池权限,同样要master进程是root用户才有效,
#和上面的全局设置一样,不设置的话会继承master进程的优先级。
process.priority = -19 

#子进程管理方式
#static(静态配置,在启动php-fpm时根据该值创建固定的子进程数量);
#dynamic(动态配置,在启动php-fpm时根据pm.start_servers的值初始化对应的子进程数,至少一个子进程);
#ondemand(按需配置,在启动php-fpm时不创建子进程,而是根据请求动态fork子进程);
pm = dynamic 

#最大子进程数量
pm.max_children = 5 

#初始化子进程数量,与上面的pm = dynamic配置使用。
pm.start_servers = 2 

#服务器闲置时最少保持2个子进程,不够这个数就会创建,只适用动态dynamic管理方式
pm.min_spare_servers = 2 

#服务器闲置时最多要有几个,多了会kill,只适用动态dynamic管理方式
pm.max_spare_servers = 3 

#子进程闲置时间,也就是说子进程没有可处理的任务时,在该之间使就会被killed。
pm.process_idle_timeout = 10s

#每个子进程最大的处理请求数量。在一定程度上可以防止内存泄漏。
pm.max_requests = 500 

#php-fpm状态监控的uri
pm.status_path string

#php-fpm监控页面的 ping 网址。
#如果没有设置,则无法访问 ping 页面。
#该页面用于外部检测php-fpm是否存活并且可以响应请求。请注意必须以斜线开头(/)。
ping.path string

#用于定义ping请求的返回响应。返回为 HTTP 200 的 text/plain 格式文本。默认值:pong。
ping.response string

#设置worker的nice(2)优先级(如果设置了的话)。
#该值从 -19(最高优先级) 到 20(更低优先级)。 
#默认值:不设置
process.priority int

#检测路径时使用的前缀
prefix string

#访问文件日志,没啥用处,比如yii2每次都记录访问index.php,只是记录真实的PHP文件。
access.log = var/log/$pool.access.log 

#php的慢日志
slowlog = var/log/$pool.log.slow 

#慢日志时间阈值
request_slowlog_timeout = 2s 

#单个请求的超时时间,当php.ini设置的最大执行时间未生效,则交由它来处理。
request_terminate_timeout = 3s 

#最大打开句柄数,默认为系统值。
rlimit_files = 1024 

#最多的核心使用数,默认为系统分配。
rlimit_core = 0
Copy after login

Partial configuration demonstration

php-fpm's backlog size setting

The backlog size setting of php-fpm is related to the processing capability of php-fpm, not bigger is better.

When the value is set too large, php-fpm cannot handle it, and nginx will wait for a timeout, disconnect, and report a 504 gateway timeout error. At the same time, when php-fpm finished processing and prepared the write data for nginx, it found that the TCP connection was disconnected and reported "Broken pipe".

When this value is set too small, client requests such as nginx cannot enter the accept queue of php-fpm at all, and a "502 Bad Gateway" error is reported. Therefore, the size of the backlog must be determined based on the QPS of php-fpm. The best calculation method is QPS=backlog.

php-fpm startup mode

php-fpm starts by socket or port. These two methods are configured according to the actual situation.

nginx and php-fpm are on the same server. At this time, unix socket inter-process communication can be used directly instead of tcp port communication, which can save the time of creating a connection and thereby improve performance. The sock file can be created anywhere, as long as fpm has the permission to write the file in that directory and nginx has the permission to read it. The tcp connection will be more stable because the TCP protocol ensures the correctness of the data, but sock has fewer data copies and context switches, and consumes less resources. However, socket can only be used when nginx and fpm are on the same machine.

How to choose socket startup or port startup.

Because the tcp method has higher concurrency than the unix method, it is recommended to use the tcp method for projects with high concurrency. Now the Nginx configuration sample file defaults to the tcp method.

Using the unix method, the optimization point is to place the socket file under the /dev/shm directory. The general meaning is that the files under the directory are not stored on the hard disk, but are stored on the hard disk. in memory. As for hard disk reading and memory reading, who is faster and who is slower? Memory is definitely the fastest.

How to view the socket file when starting in socket mode.

The socket file is based on the pid configuration item mentioned above. We can directly use the cat command to view the process number.

The default number of child processes to start. Through the above pm = dynamic configuration, we know that this method dynamically configures the size of the child process. At the same time, we can also set the default number of child processes. Number of processes.

pm = dynamic
pm.max_children = 20
### 默认15个子进程,演示的效果就是上面的shell命令的结果图。
pm.start_servers = 15
Copy after login

When we try to set it to 3, the following error message is displayed.

Explanation, there is a certain relationship between the start_servers configuration item here and the min_spare_servers configuration. We set it to a minimum of 10, and php-fpm can be started normally.

The above is the detailed content of Understand the PHP process manager php-fpm. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles