


Setting the maximum number of PHP connections and adjusting php-fpm high concurrency parameters
推荐:《PHP视频教程》
服务器中找到php-fpm.conf配置(有的会在引入的www.conf中)
[global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice [www] listen = /tmp/php-cgi.sock listen.backlog = -1 listen.allowed_clients = 127.0.0.1 listen.owner = www listen.group = www listen.mode = 0666 user = www group = www pm = static pm.max_children = 200 pm.start_servers = 40 pm.min_spare_servers = 10 pm.max_spare_servers = 20 pm.max_requests=1000 request_terminate_timeout = 100 request_slowlog_timeout = 0 slowlog = var/log/slow.log
---------------------------------------------------------------------------
pm.max_children=30 pm.max_requests=500 pm.start_servers=4 pm.max_spare_servers=30
一. pm= static
首先说一下pm这个值 pm = dynamic 这个是php的进程数是动态的 会根据访问量来确定来回增加
而在高负载的php环境下我推荐设置 pm= static php-fpm进程数固定
二. pm.max_children=???
当用静态模式下 进程数确定根据 pm.max_children来进进行确定 那么问题来了我的服务器应该设定多少php-fpm呢 ?
从理论的角度上说php-fpm进程数越多越好,相当于一个酒店有很多个充足的服务员来为你服务肯定会比较爽啊 ,你也不需要等待。
但是。。。。现实上总是残酷的 php-fpm的进程数会受到你的内存大小的限制。一般情况下我们 进程数 =用机器内存(M)除以2 再除以20(M);
当然这个也不是绝对的 你需要知道:
1、你可以分配给php多大内存 :你的服务器上是不是单纯的php服务器 有没有比较耗费内存的其他程序(mysql)。
2、你的每个php-fpm内存占多大 :内存占用多大要根据你的php代码质量和处理的相关业务。当然你可以用命令去统计你的php-fpm平均占用内存大小。
有人会问我如果设置不恰当会有什么状况出现呢?
当数值偏小时请求到nginx会无法分配到php-fpm进程 导致502错误
当数值偏大如果没有大访问量还好 如果访问量较大的话 内存都会被php占光了。导致系统响应缓慢 cpu-system 升高 系统不断的去调整内存分配
严重时会导致较高的 cup-wait 较高 内存不够用了 直接写磁盘 磁盘io直线增加 。cpu使用率也开始爆满。(如图所示)
三.request_terminate_timeout
计算方式如下:如果你的服务器性能足够好,且宽带资源足够充足,PHP脚本没有循环或BUG的话你可以直接将”request_terminate_timeout”设 置成0s。0s的含义是让PHP-CGI一直执行下去而没有时间限制。
而如果你做不到这一点,也就是说你的PHP-CGI可能出现某个BUG,或者你的宽带不够充足或者其他的原因导致你的PHP-CGI能够假死那么就建议你给”request_terminate_timeout”赋一个值,这个值可以根 据你服务器的性能进行设定。
一般来说性能越好你可以设置越高,20分钟-30分钟都可以。由于我的服务器PHP脚本需要长时间运行,有的可能会超过10分钟因此我设置了900秒,这样不会导致PHP-CGI死掉而出现502 Bad gateway这个错误。
四.pm.max_requests
这个参数的含义是php-fpm工作进程处理完多少请求后自动重启,主要目的就是为了控制请求处理过程中的内存溢出,使得内存占用在一个可接受的范围内。比较适用于服务器搭载项目比较杂乱,有点请求会比较占用内存
导致php-fpm占用比较大。在经过一定次数请求后会结束掉进程,释放自己的内存。如果这个值太小就会导致所有的工作进程几乎同时达到这个值并且进入需要重启的状态,当所有的工作进程都在同一时刻重启就会发生在
数秒内甚至更长的时间PHP将停止响应直到所有的进程均重启完为止。这是不能接受的,所以我一般会把这个值设置为PHP启动后第一批工作进程达到此值需要重启时,第一个进程重启与最后一个进程重启之间的时间相差
1分钟以上,一般在压力比较大的晚上这个差值将会扩大到5分钟左右,此时对进程重启对服务器的负面影响就可以忽略了。
下面补充几个命令统计相关php-fpm 相关数据
1、查看php-fpm的进程个数
ps -ef |grep "php-fpm"|grep "pool"|wc -l
2、查看每个php-fpm占用的内存大小
ps -ylC php-fpm --sort:rss
3.查看PHP-FPM在你的机器上的平均内存占用
ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
4.查看单个php-fpm进程消耗内存的明细
pmap $(pgrep php-fpm) | less
重启php-fpm
1. 停止命令 pkill php-fpm 2.重启或启动命令 php-fpm -R 或 /alidata/server/php/sbin/php-fpm
The above is the detailed content of Setting the maximum number of PHP connections and adjusting php-fpm high concurrency parameters. 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

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

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

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,

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

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

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.
