For PHP-FPM multiple process mode, to avoid the memory leak problem is very simple, is to let php-cgi processing a certain number of processes after the exit.
Otherwise, memory leaks caused by PHP programs or Third-party modules (such as ImageMagick extensions) can lead to memory exhaustion or inadequacy.
There are related configurations in php-fpm.conf:
#请自行按需求配置
Pm.max_requests = 1024
There is actually another value associated with it Max_children, this is how many processes each php-fpm will build, so the actual memory consumption is max_children*max_requests* each request using memory.
Other rough ways include setting up cron kill-php-cgi that consume too much memory,
1. Check the memory footprint of the PHP process, kill the memory using the excess process
In general, if the php-cgi process consumes more than 1% of the memory, consider whether to kill it. Because of normal circumstances, the php-cgi process typically occupies 0.2% or less.
Here's a script for you to use, just put it on a cron task and run it every minute.
Use the CRONTAB-E command, and then add the following scheduling task
* * * * */bin/bash/usr/local/script/kill_php_cgi.sh * * *
The kill_php_cgi.sh script is as follows
#!/bin/sh
#如果是要杀掉php-FPM process, in the following statement php-cgi please change to PHP-FPM
pids= ' Ps-ef|grep php-cgi|grep-v "grep" |grep-v "$" | awk ' {print $} '
If ["$pids"!= ""];then
for PID in $pids;
Todo
Kill-9 $pid
Done
Fi
2. Increase the memory and set the value of the php_fcgi_max_requests to the value corresponding to your total memory storage amount
3. Optimize the program, reduce the amount of memory used to process each request
If PHP-FPM is able to provide configuration subprocess memory exceeding the specified size, it will be more convenient.