Log inSign up

Isolate websites with their own PHP-FPM pools

For every site, create a Linux user, a pool file that runs PHP as that user on its own socket, and a web server block that sends that site to that socket. Add open_basedir, a private session and temp folder, and pm limits so each site has a fixed memory budget. A compromised or busy site then stays inside its own folder and its own workers.

Topic: PHP and web stacks · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Why run one pool per site?

With a single shared pool every PHP script runs as the same user, so a bug in one site can read the configuration files, database passwords and uploads of every other site on the server. Separate pools change that boundary to the operating system: each site's PHP processes run as their own account and file permissions do the rest.

Pools also give you per-site resource control. A crawler hammering one shop fills only that shop's workers, while the blog next to it keeps answering. You also get separate slow logs and status pages, which makes it much easier to see which site is causing load. If you are still setting up the base stack, start with LEMP or LAMP and come back here.

How do you create a user and folder for each site?

Create a system account with no login shell, then make the web server a group member of the site folder so it can serve static files.

sudo useradd --system --home-dir /var/www/shop.example.net --shell /usr/sbin/nologin shop
sudo mkdir -p /var/www/shop.example.net/{public,tmp,sessions,logs}
sudo chown -R shop:www-data /var/www/shop.example.net      # Rocky/Alma: shop:nginx or shop:apache
sudo find /var/www/shop.example.net -type d -exec chmod 750 {} +
sudo find /var/www/shop.example.net -type f -exec chmod 640 {} +
sudo chmod 700 /var/www/shop.example.net/tmp /var/www/shop.example.net/sessions

The web server can read public files through the group, other site users cannot enter the folder at all, and only the pool user can open its sessions and temp files. To deploy with your own account, add deploy to the site group or use sudo -u shop for writes, as explained in file permissions.

What goes in the pool file?

A pool file sets the user, the socket and its permissions, the process manager and the PHP limits for that site.

Save as /etc/php/8.3/fpm/pool.d/shop.conf on Debian or Ubuntu, or /etc/php-fpm.d/shop.conf on Rocky or Alma:

[shop]
user = shop
group = shop
; Rocky/Alma: listen = /run/php-fpm/shop.sock and owner/group nginx or apache
listen = /run/php/php8.3-shop.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
pm.status_path = /fpm-status

request_slowlog_timeout = 5s
slowlog = /var/www/shop.example.net/logs/slow.log
php_admin_value[error_log] = /var/www/shop.example.net/logs/php-error.log
php_admin_flag[log_errors] = on

php_admin_value[open_basedir] = /var/www/shop.example.net/:/usr/share/php/
php_admin_value[upload_tmp_dir] = /var/www/shop.example.net/tmp
php_admin_value[sys_temp_dir] = /var/www/shop.example.net/tmp
php_admin_value[session.save_path] = /var/www/shop.example.net/sessions
php_admin_value[session.gc_probability] = 1
php_admin_value[disable_functions] = exec,passthru,shell_exec,system,proc_open,popen
php_admin_value[memory_limit] = 256M

Values set with php_admin_value cannot be changed by the site's own code through ini_set(), which is why they suit security settings. Turning on session.gc_probability matters on Debian and Ubuntu, where the packaged cleanup job only sweeps the default session folder. Leave disable_functions out for apps that need to run shell commands, such as some deploy tools.

How do you choose between static, dynamic and ondemand?

Use ondemand for quiet sites, dynamic for steady traffic and static only when one site owns the server.

  • ondemand starts workers on the first request and stops them after pm.process_idle_timeout (10s is typical). Idle sites cost almost no memory, at the price of a small delay on the first hit.
  • dynamic keeps between pm.min_spare_servers and pm.max_spare_servers idle workers ready, up to pm.max_children in total.
  • static keeps exactly pm.max_children workers alive, which gives the most predictable latency.

Size pm.max_children from measured memory. Check the average resident size of a pool's workers under normal load:

ps -o rss= -u shop | awk '{s+=$1; n++} END {if (n) print n " workers, avg " s/n/1024 " MB"}'
free -m

Then divide the RAM you want to give that site by the average. For example, 600 MB for a shop whose workers average 70 MB gives about 8 children. Add up max_children times worker size for every pool and keep the total below the RAM left after the database and web server; pm.max_requests recycles workers so slow memory leaks do not build up.

How do you connect Nginx or Apache to each pool?

Each virtual host names its own socket, so requests for a domain only ever reach that domain's pool.

# Nginx server block for shop.example.net
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-shop.sock;
}
location = /fpm-status {
    allow 127.0.0.1;
    deny all;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /fpm-status;
    fastcgi_param SCRIPT_NAME /fpm-status;
    fastcgi_pass unix:/run/php/php8.3-shop.sock;
}
# Apache virtual host for shop.example.net (needs proxy_fcgi)
<FilesMatch "\.php$">
    SetHandler "proxy:unix:/run/php/php8.3-shop.sock|fcgi://localhost"
</FilesMatch>

On Debian and Ubuntu enable the modules with sudo a2enmod proxy_fcgi setenvif. Rocky and Alma load them already. Base server block layouts are in the Nginx and Apache guides.

What extra steps does SELinux need on Rocky or Alma?

Label the folders PHP must write to, because the default label for /var/www is read-only for web processes.

sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/shop.example.net/(tmp|sessions|logs)(/.*)?'
sudo restorecon -Rv /var/www/shop.example.net
ls -Z /var/www/shop.example.net

If an upload folder lives under public, add it to the same rule. Denials show up in sudo ausearch -m avc -ts recent. More background is in SELinux basics.

How do you apply and test a new pool?

Test the FPM configuration, restart the service and prove the pool runs as the right user and cannot leave its folder.

sudo php-fpm8.3 -t && sudo systemctl restart php8.3-fpm    # Rocky/Alma: php-fpm -t, restart php-fpm
ls -l /run/php/php8.3-shop.sock
ps -eo user,cmd | grep 'pool shop'

sudo -u shop tee /var/www/shop.example.net/public/who.php >/dev/null <<'EOF'
<?php
var_dump(@file_get_contents('/etc/passwd'));
EOF
curl -s http://shop.example.net/who.php
sudo rm /var/www/shop.example.net/public/who.php
curl -s http://127.0.0.1/fpm-status -H 'Host: shop.example.net'

The ps line should show the pool workers running as shop, and the script should print bool(false), showing that open_basedir blocks files outside the site. The status page lists active and idle workers plus the max children reached counter, which tells you when the pool is too small.

How do you troubleshoot pool problems?

Most pool faults are socket permissions, an exhausted worker limit or a path missing from open_basedir.

  • 502 and "Permission denied" in the Nginx log: listen.owner or listen.group does not match the web server user.
  • "server reached pm.max_children setting" in journalctl -u php8.3-fpm: raise the limit if RAM allows, or find slow requests in the slow log.
  • "open_basedir restriction in effect": add the needed path, such as a shared library folder, to the pool and restart.
  • Sessions lost after login: the session folder is not writable by the pool user.

When the sum of all pools no longer fits the plan, move the busiest site to its own server or a larger plan; usage monitoring helps you see the trend first.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4several isolated sites on one server2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8many pools with busy shops4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about Separate PHP-FPM pools per site

How many pools can one server run?

There is no fixed limit. With pm = ondemand an idle pool uses only the master process memory, so dozens of quiet sites fit; the real limit is the sum of busy workers.

Is open_basedir a full sandbox?

No. It restricts PHP file functions but not programs started from PHP, which is why the separate Linux user and disabled exec functions matter more. Together they give a solid boundary for shared hosting on one VPS.

Do I need a separate PHP-FPM service for each pool?

No. One master process per PHP version manages all its pools. A restart reloads every pool of that version, so keep each pool file valid.

Can pools use different PHP versions?

Yes. Put a pool in the folder of the version it should use, such as /etc/php/7.4/fpm/pool.d/, and point the site at that socket. See older PHP versions.

Should I delete the default www pool?

Keep it while anything still uses it. Once every site has its own pool, you can rename www.conf to www.conf.disabled, but leave at least one pool per FPM version or the service will not start.

Should each site also get its own database user?

Yes. Pair each pool user with a database account limited to that site's schema, as shown in MySQL users and privileges.