PHP-FPM if there is a storage leakage problem may cause insufficient server memory and thus cause the server resources to use up, this will cause the website to be unable to access, next let's take a look at a configuration to avoid Memory depletion caused by PHP-FPM Memory leakage. PHP-FPM if there is a storage leakage problem may cause insufficient server memory and thus cause the server resources to use up, this will cause the website to be unable to access, next let's take a look at a configuration to avoid Memory depletion caused by PHP-FPM Memory leakage.
Script ec (2); script
For PHP-FPM multi-process mode, to avoid Memory leakage problem is very simple, is to let the PHP-CGI after processing a certain number of processes to exit.
Otherwise, memory leaks caused by PHP programs or third-party modules (such as Imagemagick extensions) may cause memory depletion or insufficiency.
Php-fpm.conf with related configuration:
# Configure as needed
Pm. max_requests = 1024
In fact, there is another value associated with it max_children, which is how many processes will be created each time php-fpm, so the actual memory consumption is max_children * max_requests * memory usage for each request.
Some other brute-force methods include setting up cron to kill the php-cgi that occupies too much memory,
1. Check the memory usage of the php process and kill the process with excessive memory usage.
Generally, if the php-cgi process occupies more than 1% of the memory, you have to consider whether to kill it. In normal cases, php-cgi processes generally occupy 0.2% or less.
Here we provide a script for you to use, that is, to run it once every minute in a cron task.
Run 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
# If you want to kill the php-fpm process, change php-cgi to php-fpm in the following statement.
Pids = 'ps-ef | grep php-cgi | grep-v "grep" | grep-v "$0" | awk '{print $2 }''
If ["$ pids "! = ""]; Then
For pid in $ pids;
Do
Kill-9 $ pid
Done
Fi
2. Increase the memory and set the value of PHP_FCGI_MAX_REQUESTS to the value corresponding to the total memory storage.
3. Optimize the program to reduce the memory used for processing each request
If the PHP-FPM can provide the configuration that the sub-process memory exceeds the specified size and is killed, it is much easier.