Content · Web Development · PHP

Using PHP with Nginx and PHP-FPM: First Steps

An introductory Nginx/FPM site needs a Unix socket, a front controller and a correct root.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Admin

An introductory Nginx/FPM site needs a Unix socket, a front controller and a correct root.

What It Does and When to Use It


Route PHP through a correctly configured web-server-to-FPM boundary. The web server handles HTTP, the PHP worker executes scripts, and CLI has separate configuration and lifecycle.

Requirements, Inputs and Expected Behavior


Confirm the runtime, extension, environment variables, and permissions before running the examples in production. Verify the fastcgi_pass socket matches the FPM listen path.

Example 1: Basic Operation


Route PHP through a correctly configured web-server-to-FPM boundary.

location / { try_files $uri $uri/ /index.php$is_args$args; }
location = /index.php {
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root/index.php;
 fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}

Example 2: Behavior and Validation


Verify web execution and distinguish PHP-FPM from PHP CLI.

<?php
header('Content-Type: text/plain; charset=utf-8');
echo 'Running PHP ',PHP_VERSION,' via ',PHP_SAPI;

Practical Example: Application Use


Inspect the HTTP response and the worker service before investigating application code.

curl -sSI http://localhost/
php --ini
sudo systemctl status php8.4-fpm --no-pager

Important Behavior and Edge Cases


The second example highlights: Verify web execution and distinguish PHP-FPM from PHP CLI. The practical example adds: Inspect the HTTP response and the worker service before investigating application code.

Comparison with Related Approaches


ApproachWhat changes
Web server, worker and PHP CLIThe web server handles HTTP, the PHP worker executes scripts, and CLI has separate configuration and lifecycle.
Choosing safelyCheck the effective configuration in the SAPI serving requests before changing production.

Common Beginner Mistakes


MistakeCorrect approach
Checking the PHP CLI INI instead of FPMInspect phpinfo or configuration through the protected web SAPI.
Exposing worker status publiclyRestrict FPM diagnostics to localhost or a monitored private network.
Skipping operational constraintsVerify the fastcgi_pass socket matches the FPM listen path.

Key Takeaways


  • An introductory Nginx/FPM site needs a Unix socket, a front controller and a correct root.
  • Inspect the HTTP response and the worker service before investigating application code.
  • Check the effective configuration in the SAPI serving requests before changing production.
Share this resourceSend this page to someone who may find it useful.