PHP-FPM: PHP sites
Installing PHP-FPM on VDS, integrating it with Nginx, choosing the number of processes for your tier’s memory, and troubleshooting 502 and 504 errors.
Updated August 23, 2026
PHP-FPM is a separate service that executes PHP code; the web server only passes requests to it. This is how WordPress, Laravel, Bitrix and almost everything else on PHP works.
Installation
apt update
apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip
systemctl enable --now php8.3-fpm
On AlmaLinux and CentOS Stream, use dnf install -y php-fpm php-mysqlnd ...; the service is called php-fpm. To check the exact version, run:
php -v
systemctl list-units | grep fpm
Connecting to Nginx
server {
listen 80;
server_name example.ru;
root /var/www/example.ru/public;
index index.php index.html;
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; }
}
nginx -t && systemctl reload nginx
echo '<?php phpinfo();' > /var/www/example.ru/public/info.php
Open /info.php, make sure PHP works, and delete the file right away: it exposes the version, paths and module list.
How many processes to keep
Each PHP worker process eats 40–120 MB. Size it by tier, leaving memory for the database and the system. This is configured in /etc/php/8.3/fpm/pool.d/www.conf:
| Tier | Memory | Reasonable pm.max_children |
|---|---|---|
| VDS-1 | 2 GB | 5 |
| VDS-2 | 4 GB | 10 |
| VDS-3 | 8 GB | 20 |
| VDS-5 | 24 GB | 60 |
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
pm.max_requests restarts a process after N requests, so memory leaks in the code don't pile up until the end of the week.
Useful php.ini values for FPM: memory_limit = 256M, upload_max_filesize = 64M, post_max_size = 64M, opcache.enable = 1.
After making changes: systemctl reload php8.3-fpm.
Common errors
| Symptom | Cause | What to do |
|---|---|---|
502 Bad Gateway | Wrong socket path or the service isn't running | Check fastcgi_pass against the actual file in /run/php/ |
504 Gateway Time-out | The script runs longer than the timeout | Raise fastcgi_read_timeout and max_execution_time |
| The browser downloads the file instead of executing it | The location ~ \.php$ block didn't match | Check the order of the location blocks and run nginx -t |
server reached pm.max_children in the log | Not enough processes | Raise the limit if memory allows, otherwise move up a tier |
Logs: /var/log/php8.3-fpm.log and /var/log/nginx/error.log.
Create the site's database under a separate user with rights only to its own schema — users and grants. Using the
rootaccount in your CMS config is a direct path to losing all your databases at once.