Skip to content
VDS · Websites and web servers

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:

TierMemoryReasonable pm.max_children
VDS-12 GB5
VDS-24 GB10
VDS-38 GB20
VDS-524 GB60
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

SymptomCauseWhat to do
502 Bad GatewayWrong socket path or the service isn't runningCheck fastcgi_pass against the actual file in /run/php/
504 Gateway Time-outThe script runs longer than the timeoutRaise fastcgi_read_timeout and max_execution_time
The browser downloads the file instead of executing itThe location ~ \.php$ block didn't matchCheck the order of the location blocks and run nginx -t
server reached pm.max_children in the logNot enough processesRaise 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 root account in your CMS config is a direct path to losing all your databases at once.