Skip to main content

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_params and fastcgi.conf do
  • How to configure PHP-FPM pools (pm modes, 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


Best Practices

  • Always use a UNIX socket for PHP-FPM (/run/php/phpX.X-fpm.sock) on single-server setups
  • Set pm = dynamic for variable-load production sites
  • Enable OPcache — it is the single largest PHP performance improvement
  • Set opcache.revalidate_freq = 0 in 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.