PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
145: PHP-FPM Configuration
Up until now, we've mostly let the default www pool handle everything. That works fine for a blog or a small app, but in production, that's a recipe for disaster. If one heavy script—like a PDF generator or a massive CSV export—starts hogging all the PHP-FPM workers, your entire site goes down. You get those dreaded 504 Gateway Timeouts for every single user, even the ones just trying to load the homepage.
I've seen this happen more times than I care to admit. The fix is to create dedicated pools. Think of it as giving your "heavy" tasks their own lane on the highway so they don't cause a traffic jam for everyone else.
Isolating the Heavy Lifters
Let's say we have a specific set of scripts for generating monthly reports. Instead of letting them run in the default pool, I'm going to create a new pool configuration file. I usually put these in /etc/php/8.x/fpm/pool.d/. I'll call mine reports.conf.
[reports]
user = www-data
group = www-data
listen = /run/php/php8.2-fpm-reports.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
Notice how I'm limiting pm.max_children to 5. I'm doing this intentionally. I don't want my report generator to spin up 50 processes and eat all the server's RAM, which would trigger the OOM (Out Of Memory) killer and possibly take down MySQL along with it.
The "502 Bad Gateway" Moment
Here is where I usually mess up the first time—and I did it just now while testing this. I restarted PHP-FPM, refreshed my report page, and got a 502 Bad Gateway. I spent five minutes checking the PHP logs, but there was nothing there. Why? Because the request never even reached PHP.
I forgot that Nginx doesn't magically know about my new pool. Nginx is still trying to send everything to the default php8.2-fpm.sock. I need to tell Nginx to route requests for the /reports URI to my new socket.
I'll jump into my Nginx site config and add this block:
location /reports {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm-reports.sock;
}
After a systemctl reload nginx, everything clicks into place. Always remember: if you change the listen directive in FPM, you must update the fastcgi_pass in your web server config to match.
Hunting Down the Bottlenecks
Now that the report pool is isolated, I want to know why some reports are taking so long. Instead of guessing or peppering my code with microtime() calls, I'll use the PHP-FPM slow log. This is one of the most underrated features of FPM.
I'll add these two lines to my reports.conf pool:
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/reports.slow.log
By setting this to 5 seconds, PHP-FPM will take a "snapshot" of the call stack for any request that takes longer than 5 seconds to execute. When a user complains a report is hanging, I don't have to guess; I can just tail that log and see exactly which function or database query is the culprit.
One quick tip: make sure the directory /var/log/php-fpm/ actually exists and is writable by the www-data user, otherwise FPM will fail to start or simply won't log anything.
📋 Practical Task
Exercise: Configuring a Dedicated Pool for an Image Processing Queue
You have a web application where users upload high-resolution images that need to be resized. Currently, these are handled by the main www pool, causing the rest of the site to lag during uploads.
Your Task: Create a PHP-FPM pool configuration for a new pool named [images]. Your configuration must meet the following requirements:
- Use a Unix socket located at
/run/php/php-fpm-images.sock. - Set the process manager to
static(since image processing is predictable and resource-heavy). - Limit the number of children to exactly 3 to prevent CPU exhaustion.
- Enable a slow log that triggers after 10 seconds, saving logs to
/var/log/php-fpm/images.slow.log. - Ensure the socket is owned by the
www-datauser and group.
Write out the full content of the images.conf file as it would appear in the pool.d directory.
There are no comments for now.