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 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

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

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

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,

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

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

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 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.
