Article
Nginx and PHP-FPM for Legacy PHP Apps
Front legacy PHP applications with nginx and PHP-FPM using a correct front controller, try_files, SCRIPT_FILENAME, sockets, and upload timeouts.
Moving a legacy Apache/mod_php app to nginx + PHP-FPM is a common modernization step for performance and deployment consistency. Most failures are path and FastCGI parameter mistakes—not PHP business logic. Get SCRIPT_FILENAME, the socket permissions, and the front-controller try_files pattern right before tuning buffers.
MODERN — front controller pattern
server {
listen 443 ssl;
server_name example.com;
root /var/www/app/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php-fpm/app.sock;
}
}
Operational details
- Match socket user/group/mode to the nginx worker user
- Raise timeouts and body size for large uploads deliberately
- Log access/error separately from PHP-FPM logs for faster triage
- Do not leave directory listing or raw
.phpbackups under the root
Generate starter configs with Nginx PHP Generator and size pools with PHP-FPM Pool Calculator. Docs: nginx.org.
Related reading
- Composer Modernization Center Add Composer to legacy PHP projects, migrate includes to autoloading, set platform constraints, and replace a…
- PHP 5 to Modern PHP: Complete Incremental Migration Guide A deep, production-minded path from PHP 5.x codebases to supported PHP 8.x: removed extensions, charset, PDO,…
- PHP Security Modernization for Legacy Applications Upgrade inherited PHP security practices: prepared statements, password hashing, sessions, CSRF, XSS escaping…
- PHP 7 to PHP 8 Migration Guide Deep guide to PHP 8.0 breaking changes that matter for PHP 7 applications, with upgrade tactics through suppo…
- Inheriting a Legacy PHP Application A first-30-days playbook for developers handed an unfamiliar PHP codebase: runtime truth, risk triage, and sa…