The HestiaCP php-fpm process takes up a lot of CPU resources. How can I optimize it?

HestiaCP Server CPU soaring? A complete guide to PHP-FPM process optimization!

The HestiaCP php-fpm process takes up a lot of CPU resources. How can I optimize it?

Is the server CPU always at 100%? Is the fan spinning like crazy? Is the website loading slower?The culprit might be php-fpm!

I recently discovered chenweiliang.com This PHP pool php-fpm The process ate up the CPU like crazy, and the server crashed!

In order to revive the server, I tried a series of optimization methods and finally succeeded in reducing the CPU usage.

Now, let's share this Efficient Optimization Guide! 🚀

1. Limit the number of php-fpm processes

php-fpm default settings mayunlimitedThe process is created uncontrollably, causing the server CPU to overload.

To optimize it, we can modify the process management parameters.

turn on 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.

Locate and modify the following 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 后自动退出

then restart php-fpm To make it effective:

sudo systemctl restart php-fpm

effect: Limit the number of processes to prevent php-fpm from over-consuming CPU resources.

2. Enable OPCache (to improve PHP performance)

PHP reparses the code every time it is executed, which is a waste of resources.
The solution? Enable OPCache and cache your PHP code!

edit php.ini file:

sudo nano /etc/php/*/fpm/php.ini

Add or modify the following:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0  # 禁用实时检测,提高性能

After saving, restart the PHP process:

sudo systemctl restart php8.3-fpm

effect: Reduce repeated parsing of PHP code, reduce CPU burden, and improve execution efficiency.

3. Limit the maximum execution time of PHP scripts

If a PHP script runs for too long, CPU resources will be continuously occupied.
Setting a reasonable timeout period can prevent "stubborn" processes from occupying the server for a long time.

turn on php.ini file:

sudo nano /etc/php/*/fpm/php.ini

Modify the following parameters:

max_execution_time = 30  # PHP 脚本最多执行 30max_input_time = 30  # 处理输入数据最多 30memory_limit = 256M  # 限制单个 PHP 进程的内存占用

Then restart the PHP process:

sudo systemctl restart php-fpm

effect: Prevent PHP processes from running for a long time and reduce CPU resource waste.

4. Check MySQL slow query

PHP-FPM has a high load,It may be caused by the slow SQL query!
Open MySQL Slow query log, find out the SQL statements that are holding back.

Edit the MySQL configuration file:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add to:

slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 1  # 超过 1 秒的查询会被记录

After saving, restart MySQL:

sudo systemctl restart mysql

and then use mysqldumpslow Analyze slow queries:

mysqldumpslow -s c -t 10 /var/log/mysql-slow.log

effect: Find out time-consuming SQL queries, optimize database performance, and indirectly reduce PHP load.

5. Limit Memcached resource usage

Looking at the process list, I found Memcached process CPU usage is 24.8%!
It may be that the cache allocation is too large, causing the CPU to process a large amount of data.

Adjustment memcached Configuration:

sudo nano /etc/memcached.conf

Revise:

-m 32  # 限制 Memcached 内存使用 32MB

Then reboot:

sudo systemctl restart memcached

effect: Reduce the CPU burden of the Memcached process and improve cache efficiency.

6. Enable Nginx to cache static resources

Many times, PHP processes requests that contain a large number of CSS, JS, images, but these files don't need PHP parsing at all!
The solution? Use Nginx to cache static resources!

Edit Nginx configuration:

sudo nano /etc/nginx/nginx.conf

Add to:

location ~* \.(jpg|jpeg|png|gif|css|js|ico|xml)$ {
    expires max;
    log_not_found off;
}

After saving, restart Nginx:

sudo systemctl restart nginx

effect: Reduce the number of times PHP processes static files and reduce CPU usage.

7. Find the PHP script that uses the most CPU

If the CPU is still too high after optimization, it may be that some PHP scripts are running wildly.
Use the following command to find out the PHP process with the highest CPU usage:

ps -eo pid,user,pcpu,pmem,args --sort=-pcpu | grep php

If you find that a PHP script is taking up too much CPU, you can kill it directly:

kill -9 PID

effect:Accurately identify the "bad apples" and prevent the PHP process from occupying the CPU for a long time.

Summary: 7 ways to optimize PHP-FPM

Limit the number of PHP-FPM processes, to prevent CPU overload
Enable OPCache, reduce repeated parsing of PHP code
Setting PHP timeout, to prevent long-term operation
Checking MySQL slow queries, optimize database query performance
Adjust Memcached Memory, reducing CPU burden
Enable Nginx static cache, reducing the PHP parsing burden
Find high CPU usage PHP scripts, precise optimization

in conclusion

Server optimization is like fitness, you need to make precise adjustments and don’t mess around!
After a series of optimizations, my server CPU load has dropped from 80% down to 15%, the website opens twice as fast!
If your server has similar problems, try these methods! 💪

👉 Act now! Optimize your PHP-FPM and save your server! 🚀

Comment

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

Article directory
Scroll to Top