How to Tune PHP-FPM Guide - Koerge (2024)

PHP-FPM is very tricky to tune correctly and there are so many variables that make it almost impossible to provide you with the ultimate settings. However, I can certainly guide you in the right direction to tune it for ultimate performance and reliability.

Common issues for PHP-FPM crashing after tuning

  • You tuned and stress tested PHP-FPM on cached pages but forgot to see how it performs on non-cached pages
  • You aren’t simulating enough connections, resulting in PHP-FPM running out of PM.Children and crashing
  • You tuned PHP-FPM to use far more RAM than you actually have. This is a very common issue when you tune on a cached page, but not tested it on non-cached pages. Remember, a non-cached page needs to execute a lot of PHP, requiring far more RAM than serving a cached HTML page.

PHP-FPM TUNING

  1. Start by checking the current configuration settings by running the command php-fpm -i. This will give you an overview of the current settings, including the version of PHP, the number of processes, and the current memory limits.
  2. Increase the number of processes by editing the pm.max_childrensetting in the PHP-FPM configuration file (usually located at /etc/php-fpm.d/www.conf). This setting controls the maximum number of child processes that PHP-FPM can spawn. A good starting point is to set this to the number of CPU cores on your server.
  3. Adjust the amount of memory allocated to each process by editing the pm.max_requestssetting. This setting controls the number of requests each child process can handle before being terminated and replaced with a new one. A higher value can help reduce memory usage and improve performance.
  4. Configure the PHP-FPM pool to use ondemand process management by editing the pmsetting in the PHP-FPM configuration file. This means that PHP-FPM will only spawn new child processes when needed, rather than keeping a fixed number of processes running at all times.
  5. Finally, it’s a good practice to monitor the performance of PHP-FPM by using tools such as top or htopto check the number of active processes, CPU usage, and memory usage.

Please note that these are general instructions and may vary depending on the specific version of PHP and the server configuration. Also, this is a starting point, and you may have to fine-tune the settings based on your specific use case and the traffic on your website.

Let’s give it a go

Now let’s begin, the settings you would need to change will live in the following file:

nano /etc/php/7.0/fpm/pool.d/www.conf

You would need to scroll down and find the following lines. Note: they are not altogether as shown below.

pm = dynamicpm.max_children = 4pm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3pm.max_requests = 200

These are the key settings which will heavily depend on how reliable PHP-FPM will be and much traffic it can handle.

Before we begin tuning for PHP-FPM you will need to use a server stress testing tool. I personally use an SEO tool called ScreamingFrog where I can set the crawl rate very high which will simulate traffic on random pages. However this is a paid tool. If you do not have access to this, you can use something like the Apache Stress test tool which can be installed using the following command

sudo apt-get install apache2-utils

In order to execute a stress test you would use the following command line

ab -c 100 -t 60 -r http://example.com/

-c 100means 100 requests per second and-t 60means for 60 seconds. It is strongly advised that you run this command from a different server then the one you are testing.

Here you will be able to see how many requests your server can handle, how long it took to complete the requests and if PHP-FPM crashes along with how much memory it’s using under load using ‘htop’.

This is where you would tweak the settings until you find the optimum performance. The key element here will be ‘pm.max_children’. Set this too high and your server will crash, set this too low and again your server will crash. This is heavily dependant on how much RAM your server has, how many CPU’s and what application is it running i.e. WordPress, Magento etc.

High numbers usually perform best for multi-core CPU servers serving cached pages. However, will likely crash PHP-FPM if you get a surge of request on non-cached pages. This is what makes this tricky to get right.

The second most important is ‘pm.max_requests’, after days of testing I always found setting this to 0 (unlimited) provides the best results when you are dealing with high concurrency.

I appreciate some of you may want actual figures, here’s one I did on an 8 core server with 16GB ram, running WordPress and full page caching in Redis.

pm = dynamicpm.max_children = 12pm.start_servers = 6pm.min_spare_servers = 4pm.max_spare_servers = 8pm.max_requests = 0

The configuration above allowed me to test up to 1,400 requests per second before maxing out the servers bandwidth at 2GB/s.

Tip:when stress testing high concurrency, make sure you do not have other bottlenecks such as your server running out of TCP/IP connections, or Nginx keeping connections open for too long! this is a common issue with high concurrency where you can spend hours trying to get your settings right, not realising that PHP-FPM is not your bottleneck.

How to Tune PHP-FPM Guide - Koerge (2024)

FAQs

How to Tune PHP-FPM Guide - Koerge? ›

As PHP-FPM can execute opcode from memory so quickly, it eliminates the need to read the source code for a script from disk and compile that source code to opcode. Reading data directly from the server's memory is significantly more efficient than reading it from the server's filesystem instead.

How to fine tune PHP-FPM? ›

An example of PHP-FPM configuration
  1. Step 1: Determine the maximum number of connection request: ...
  2. Step 2: Calculate the total number of PHP-FPM processes on the server: ...
  3. Step 3: Configure the maximum number of PHP-FPM child processes for the server: ...
  4. Step 4: Set the maximum requests per PHP-FPM process for the server:
Dec 2, 2023

How to increase the PHP-FPM timeout? ›

Configuring PHP-FPM Timeout Settings
  1. Open the PHP-FPM configuration file with a text editor.
  2. Find the request_terminate_timeout line.
  3. Set the value in seconds (e.g., request_terminate_timeout = 300 for a 5-minute timeout).
  4. If the line is missing, add it to the file.
Jul 25, 2024

Why is PHP-FPM faster? ›

As PHP-FPM can execute opcode from memory so quickly, it eliminates the need to read the source code for a script from disk and compile that source code to opcode. Reading data directly from the server's memory is significantly more efficient than reading it from the server's filesystem instead.

What is the PHP-FPM limit? ›

A max_children setting of 5 means only five processes can spawn for that particular domain. The purpose of the max_children directive is to provide a way to limit the number of resources used by PHP-FPM so that it cannot overwhelm the server. When the limit is hit, only a single domain will become unavailable.

Is FPM faster than Apache? ›

Apache mod_php is still much faster than php-fpm, and since slack uses a lot of PHP on the backend it makes a lot of sense for them.

How to fine tune server? ›

Load balancing can be implemented by using hardware devices, software applications, or cloud services, depending on your preferences and requirements. To fine-tune your load balancing, you need to monitor your server's load and performance, and adjust the settings according to your workload and performance goals.

How to improve PHP speed? ›

Tips for Optimizing PHP Performance and Speed
  1. Upgrade to the Latest PHP Version: ...
  2. Use Opcode Caching: ...
  3. Optimize Database Queries: ...
  4. Leverage Content Delivery Networks (CDNs): ...
  5. Minify and Compress Assets: ...
  6. Implement Browser Caching: ...
  7. Reduce HTTP Requests: ...
  8. Optimize Images:
Sep 30, 2023

Should you use PHP-FPM? ›

PHP-FPM provides a stable and secure environment for running PHP applications. If one PHP process encounters an error or becomes unresponsive, it won't affect other active processes. This isolation ensures that individual requests are isolated and do not impact the overall system stability.

What is the alternative to PHP-FPM? ›

HHVM (HipHop Virtual Machine), PHP, NGINX, uWSGI, and JavaScript are the most popular alternatives and competitors to PHP-FPM.

How many requests can PHP-FPM handle? ›

No - a PHP-FPM process can only handle one request at a time. Usually a process can only handle one task at a time - an exception is with lightweight processes (threads) where the OS process entity has more than one schedulable execution.

How do I know if PHP-FPM is working? ›

This can be done from the command line using the php-fpm-status tool. This way, you can check what requests your website is currently processing. You should replace "www.example.com" with the name of your website. The first part of the output shows the same information as the Status section described above.

What user should PHP-FPM run as? ›

If php-fpm is run as non-root then it will ignore the user and group directives for the pool config and run instead as the current user. Those directives only apply if php-fpm is run as root. The standard php-fpm docker container runs as root but has the pool configured to for www-data .

How do I clear PHP-FPM cache? ›

To flush PHP Opcache on the PHP-FPM method, you have to send a reload to your PHP-FPM daemon. The reload will clear the Opcache, and when the next request arrives, it will force it to rebuild the cache. You can flush the entire cache of all the websites by reloading the single master.

How do I change the PHP version in FPM FastCGI? ›

How to set and change a PHP version
  1. Go to Settings → Feature → Alternative PHP versions → Edit.
  2. Select "PHP CGI" in the ISPmanager PHP field.
  3. Enable the ISPmanager PHP for PHP-FPM option to make this PHP version available for "FastCGI (Nginx + PHP-FPM)".

How to speed up Apache PHP? ›

How to speed up Apache
  1. Disable modules you don't use. Every server install comes with a set of automatically enabled modules. ...
  2. Use Apache caching. Caching frequently accessed files and content can increase your server performance. ...
  3. Tune your MySQL settings for performance. ...
  4. Use a PHP accelerator. ...
  5. Reduce the number of processes.

Top Articles
The 7 Design Principles of a Blockchain Economy — Steemit
Resource Management Best Practices: 7 Tips to Consider - Epicflow
Sound Of Freedom Showtimes Near Governor's Crossing Stadium 14
Breaded Mushrooms
Celebrity Extra
Ross Dress For Less Hiring Near Me
Coffman Memorial Union | U of M Bookstores
The Idol - watch tv show streaming online
Chase Claypool Pfr
Knaben Pirate Download
Ap Chem Unit 8 Progress Check Mcq
Wordscape 5832
Connexus Outage Map
This Modern World Daily Kos
Busted Barren County Ky
Nutrislice Menus
Obsidian Guard's Cutlass
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Craigslist Missoula Atv
Water Trends Inferno Pool Cleaner
Home
Munis Self Service Brockton
Living Shard Calamity
Mals Crazy Crab
Roanoke Skipthegames Com
Meta Carevr
Bidrl.com Visalia
3 Ways to Drive Employee Engagement with Recognition Programs | UKG
Anesthesia Simstat Answers
Stephanie Bowe Downey Ca
Bj's Tires Near Me
Laveen Modern Dentistry And Orthodontics Laveen Village Az
Fbsm Greenville Sc
Gas Prices In Henderson Kentucky
Steven Batash Md Pc Photos
Glossytightsglamour
John F Slater Funeral Home Brentwood
Whitehall Preparatory And Fitness Academy Calendar
How are you feeling? Vocabulary & expressions to answer this common question!
Felix Mallard Lpsg
The TBM 930 Is Another Daher Masterpiece
Puretalkusa.com/Amac
Download Diablo 2 From Blizzard
Smite Builds Season 9
Garland County Mugshots Today
Portal Pacjenta LUX MED
Perc H965I With Rear Load Bracket
The Pretty Kitty Tanglewood
Latina Webcam Lesbian
Lagrone Funeral Chapel & Crematory Obituaries
Escape From Tarkov Supply Plans Therapist Quest Guide
211475039
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5791

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.