Article directory
- 1 The core reason why PHP-FPM is overloaded
- 2 PHP-FPM process pool optimization (core parameter adjustment)
- 3 Enable PHP-FPM status monitoring to keep track of the progress at any time
- 4 Optimize PHP-FPM logs to quickly troubleshoot problems
- 5 Restart PHP-FPM regularly to prevent memory leaks
- 6 Still having problems? Optimize!
- 7 Summary: Optimize PHP-FPM and the website will no longer crash!
Have you ever encountered this situation?Website access suddenly slowed down, or even resulted in a 500 error. After restarting PHP-FPM, it returned to normal., but the problem reappears after a while? This is really frustrating!
Why is this happening?In fact, this is usuallyThe PHP-FPM process pool is not configured properly, or the server resources are insufficient.Today, we will thoroughly optimize HestiaCP PHP-FPM under the hood makes the website as stable as a rock!
The core reason why PHP-FPM is overloaded
PHP-FPM is a进程管理器, which is responsible for processing dynamic requests. If the configuration is not reasonable, it may lead to:
- Server resources are exhausted, causing PHP-FPM to be unable to respond to new requests in a timely manner;
- Too few processes, when traffic suddenly increases, it cannot be processed in time;
- Process usage is too high, causing the CPU load to explode.

How to tell if PHP-FPM is overloaded?
can use top Or htop Command to view CPU and memory usage:
top -c
If you see process information similar to the following, it means PHP-FPM is running under high load:
1669293 abc 20 0 790284 227880 185568 R 73.1 0.9 1:30.09 php-fpm: pool chenweiliang.com
1669522 abc 20 0 801924 224224 170236 R 69.9 0.9 0:59.01 php-fpm: pool chenweiliang.com
Do you see these processes taking up more than 70% of the CPU? If this happens often, your PHP-FPM There must be a problem!
So, how can we optimize the PHP-FPM configuration so that the server is no longer overloaded?
PHP-FPM process pool optimization (core parameter adjustment)
First, open php-fpm Configuration file:
sudo nano /etc/php/*/fpm/pool.d/www.conf- *Change to your PHP version, such as PHP8.3, and change it to this:
/etc/php/8.3/fpm/pool.d/www.conf
Query the PHP version set by HestiaCP
v-list-web-domain user domain.com
For example:
v-list-web-domain abc chenweiliang.com
In the output, you will see something like:
PHP SUPPORT yes
PHP MODE php-fpm
PHP VERSION 8.3
This means that the site uses PHP 8.3.
Let's take a look at your PHP-FPM configuration:
[chenweiliang.com]
listen = /run/php/php8.3-fpm-chenweiliang.com.sock
listen.owner = abc
listen.group = www-data
listen.mode = 0660
user = abc
group = abc
pm = ondemand
pm.max_children = 8
pm.max_requests = 4000
pm.process_idle_timeout = 10s
You can see that your pm Used ondemand.Although it can reduce resource usage during idle time, when traffic suddenly increases, the process may not be able to respond in time., resulting in a 500 error.
1. Adjust PHP-FPM process pool parameters
If the configuration uses dynamicThis is a method of pre-starting some work processes and dynamically adjusting them according to the request volume, which can respond faster when the request volume suddenly increases.
For websites with a certain amount of traffic, it is recommended to use pm = dynamicBecause it can maintain a certain amount of idle processes and avoid 500 errors during high concurrency.
It is recommended to use it only when the access volume is extremely low and the memory resources are tight. pm = ondemand To save resources.
Suggested to ondemand, and optimize pm.max_children And other parameters:
pm = dynamic
pm.max_children = 16 ; 根据服务器资源调整,建议值:CPU 核心数 × 2
pm.start_servers = 4 ; 初始进程数,建议设为 max_children × 25%
pm.min_spare_servers = 2 ; 最小空闲进程数
pm.max_spare_servers = 7 ; 最大空闲进程数
pm.max_requests = 3000 ; 每个子进程处理完 3000 个请求后自动重启
pm.process_idle_timeout = 10s ; 空闲进程 10s 后自动退出
Why do you want to change it like this?
pm = dynamic: Allocate processes more flexibly to avoid request waiting that may be caused by ondemand;pm.max_children = 16: Prevent 500 errors caused by too few processes;pm.start_servers = 5: Avoid slow process startup;pm.max_requests = 3000: Preventing memory leaks, recycle the process regularly.
2. Limit the execution time of PHP scripts to prevent long-term occupancy
request_terminate_timeout = 30s ; 超过 30s 的 PHP 脚本自动终止
php_admin_value[memory_limit] = 128M ; 限制 PHP 进程最大内存占用
This can prevent somePHP scripts that use too much CPU can bring down your server.
After saving, restart the PHP process:
sudo systemctl restart php8.3-fpmEnable PHP-FPM status monitoring to keep track of the progress at any time
Enable PHP-FPM process monitoring and view it at any timeCurrent number of active processes and request waiting status, to avoid server overloading.
On php-fpm.conf Added in:
pm.status_path = /status
Then, Nginx configuration:
location /status {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
allow 127.0.0.1;
deny all;
}
In this way, you can http://yourdomain.com/status Check out PHP-FPM in action!
Optimize PHP-FPM logs to quickly troubleshoot problems
On php-fpm.conf Add to:
php_admin_value[error_log] = /var/log/php-fpm/error.log
php_admin_value[log_errors] = On
php_admin_value[error_reporting] = E_ALL
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 5s ; 执行超过 5s 的脚本记录到日志
In this way, whenever a 500 error occurs, you can directly view the log:
tail -f /var/log/php-fpm/error.log
See if PHP reports an error, such as out of memory,script execution timeout and so on.
Restart PHP-FPM regularly to prevent memory leaks
able to pass cron Restart PHP-FPM regularly to prevent long-running processes from causingmemory leak.
crontab -e
Add the following scheduled task to automatically restart PHP-FPM at 3 am every day:
0 3 * * * /usr/sbin/service php8.3-fpm restart
Still having problems? Optimize!
If you still follow the above optimizationOccasionally 500 errors occur, you can continue with the following optimizations:
1. Enable OPcache to improve PHP execution efficiency
If OPcache is not enabled yet, you can install it like this (using Ubuntu as an example):
sudo apt install php8.3-opcache -y
Then edit php.ini :
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.validate_timestamps=1
The result? PHP page execution speed is greatly improved!
2. Nginx configuration optimization
Make sure that Nginx related parameters are reasonable, such as fastcgi_read_timeout Adjust it appropriately to avoid PHP scripts being terminated by Nginx due to long execution time:
fastcgi_read_timeout 60s;
client_max_body_size 100M;
Summary: Optimize PHP-FPM and the website will no longer crash!
What adjustments have we made after this optimization?
✅ Optimizing the PHP-FPM process pool,use ondemandand optimize pm.max_children parameter;
✅ Limiting the execution time of PHP scripts, to prevent long-term CPU occupation;
✅ Enable PHP-FPM monitoring, view the process load in real time;
✅ Optimizing PHP-FPM logs, quickly troubleshoot 500 errors;
✅ Restart PHP-FPM regularly, prevent memory leaks;
✅ Enable OPcache, improve PHP execution efficiency;
✅ Optimizing Nginx Configuration, to avoid timeout issues.
After this optimization, the PHP-FPM load will be greatly reduced and the website will run more stably! 🔥
Go try it now! 💪🚀
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ )'s sharing of "HestiaCP PHP-FPM load is too high? Dynamic web page 500 error? This optimization will take effect immediately! " may be helpful to you.
Welcome to share the link of this article:https://www.chenweiliang.com/cwl-32512.html
To unlock more hidden tricks🔑, welcome to join our Telegram channel!
If you like it, please share and like it! Your sharing and likes are our continuous motivation!