Need help?

Our experts have had an average response time of 11.43 minutes in March 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

PHP-FPM tuning for high load – The Best practices!

by | Nov 23, 2018

“We’re having high overload in the php fpm service (it loads the 16 VCPU with 100%). I’ve to restart the service 4 times. We need your help.”

We got this request recently from a server owner who was facing high CPU load due to PHP-FPM processes.

In our role as Technical Support Services for web hosting companies, configuring and managing the web servers for best performance, is a major task we do.

Analyzing the reasons for high server load and resolving it, form a part of that service. Here’s how we tackled this issue.

 

What is PHP-FPM? Why does it cause high load?

PHP-FPM is PHP FastCGI Process Manager. It is a FastCGI daemon that allows a website to handle high load. But sometimes this service itself causes high load.

To debug the issue, the first thing we did is to examine the PHP-FPM error logs, which is available in a location such as ” /var/log/php7.0-fpm.log “.

The location for the error logs varies based on the installation path and PHP version. While checking the error log, we found some warnings like these.

[12-Nov-2018 11:12:11] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 9 idle, and 89 total children

 

This message means that the PHP-FPM pool is very busy and is unable to handle the high traffic with the existing configuration parameters.

PHP-FPM can lead to high server load due to many reasons:

  1. Too many processes running due to high traffic or attacks
  2. A related service such as MySQL or web server being abused
  3. PHP-FPM configuration is not properly mapped to available server resources
  4. Poorly coded or buggy applications abusing the server resources

How to perform PHP-FPM tuning for high load

The first thing server owners tend to do for an immediate fix, is to restart the PHP-FPM service. But simply restarting the service is only a band-aid solution, and you will soon see the load spiking.

When we debug such load issues, we analyze the logs and server performance over a period of time. This helps us to pinpoint the culprit and take prompt actions to fix it.

1. Switch process manager

Process manager is mainly of 3 types – dynamic, ondemand and static. The performance of the service drastically varies based on this type.

In dynamic type, the number of child processes is set dynamically based on the PHP-FPM parameters in conf file. But it is a bit memory-intensive type.

In static type, the number of child processes is fixed by pm.max_children parameter, but this type is not flexible for a server with changing web traffic. It also consumes too much memory.

In ondemand type, the PHP-FPM processes are spawned only on demand, based on the traffic. This type helps to manage varying traffic in memory restrained servers. But the overhead increases when there is so much traffic fluctuation.

The PHP-FPM process manager is determined after assessing the available server memory, traffic spikes, website pages, CPU usage, etc.

2. Tweak PHP-FPM parameters

PHP-FPM has a lot of configuration parameters which determine the way it performs. These parameters have to be determined based on available server resources such as RAM and CPU.

For instance, the total processes that run in a server would be approximately = (Total RAM / Memory per process) . In a server with multiple services, all of them are taken into account for the tuning process.

The PHP-FPM configuration file will be available at /etc/php-fpm.conf or some other location based on the service path. The contents of the file would look like:

listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 150

 

Some of the major parameters that we tune for performance are:

    1. pm.max_children – This is used to set the total number of processes allowed
    2. pm.start_servers – The number of child processes created on startup is defined by this
    3. pm.min_spare_servers – Defines the minimum number of idle processes
    4. pm.max_spare_servers – Sets the maximum number of idle server processes
    5. pm.process_idle_timeout – The number of seconds an idle process will be alive
    6. pm.max_requests – This sets the execution time of each child process and is used to curb memory leaks.

A common mistake server owners do, is to set very high values for these parameters, to get maximum performance. But if there are not enough resources, such high values can crash the server.

The value for ‘pm.max_children’ parameter is first determined based on the server resources, website traffic and content of the sites. Other parameters are then derived upon further testing.

3. Enable slow log

In cases where the parameters are fine, but a certain application or site is suspected to cause the high load, we enable the slow log option in PHP-FPM.

request_slowlog_timeout = 6s
slowlog = /var/log/php-fpm/slowlog-site.log

 

The log file mentioned for ‘slowlog’ parameter in the PHP-FPM config file records the requests that take more than 6 seconds to execute.

Analyzing these requests enable us to track and fix the resource-intensive requests and applications in the server.

4. Managing multiple pools

PHP-FPM has the option to configure multiple resource pools for separate applications. This helps to contain resources per application and for additional security.

By configuring parameters based on the pool, we restrict a single pool from using up all server resources. Unused pools are deleted for further management.

 

PHP-FPM tuning for high load – An ongoing process!

Tweaking PHP-FPM for best performance, is not just a template fix. The configuration and parameters for the service would vary based on the server needs and resources.

During peak traffic, the performance would be different from that of normal hours. So, after doing the tweaks, we conduct stress tests to see how the service performs during high load.

Based on the server performance during stress test, we do further fine-tuning of the service. We also monitor the server load for a period of time, to ensure that the changes has indeed helped.

 

CPU load reduction after tuning PHP-FPM

 

 

In this scenario, we were able to reduce the server load drastically, by performing the PHP-FPM tweaks.

But PHP-FPM tuning alone may not help in certain scenarios where services such as MySQL or web server too are culprits. In those cases, we perform additional tasks such as:

  • Fine-tuning the MySQL server and web server such as Apache and Nginx
  • Limiting server resources based on service, applications, users, etc. to avoid server crash
  • Setting up load balancers or clusters to distribute services in high traffic servers
  • Monitoring the server 24/7 and conducting periodic server audits to detect and resolve issues then and there
  • Enabling page caching and optimizing code and queries to avoid applications from overloading server

 

Conclusion

Today we saw how our Support Engineers fix high load issue due to PHP-FPM service. In our customers’ servers, we monitor the server performance 24/7 and perform periodic server tweaks to ensure that server load stays normal.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

8 Comments

  1. ali

    hi
    where is php-fpm configuration (php7.3)

    Reply
    • Sijin George

      Hello Ali,
      Usually, the php-fpm configuration will be available at /etc/php/7.3/fpm/php.ini. Again this depends on the installation location on your server too.

      Reply
  2. ali

    hi
    where is php-fpm configuration (php7.3) ?
    in centos 7

    Reply
    • Sijin George

      Hi Ali,
      The php-fpm configuration depends on php installation path. Usually, it will be at /etc/php/7.3/fpm/php-fpm.conf. If you can’t find it out, we can help. We’ll be happy to talk to you on chat (click on icon at right-bottom)

      Reply
  3. safi

    I have the same issue my server specification is 8gb ram 4 core cpu vps we have high load and system goes into sleep

    Reply
    • Hiba Razak

      Hello Safi,
      Our Experts can help you with the issue, we’ll be happy to talk to you on chat (click on the icon at right-bottom).

      Reply
  4. Alex

    This is very interesting, thank you for the write-up! In your experience, on a server with 4 CPU cores available, what number of requests per minute would lead to a constant 50% load on all four? I understand that this depends heavily on the hardware, but what would be a general approximate range that you would consider reasonably normal?

    Reply
    • Hiba Razak

      Hi Alex,
      Assuming an average workload per request, a rough approximation for a server with 4 CPU cores would be between 400 and 800 requests per minute. However, this is only a very rough estimate, and the precise figure may vary based on the details of the situation.
      It’s also worth noting that a constant 50% load on all four cores may not be the optimal utilization level for the server, as it could potentially lead to bottlenecks and slower response times.

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF