PHP Integration
Nginx does not execute PHP natively. It hands PHP requests to PHP-FPM (FastCGI Process Manager) via a FastCGI socket. This module covers the complete connection from Nginx location block to PHP-FPM worker process.
How It Works
Nginx receives .php request
└── fastcgi_pass → PHP-FPM pool (via UNIX socket or TCP)
└── PHP-FPM worker forks/spawns
└── Executes the PHP script
└── Returns response to Nginx
└── Nginx sends to client
Using a UNIX socket instead of TCP is faster because it avoids the TCP stack entirely — prefer it unless PHP-FPM is on a different host.
What You Will Learn
- How to install PHP-FPM and connect it to Nginx
- What
fastcgi_paramsandfastcgi.confdo - How to configure PHP-FPM pools (
pmmodes, worker counts) - How to tune php.ini for production
- How to run PHP 8.1, 8.2, and 8.4 simultaneously for different sites
- How to enable and verify OPcache
Topics in This Module
- PHP-FPM Installation — Install, enable, and verify PHP-FPM on Debian and RHEL
- Connecting Nginx to PHP-FPM —
fastcgi_pass, socket vs TCP,fastcgi_paramssnippet - PHP-FPM Pool Configuration —
pmmodes (static/dynamic/ondemand), worker limits, socket path - php.ini Tuning — Memory, execution time, upload limits, error logging
- OPcache Configuration — Enable, memory sizing, JIT, preload
- Multiple PHP Versions — Run PHP 8.1 + 8.4 simultaneously, per-site version assignment
Best Practices
- Always use a UNIX socket for PHP-FPM (
/run/php/phpX.X-fpm.sock) on single-server setups - Set
pm = dynamicfor variable-load production sites - Enable OPcache — it is the single largest PHP performance improvement
- Set
opcache.revalidate_freq = 0in production (never check disk for changes) - Use separate PHP-FPM pools per site for isolation and resource control
Success Checkpoint
By the end of this module you should be able to install PHP-FPM, write a working fastcgi_pass location block, configure a pool, and verify PHP is executing correctly.