Sub topic 7
Connecting Nginx to PHP-FPM with FastCGI
Assume you know basic Linux commands, file permissions, HTTP status codes, and how to edit files on Debian or Ubuntu. You also need a PHP application stored on the server.
How Nginx and PHP-FPM work together
Nginx is good at accepting connections and serving files. It does not execute PHP code itself. PHP-FPM is a service that keeps PHP worker processes ready to execute PHP scripts.
FastCGI is the protocol Nginx uses to send a request to PHP-FPM. The request usually travels like this:
Nginx chooses the FastCGI path when the requested URI ends in .php. PHP-FPM receives parameters such as the script filename, query string, request method, and HTTP headers. It executes the script and returns the generated response.
PHP-FPM uses pools. A pool is a group of PHP worker processes with its own user, limits, and listening endpoint. A default installation commonly provides a socket such as /run/php/php8.3-fpm.sock.
Configure the server block
Create or edit a site configuration such as /etc/nginx/sites-available/example. This example serves files from /var/www/example/public.
server {
listen 80;
server_name example.com;
root /var/www/example/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.(?!well-known) {
deny all;
}
}The root directive identifies the public directory. The try_files directive serves an existing file or directory. If neither exists, it sends the request to the application’s front controller, usually index.php.
The include file contains distribution-specific FastCGI parameters. These parameters include the script filename and help prevent Nginx from passing a nonexistent script to PHP-FPM.
The fastcgi_pass directive identifies PHP-FPM. Replace php8.3-fpm.sock with the socket installed on your server. Check available sockets with:
ls /run/php/Enable the site and verify both services:
sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example
sudo systemctl enable --now php8.3-fpm
sudo nginx -t
sudo systemctl reload nginxUse the PHP-FPM version installed on your system in the service command.
Permissions and common failures
Nginx must be able to read the public files. PHP-FPM must be able to read the PHP scripts and write only to directories that require application state, such as an upload or cache directory.
| Error | Likely cause | First check |
|---|---|---|
| 502 Bad Gateway | Nginx cannot reach PHP-FPM | Socket path and PHP-FPM status |
| 403 Forbidden | Permission or access-rule problem | File and directory permissions |
| 404 Not Found | Wrong root or missing route fallback | `root` and `try_files` |
Check recent messages after a failed request:
sudo systemctl status php8.3-fpm
sudo tail -n 20 /var/log/nginx/error.log
sudo journalctl -u php8.3-fpm -n 20 --no-pagerA 502 error often means that the socket path is wrong, PHP-FPM is stopped, or the socket permissions do not allow Nginx to connect. A 403 may result from a directory lacking execute permission or from the hidden-file denial rule. A 404 may mean that the requested script is not under root, or that the application needs the try_files fallback.
Exercise
Nginx returns 502 after you configured fastcgi_pass unix:/run/php/php8.3-fpm.sock;. What should you check first?
Show answer
Check whether PHP-FPM is running and whether /run/php/php8.3-fpm.sock exists. If the installed PHP version uses another socket name, update fastcgi_pass and reload Nginx.