文章大纲
博客在今天凌晨 0:43 再一次 502 了,为什么是再一次?因为这种情况出现了几次。虽然知道是 php-fpm
服务被 OOM 了,但没有考虑太多,直接 systemctl restart php-fpm
了事,恰逢是周六,花点时间来研究一下为什么默认配置情况下出现多次的 OOM。
首先不得不承认 OOM 出现的原因就是内存不足,解决 OOM 的办法就是增加内存,但当前使用的 VPS 拥有 2GB 的内存,这对于运行一个访问量非常低(几乎没有)的博客来说是很大的了,更别说最早还用过 32MB 的 VPS 运行 Typecho 呢。同时后续打算迁移到更具性价比的 512MB 内存的 VPS 上,毕竟没有什么负载,所以我这次不得不面对这个问题。
PHP-FPM
首先需要搞清楚运行的 php-fpm.service
是个什么东西。
PHP-FPM 全称是 PHP FastCGI 进程管理器,是用于管理 PHP 进程池的软件。
Web 服务器(Apache/Nginx)是无法解析 PHP 脚本的,所以当请求过来时需要借助 php-fastcgi 的 CGI 程序进行解析,但是 php-fastcgi 只会返回结果,不会管理,所以才有了 php-fpm
。
以前的 CGI 程序每当收到 Web 服务器的请求后,会读取 PHP 的配置,初始化环境,然后解析 PHP 脚本并返回结果,最后退出程序。每一个请求过来都需要重复这个操作,效率比较低。因此 php-fastcgi 带来了改变。
php-fastcgi 启动时会读取一次 PHP 配置文件,然后产生一个 master 进程和多个 worker 进程。当收到请求时,master 会将请求转发给一个 worker,当有多个请求的时候,master 会转发给底下的多个 worker,当 worker 不够的时候,master 会根据配置文件动态的创建 worker 进程,所以接受请求的时候不会受到阻塞,这非常类似于 Nginx ,当然也会根据配置来销毁空闲 worker 进程。 php-fpm.service
就是用来管理 php-fastcgi 的 master 和 worker 进程。
分析日志
知道了 php-fpm
之后,可以得到其实解析 PHP 脚本的进程是 worker 进程,那么可以通过查看 OOM 时的日志,找到当时 worker 进程的状况。
博客是在 00:43 挂的,所以使用 journalctl
命令查询特定时间段的日志:
[root@sz-blog ~]# journalctl --since 00:40 --until 00:43 > /tmp/oom.log
日志内容如下:
Nov 25 00:43:01 kernel: tuned invoked oom-killer: gfp_mask=0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), order=0, oom_score_adj=0
Nov 25 00:43:02 kernel: CPU: 0 PID: 678 Comm: tuned Not tainted 5.14.0-368.el9.x86_64 #1
Nov 25 00:43:02 kernel: Hardware name: Alibaba Cloud Alibaba Cloud ECS, BIOS 449e491 04/01/2014
Nov 25 00:43:02 kernel: Call Trace:
Nov 25 00:43:02 kernel: <TASK>
Nov 25 00:43:02 kernel: dump_stack_lvl+0x34/0x48
Nov 25 00:43:02 kernel: dump_header+0x4a/0x201
Nov 25 00:43:02 kernel: oom_kill_process.cold+0xb/0x10
Nov 25 00:43:02 kernel: out_of_memory+0xed/0x2e0
Nov 25 00:43:02 kernel: __alloc_pages_slowpath.constprop.0+0x6e8/0x960
Nov 25 00:43:02 kernel: __alloc_pages+0x21d/0x250
Nov 25 00:43:02 kernel: folio_alloc+0x17/0x50
Nov 25 00:43:02 kernel: __filemap_get_folio+0x1cd/0x330
Nov 25 00:43:02 kernel: filemap_fault+0x40b/0x740
Nov 25 00:43:02 kernel: __do_fault+0x36/0x140
Nov 25 00:43:02 kernel: do_read_fault+0xf0/0x160
Nov 25 00:43:02 kernel: do_fault+0xa9/0x390
Nov 25 00:43:03 kernel: __handle_mm_fault+0x585/0x650
Nov 25 00:43:03 kernel: handle_mm_fault+0xc5/0x2a0
Nov 25 00:43:03 kernel: do_user_addr_fault+0x1b4/0x6a0
Nov 25 00:43:03 kernel: exc_page_fault+0x62/0x150
Nov 25 00:43:03 kernel: asm_exc_page_fault+0x22/0x30
Nov 25 00:43:03 kernel: RIP: 0033:0x7f949189c39a
Nov 25 00:43:03 kernel: Code: Unable to access opcode bytes at RIP 0x7f949189c370.
Nov 25 00:43:03 kernel: RSP: 002b:00007ffdd43d21f0 EFLAGS: 00010246
Nov 25 00:43:03 kernel: RAX: ffffffffffffff92 RBX: 00007ffdd43d2330 RCX: 00007f949189c39a
Nov 25 00:43:03 kernel: RDX: 0000000000000000 RSI: 0000000000000089 RDI: 00007f9491f40fec
Nov 25 00:43:03 kernel: RBP: 0000000000000000 R08: 0000000000000000 R09: 00000000ffffffff
Nov 25 00:43:03 kernel: R10: 00007ffdd43d2330 R11: 0000000000000246 R12: 0000000000000000
Nov 25 00:43:03 kernel: R13: 00007f9491f40fec R14: 0000000000000000 R15: 00007f9491f40ff0
Nov 25 00:43:03 kernel: </TASK>
Nov 25 00:43:03 kernel: Mem-Info:
Nov 25 00:43:03 kernel: active_anon:30750 inactive_anon:358463 isolated_anon:0
active_file:7 inactive_file:8378 isolated_file:0
unevictable:0 dirty:0 writeback:0
slab_reclaimable:10254 slab_unreclaimable:14247
mapped:33452 shmem:33896 pagetables:6074
sec_pagetables:0 bounce:0
kernel_misc_reclaimable:0
free:13407 free_pcp:62 free_cma:0
Nov 25 00:43:03 kernel: Node 0 active_anon:123000kB inactive_anon:1433852kB active_file:28kB inactive_file:33512kB unevictable:0kB isolated(anon):0kB isolated(file):0kB mapped:133808kB dirty:0kB writeback:0kB shmem:135584kB shmem_thp: 0kB shmem_pmdmapped: 0kB anon_thp: 751616kB writeback_tmp:0kB kernel_stack:4368kB pagetables:24296kB sec_pagetables:0kB all_unreclaimable? yes
Nov 25 00:43:03 kernel: Node 0 DMA free:7252kB boost:0kB min:388kB low:484kB high:580kB reserved_highatomic:0KB active_anon:180kB inactive_anon:5704kB active_file:0kB inactive_file:104kB unevictable:0kB writepending:0kB present:15992kB managed:15360kB mlocked:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB
Nov 25 00:43:03 kernel: lowmem_reserve[]: 0 1710 1710 1710 1710
Nov 25 00:43:03 kernel: Node 0 DMA32 free:46376kB boost:0kB min:44664kB low:55828kB high:66992kB reserved_highatomic:0KB active_anon:122820kB inactive_anon:1428148kB active_file:136kB inactive_file:33440kB unevictable:0kB writepending:0kB present:2080640kB managed:1790624kB mlocked:0kB bounce:0kB free_pcp:248kB local_pcp:0kB free_cma:0kB
Nov 25 00:43:03 kernel: lowmem_reserve[]: 0 0 0 0 0
Nov 25 00:43:03 kernel: Node 0 DMA: 47*4kB (UME) 12*8kB (UME) 12*16kB (UE) 22*32kB (UME) 11*64kB (UME) 10*128kB (UME) 2*256kB (UM) 3*512kB (UME) 2*1024kB (UM) 0*2048kB 0*4096kB = 7260kB
Nov 25 00:43:03 kernel: Node 0 DMA32: 324*4kB (UME) 354*8kB (UME) 260*16kB (UME) 361*32kB (UME) 182*64kB (ME) 67*128kB (UME) 25*256kB (UM) 0*512kB 0*1024kB 0*2048kB 0*4096kB = 46464kB
Nov 25 00:43:03 kernel: Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=1048576kB
Nov 25 00:43:03 kernel: Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
Nov 25 00:43:03 kernel: 42295 total pagecache pages
Nov 25 00:43:03 kernel: 0 pages in swap cache
Nov 25 00:43:03 kernel: Free swap = 0kB
Nov 25 00:43:03 kernel: Total swap = 0kB
Nov 25 00:43:03 kernel: 524158 pages RAM
Nov 25 00:43:03 kernel: 0 pages HighMem/MovableOnly
Nov 25 00:43:03 kernel: 72662 pages reserved
Nov 25 00:43:03 kernel: 0 pages cma reserved
Nov 25 00:43:03 kernel: 0 pages hwpoisoned
Nov 25 00:43:03 kernel: Tasks state (memory values in pages):
Nov 25 00:43:03 kernel: [ pid ] uid tgid total_vm rss pgtables_bytes swapents oom_score_adj name
Nov 25 00:43:03 kernel: [ 460] 0 460 8620 329 94208 0 -250 systemd-journal
Nov 25 00:43:03 kernel: [ 473] 0 473 8085 497 90112 0 -1000 systemd-udevd
Nov 25 00:43:03 kernel: [ 553] 81 553 2723 199 57344 0 -900 dbus-broker-lau
Nov 25 00:43:03 kernel: [ 554] 81 554 1314 159 49152 0 -900 dbus-broker
Nov 25 00:43:03 kernel: [ 556] 0 556 139890 2139 147456 0 0 CmsGoAgent.linu
Nov 25 00:43:03 kernel: [ 560] 997 560 676 40 40960 0 0 lsmd
Nov 25 00:43:03 kernel: [ 561] 0 561 4641 286 77824 0 0 systemd-logind
Nov 25 00:43:04 kernel: [ 573] 991 573 21130 184 65536 0 0 chronyd
Nov 25 00:43:04 kernel: [ 596] 0 596 250781 4912 217088 0 0 exe
Nov 25 00:43:04 kernel: [ 641] 0 641 64282 1178 139264 0 0 NetworkManager
Nov 25 00:43:04 kernel: [ 675] 982 675 112209 9776 196608 0 0 memcached
Nov 25 00:43:04 kernel: [ 677] 0 677 71269 2871 249856 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 678] 0 678 64074 3261 131072 0 0 tuned
Nov 25 00:43:04 kernel: [ 848] 27 848 390742 30204 471040 0 0 mariadbd
Nov 25 00:43:04 kernel: [ 871] 998 871 627854 1566 196608 0 0 polkitd
Nov 25 00:43:04 kernel: [ 956] 0 956 3948 322 69632 0 -1000 sshd
Nov 25 00:43:04 kernel: [ 959] 0 959 2137 215 49152 0 0 crond
Nov 25 00:43:04 kernel: [ 960] 0 960 761 23 49152 0 0 agetty
Nov 25 00:43:04 kernel: [ 961] 0 961 1403 22 53248 0 0 agetty
Nov 25 00:43:04 kernel: [ 998] 0 998 6151 650 69632 0 0 nginx
Nov 25 00:43:04 kernel: [ 10420] 983 10420 14003 8925 143360 0 0 nginx
Nov 25 00:43:04 kernel: [ 10421] 983 10421 14217 9115 143360 0 0 nginx
Nov 25 00:43:04 kernel: [ 13948] 983 13948 107522 39919 634880 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13951] 983 13951 107900 40635 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13952] 983 13952 108157 41937 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13953] 983 13953 107539 40598 634880 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13954] 983 13954 108054 40737 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 77409] 0 77409 203988 1105 86016 0 0 aliyun-service
Nov 25 00:43:04 kernel: [ 77562] 0 77562 4857 243 65536 0 0 assist_daemon
Nov 25 00:43:04 kernel: [ 82166] 983 82166 107787 35231 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82168] 983 82168 107819 35405 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82169] 983 82169 110583 39080 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82172] 983 82172 107824 35183 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82173] 983 82173 107215 35214 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82174] 983 82174 107278 34634 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82175] 983 82175 107223 33393 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82180] 983 82180 107317 35138 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82181] 983 82181 107777 34662 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82182] 983 82182 107249 35199 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82183] 983 82183 110966 38218 643072 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82184] 983 82184 107787 35609 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82190] 983 82190 107309 34645 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82191] 983 82191 107753 34230 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82192] 983 82192 107301 35175 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82193] 983 82193 107762 34650 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82194] 983 82194 107357 35015 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82200] 983 82200 106742 34020 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82201] 983 82201 107737 35328 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82202] 983 82202 107819 34901 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82203] 983 82203 107752 35727 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82204] 983 82204 107264 35391 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82210] 983 82210 107308 33879 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82211] 983 82211 107318 34622 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82212] 983 82212 107744 35832 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82213] 983 82213 107251 35344 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82214] 983 82214 107813 34812 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82220] 983 82220 107235 34438 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82221] 983 82221 107304 34841 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82222] 983 82222 107816 36233 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 88622] 0 88622 10741 975 94208 0 0 AliYunDunUpdate
Nov 25 00:43:04 kernel: [ 88653] 0 88653 26767 2245 282624 0 0 AliYunDun
Nov 25 00:43:04 kernel: [ 88664] 0 88664 46790 4263 393216 0 0 AliYunDunMonito
Nov 25 00:43:04 kernel: oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/system.slice/php-fpm.service,task=php-fpm,pid=13952,uid=983
Nov 25 00:43:04 kernel: Out of memory: Killed process 13952 (php-fpm) total-vm:432628kB, anon-rss:38968kB, file-rss:0kB, shmem-rss:128780kB, UID:983 pgtables:624kB oom_score_adj:0
Nov 25 00:43:05 systemd[1]: php-fpm.service: A process of this unit has been killed by the OOM killer.
Nov 25 00:43:06 systemd[1]: php-fpm.service: Failed with result 'oom-kill'.
Nov 25 00:43:06 systemd[1]: php-fpm.service: Consumed 2h 33min 29.899s CPU time.
可以看到当系统内存不足的时候,php-fpm
由于占用内存最多且没有设置 oom_score_adj
导致被杀掉。
从 php-fpm
的 error.log 也可以看到是被 SIGKILL
信号干掉的:
[root@sz-blog ~]# cat /var/log/php-fpm/error.log
[19-Nov-2023 00:00:05] NOTICE: error log file re-opened
[25-Nov-2023 00:43:01] WARNING: [pool www] child 13952 exited on signal 9 (SIGKILL) after 4019353.418821 seconds from start
[25-Nov-2023 00:43:01] NOTICE: [pool www] child 95930 started
[25-Nov-2023 00:43:05] NOTICE: Terminating ...
[25-Nov-2023 00:43:05] NOTICE: exiting, bye-bye!
把日志稍微处理以下,看一下当时 php-fpm
服务管理的进程信息:
[root@sz-blog ~]# grep php-fpm$ /tmp/oom.log
Nov 25 00:43:04 kernel: [ 677] 0 677 71269 2871 249856 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13948] 983 13948 107522 39919 634880 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13951] 983 13951 107900 40635 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13952] 983 13952 108157 41937 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13953] 983 13953 107539 40598 634880 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 13954] 983 13954 108054 40737 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82166] 983 82166 107787 35231 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82168] 983 82168 107819 35405 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82169] 983 82169 110583 39080 638976 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82172] 983 82172 107824 35183 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82173] 983 82173 107215 35214 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82174] 983 82174 107278 34634 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82175] 983 82175 107223 33393 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82180] 983 82180 107317 35138 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82181] 983 82181 107777 34662 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82182] 983 82182 107249 35199 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82183] 983 82183 110966 38218 643072 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82184] 983 82184 107787 35609 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82190] 983 82190 107309 34645 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82191] 983 82191 107753 34230 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82192] 983 82192 107301 35175 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82193] 983 82193 107762 34650 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82194] 983 82194 107357 35015 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82200] 983 82200 106742 34020 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82201] 983 82201 107737 35328 622592 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82202] 983 82202 107819 34901 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82203] 983 82203 107752 35727 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82204] 983 82204 107264 35391 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82210] 983 82210 107308 33879 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82211] 983 82211 107318 34622 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82212] 983 82212 107744 35832 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82213] 983 82213 107251 35344 614400 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82214] 983 82214 107813 34812 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82220] 983 82220 107235 34438 610304 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82221] 983 82221 107304 34841 618496 0 0 php-fpm
Nov 25 00:43:04 kernel: [ 82222] 983 82222 107816 36233 622592 0 0 php-fpm
除去一个 master 进程外,足足有 35 个 worker ,每个 worker 占用的 rss 也很大,问题就是出现在这里了。
我需要这么多个 worker 进程吗?
配置 php-fpm
php-fpm
提供了一个默认的配置文件,位于 /etc/php-fpm.d/www.conf
,在这个配置文件中,有描述了 worker 进程数量相关的配置:
; Choose how the process manager will control the number of child processes.
; Possible Values:
; static - a fixed number (pm.max_children) of child processes;
; dynamic - the number of child processes are set dynamically based on the
; following directives. With this process management, there will be
; always at least 1 children.
; pm.max_children - the maximum number of children that can
; be alive at the same time.
; pm.start_servers - the number of children created on startup.
; pm.min_spare_servers - the minimum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is less than this
; number then some children will be created.
; pm.max_spare_servers - the maximum number of children in 'idle'
; state (waiting to process). If the number
; of 'idle' processes is greater than this
; number then some children will be killed.
; ondemand - no children are created at startup. Children will be forked when
; new requests will connect. The following parameter are used:
; pm.max_children - the maximum number of children that
; can be alive at the same time.
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic
选择管理进程数量的类型有三种:
- static 静态的指定进程数量
- dynamic 基于一些配置动态的管理进程数量
- ondemand 启动时不创建子进程,当新请求进来时再创建,这种类型已被废弃,不考虑
static
static 模式下只需要配置 pm.max_children
就可以了,可以根据系统内存容量进行计算。
dynamic
dynamic 模式下进程数最少为 1 个,且通过以下参数进行调整数量:
- pm.max_children 定义进程最多的数目
- pm.start_servers 启动时创建进程的数目
- pm.min_spare_servers 最小处于
idle
状态的进程数量,少于的话将会创建 - pm.max_spare_servers 最大处于
idle
状态的进程数量,大于的话将会被销毁
计算合理的进程数目
列出所有的 php-fpm
进程:
[root@sz-blog ~]# ps -ylC php-fpm --sort:rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 0 96826 1 0 80 0 38412 71269 ep_pol ? 00:00:00 php-fpm
S 983 96828 96826 0 80 0 99464 75530 skb_wa ? 00:00:06 php-fpm
S 983 96835 96826 0 80 0 106412 94747 skb_wa ? 00:00:06 php-fpm
S 983 96831 96826 0 80 0 106920 95270 skb_wa ? 00:00:06 php-fpm
S 983 96832 96826 0 80 0 107080 94756 skb_wa ? 00:00:06 php-fpm
S 983 96829 96826 0 80 0 107208 94781 skb_wa ? 00:00:06 php-fpm
S 983 96830 96826 0 80 0 127036 106533 skb_wa ? 00:00:06 php-fpm
计算平均值:
[root@sz-blog ~]# ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
96M
可得出每个 Pool 平均使用的内存大概为 96M,由此可以计算系统最多能创建多少个进程,计算公式:最大数量=可用内存/平均使用内存
假设能够使用的内存为 1000M ,就可以得出最大运行的数量为 10
。
所以如果使用 static
模式的话,pm.max_children
设置为 10,如果设置为 dynamic ,则相关参数可参考:
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 5
应用配置并检查状态
修改完成后需要重启 php-fpm
,并验证相关值:
[root@sz-blog ~]# systemctl restart php-fpm.service
[root@sz-blog ~]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; preset: disabled)
Active: active (running) since Sat 2023-11-25 13:33:20 CST; 3s ago
Main PID: 97658 (php-fpm)
Status: "Ready to handle connections"
Tasks: 3 (limit: 11041)
Memory: 19.4M
CPU: 71ms
CGroup: /system.slice/php-fpm.service
├─97658 "php-fpm: master process (/etc/php-fpm.conf)"
├─97659 "php-fpm: pool www"
└─97660 "php-fpm: pool www"
Nov 25 13:33:20 sz-blog systemd[1]: Starting The PHP FastCGI Process Manager...
Nov 25 13:33:20 sz-blog systemd[1]: Started The PHP FastCGI Process Manager.
可以看到启动后只有两个 worker 进程,后续需要注意内存使用量和进程的数量。