Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jake Casto
Jake Casto

Posted on • Edited on

     

Configuring PHP-FPM For High Network Traffic

php-fpm logo

Maintaining a constant response time on a server with high network traffic while using PHP is probably the hardest & most annoying thing I've done in my career. Switching from Apache to Nginx was a huge performance upgrade... that lasted for a solid half hour before the RPS (requests-per-second) was causing the stack to overflow once again.

I started browsing Stack Overflow & Google (devs best & worst friend/enemy) for answers, I found a few posts that showed how to fix the issue (PHP-FPM conf) but didn't explain anything. As a hands-on developer, I wanted to know how things worked.

Originally my PHP-FPM conf looked something like this

[www]user = apachegroup = apachelisten = 127.0.0.1:9000listen.allowed_clients = 127.0.0.1pm = dynamicpm.max_children = 200pm.start_servers = 20pm.min_spare_servers = 10pm.max_spare_servers = 20pm.max_requests = 1000
Enter fullscreen modeExit fullscreen mode

I started reading into whatpm = dynamic meant and I found this (in another conf file haha)

dynamic - the number of child processes are set dynamically based on the following directives. With this process management, there will be always at least 1 children.

Hold on... are all 200 children being spawned on startup? Are they always idle?

Yes & Yes (i think). I started viewing and recording memory usage of theapache pool usingps aux |grep apache. No matter how many requests were being processed (0 requests - 1000 requests) there were always 200 children alive. Don't get me wrong, I love kids but 200 at once for no reason is a bit much.

After spending a few hours screwing with my PHP-FPM conf and running stress tests I came up with this

[www]user = apachegroup = apachelisten = 127.0.0.1:9001listen.allowed_clients = 127.0.0.1pm = ondemandpm.max_children = 200pm.process_idle_timeout = 1spm.max_requests = 1000
Enter fullscreen modeExit fullscreen mode

I ran another stress test: 2000 RPS (requests-per-second) for one min and the average response time went from 1000MS to 120MS.

What happened?

PHP-FPM's PM (Pool Manager) was spawning 200 children on startup, even though those children were idle the PM was using unnecessary resources to manage the children. The switch fromdynamic toondemand allowed children to be spawned when needed and killed them after 1s of inactivity.

Feel free to critique this post, I have little knowledge of how PHP-FPM's pool manager works. I felt that it might be helpful to someone in a bind with PHP-FPM.

EDIT: Some info in this article is incorrect e.g I confuseddynamic withstatic spawning 200 children on startup. However, I still use this same setup on new servers and it performs so much better than any other config I've used.

Top comments(8)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
azazqadir profile image
Muhammad Azaz Qadir
  • Joined

Manually configuring PHP-FPM with nginx and apache is quite difficult for someone who doesn't have sysadmin experience. They might not even knowwhat is php-fpm. I think it is better to use some tool that automates this process or at least makes it easier.

CollapseExpand
 
jake profile image
Jake Casto
Co-Founder @ Proton, a ghost agency taking the Shopify Plus ecosystem by storm ~ Ecomm Wizard 🧙‍♀️ ~ I turn ideas into realities
  • Location
    New York, New York
  • Work
    Co-Founder @ Proton
  • Joined

This article wasn’t directed at users who don’t know what PHP-FPM is.

CollapseExpand
 
aliuadepoju profile image
Wuyi Adepoju
I design & code
  • Location
    Lagos
  • Work
    Product Designer at Natview Technology
  • Joined

Got curious with your comment, what are the automation tools?

CollapseExpand
 
jake profile image
Jake Casto
Co-Founder @ Proton, a ghost agency taking the Shopify Plus ecosystem by storm ~ Ecomm Wizard 🧙‍♀️ ~ I turn ideas into realities
  • Location
    New York, New York
  • Work
    Co-Founder @ Proton
  • Joined

^ I’d like to know them as well. I use Laravel Forge to provision the server and setup PHP-FPM but I still make configuration changes.

CollapseExpand
 
reedzhao profile image
reedzhao
  • Joined

Your article is full of factual errors aboutdynamic,ondemand,static. Please fix them.

RTFM:

static  - a fixed number (pm.max_children) of child processes;ondemand - no children are created at startup. Children will be forked when new requests will connect.dynamic - the number of child processes are set dynamically based on the following directives.
CollapseExpand
 
luissiqueira profile image
Luis Siqueira
  • Joined

What is your setup?
RAM and CPU?

CollapseExpand
 
jake profile image
Jake Casto
Co-Founder @ Proton, a ghost agency taking the Shopify Plus ecosystem by storm ~ Ecomm Wizard 🧙‍♀️ ~ I turn ideas into realities
  • Location
    New York, New York
  • Work
    Co-Founder @ Proton
  • Joined

Really depends on the project, I’d always run at least 4GB of RAM and 2 CPU’s. That works great for small-med projects, I believe the server that I referenced in this post had 18GB of RAM and 8 cpu’s I’m not 100% sure.

CollapseExpand
 
adam_kramer02 profile image
Adam_Kramer ( Test Bank and Solution Manual)

so

wht is the best value for

PHP-FPM Pool
Options Max Requests =

Process Idle =

Timeout Max Children=

??

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Co-Founder @ Proton, a ghost agency taking the Shopify Plus ecosystem by storm ~ Ecomm Wizard 🧙‍♀️ ~ I turn ideas into realities
  • Location
    New York, New York
  • Work
    Co-Founder @ Proton
  • Joined

More fromJake Casto

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp