HestiaCP PHP-FPM Load too high? Dynamic web page 500 error? This optimization will take effect immediately!

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.

HestiaCP PHP-FPM Load too high? Dynamic web page 500 error? This optimization will take effect immediately!

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.5, 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.5 

This means that the site uses PHP 8.5.

Let's take a look at your PHP-FPM configuration:

[chenweiliang.com]
listen = /run/php/php8.5-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.

www.conf: The system's built-in "universal resource pool"

After installing PHP-FPM, the system will automatically provide you with a... www.conf file.
itsPositioningIt's very simple—it's just a default process pool that works out of the box, usually attached to... www data User download.

This type of pool is particularly suitable for single-site environments: the configuration is lightweight, and the parameters are all generic templates, such as:

user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
pm.max_children = 5

If you only host one site, you can use it directly and reliably without any extra hassle.

etUFO.org.conf: Custom pool

Once you're running multiple sites, you can't keep everyone crammed into the same pool.
At this point, HestiaCP will automatically create a separate pool for each site, for example... etUFO.org.confSpecialized for domain names etufo.org service.

The common way to play is:

  • Change users and groups:user = etufo.group = etufo
  • Independent monitoring:listen = /run/php/etufo.sock
  • Adjusting the number of processes ensures rock-solid stability even under high concurrency.
  • Separate log files make troubleshooting clearer.

The benefits are obvious:Secure isolationEven if one site is compromised, the other sites will remain unaffected.

dummy.conf: dummy file

dummy.conf These are usually examples or templates provided by the system.
It won't actually run unless you manually modify and enable it.
Its significance is more like an "operation manual," telling you how to write a new pool configuration.

Why divide the pool?

  • SafetyUse different users for different sites to avoid conflicting permissions.
  • Performance optimizationThe number of processes can be adjusted individually for each pool, allowing for flexible adjustments based on traffic demand.
  • IsolationLogs, errors, and listening addresses are all separated, making troubleshooting easier.

For example: even if www.conf It collapsed.etufo.org.conf It will still run normally and will not bring down the entire server.

Real-world scenarios

  • Single-site server: www.conf is enough.
  • Multisite serverEach site has its own independent .conf file, such as etufo.org.conf.
  • dummy.confFor reference only, not recommended.

Configuration Comparison

www.conf (default pool)

[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
pm = dynamic
pm.max_children = 5

etufo.org.conf (Custom Pool)

[etufo.org]
user = etufo
group = etufo
listen = /run/php/etufo.sock
pm = dynamic
pm.max_children = 20
access.log = /var/log/php-fpm/etufo.access.log

The main difference is:User identity, listening address, number of processes.

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 dynamic, 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-fpm

Optimize PHP-FPM based on VPS configuration

VPS configuration example:

  • Description: VPS 3 NVMe
  • Disk Space: 300 GB
  • CPU cores: 8
  • RAM: 24 GB

Depending on your VPS configuration (8-core CPU, 24 GB RAMYour server resources are more than sufficient. For PHP-FPM, 24 GB of memory allows you to configure a very high number of concurrent processes.

In a production environment, we typically provide the system itself and the Apache database (such as...) MySQL/MariaDB) and cache (such as Redis/MemcachedLeave enough memory (let's say 8GB-12GB), the rest... 12GB-16GB RAMIt can be completely assigned to PHP-FPM.

Based on the average usage per PHP process 40MB – 60MB In terms of memory usage, 1GB of memory can run approximately 16-25 processes.

The following is tailor-made for you.High-concurrency, high-performance FPM configurationIt can greatly improve Wordpress It has the capacity to handle heavy traffic and can prevent sudden surges in traffic from causing system crashes.

pm = dynamic

; 允许的最大 PHP 进程数(12GB 内存 / 40MB ≈ 300)
; 8核CPU搭配300个进程,可以轻松应对极高并发,且不至于让内存溢出
pm.max_children = 300

; 启动时创建的初始进程数(CPU核心数 * 4)
pm.start_servers = 32

; 维持的最小空闲进程数(服务器空闲时保留的进程,保证随时响应)
pm.min_spare_servers = 16

; 维持的最大空闲进程数(超过这个数量的空闲进程会被释放)
pm.max_spare_servers = 64

; 每个进程处理1000个请求后自动重启,高配置服务器可适当调大,有效防止WP插件内存泄露
pm.max_requests = 1000

; 单个请求最大执行时间,超时60秒强杀,防止死锁卡死
request_terminate_timeout = 60s

; 慢日志路径及触发阈值(请求超过5秒则记录,用于排查性能瓶颈)
slowlog = /var/log/php8.5-fpm.log.slow
request_slowlog_timeout = 5s

💡 Why this configuration?

  1. pm.max_children = 300This is a core optimization. Your previous configuration of 50 processes was too conservative for 24GB of memory. When encountering sudden surges in traffic (or bots flooding the background), 50 processes would be instantly overwhelmed, causing connection timeouts. Increasing it to 300 can improve your server's concurrency processing capacity several times over.
  2. pm.start_servers / `min_spare_serversBecause you have 8 CPU cores, you can initially and normally retain more idle processes, taking advantage of the multi-core advantage so that new requests can be opened instantly without waiting for process creation.
  3. pm.max_requests = 1000Increase from 500 to 1000. Your memory is large, and processes do not need to be restarted frequently. Increasing to 1000 can reduce the CPU consumption caused by frequent process destruction and creation.

After making the changes, remember to restart the PHP-FPM service for the changes to take effect.

systemctl restart php8.5-fpm

Enable 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.5-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.5-opcache -y

Then edit php.ini :

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.validate_timestamps=0
  • opcache.validate_timestamps=0
  • Disable real-time detectionReduce file system I/O and improve performance.
  • However, this means that you must manually clear the cache (restart the PHP service) after modifying PHP files.

After modifying the configuration, you must restart the PHP service for the changes to take effect.

sudo systemctl restart php<版本>-fpm

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! 💪🚀

If you're still eager to learn more about customizing PHP-FPM templates with HestiaCP, then this article will give you a deeper understanding:

👉 HestiaCP Custom PHP-FPM Template: PHP 8.5 Performance Optimization Secrets ▼

In this content, you will see:

  • High-concurrency optimization techniques: How to improve response speed through reasonable process configuration.
  • Security isolation solution: Avoid cross-site access risks and ensure account stability.
  • Logs and monitoring: Use slow logs to locate bottlenecks and continuously optimize website performance.

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top