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 .php backups under the root

Generate starter configs with Nginx PHP Generator and size pools with PHP-FPM Pool Calculator. Docs: nginx.org.

Related reading