PHP-FPM Security and Isolation: Separate Users, Not Just Pools

By Lars Nielsen ·

PHP-FPM security comes down to a few settings and one rule: separate Unix users, not separate pools, are what keep one PHP application from reading another's files. This guide goes through the settings that matter, quoting what PHP's own manual and the upstream config files say about each, including the protections the manual itself calls insufficient.

Updated 15 November 2026: rewritten against the PHP manual, PHP 8.4's upstream www.conf and php.ini-production, and the systemd documentation. The earlier version presented separate pools as an isolation boundary and suggested a systemd setting that, on its own, stops PHP-FPM from writing its socket and logs.

Pools are not the boundary. Users are.

PHP's manual is blunt about this, in its FPM configuration page: "Pools are not a security mechanism, because they do not provide full separation; e.g. all pools would use a single OPcache instance." (FPM configuration)

A pool gives an application its own process limits, socket and settings. What actually stops application A from reading application B's settings.php or .env is that A's PHP processes run as a different Unix user, and B's files aren't readable by that user. So the useful layout is one pool and one user per application:

; /etc/php/8.4/fpm/pool.d/app1.conf
[app1]
user = app1
group = app1
listen = /run/php/app1.sock
listen.owner = www-data      ; the web server's user
listen.group = www-data
listen.mode = 0660

Two details from the upstream www.conf comments:

  • user and group "can be used only if the master process running user is root". The usual packaged setup starts the master as root and drops to these users per pool.
  • For the socket, "read/write permissions must be set in order to allow connections from a web server". Owner and group default to the master's user, and the mode defaults to 0660. So the web server's user has to own the socket or be in its group.

A pool that runs as the same user as the web server (often www-data for both) gives up most of this: a compromised application can then read everything the web server can read.

Permissions: what the PHP user may write

With a dedicated user, make the code owned by a different account (the deploy user) and only readable by the PHP user. Then give write access only where the application needs it: upload and cache directories, sessions, temporary files. If the PHP user can't write to the code, a file-upload bug can't turn into a modified index.php. For Drupal, the directories that must be writable are documented in its file permissions guide.

security.limit_extensions

This setting limits which files FPM will execute as PHP. The upstream comment explains why it exists: "This can prevent configuration mistakes on the web server side. You should only limit FPM to .php extensions to prevent malicious users to use other extensions to execute php code." The manual gives the default as .php .phar.

security.limit_extensions = .php

It only protects requests that reach FPM. If the web server never sends a .php request to FPM, for example a virtual host with a document root but no location ~ \.php$ block, nginx serves the PHP source as a plain file, and no FPM setting can help. Any server block that points at a PHP document root needs either a PHP handler or no root at all (a pure redirect).

Environment variables: clear_env

clear_env defaults to yes, which "prevents arbitrary environment variables from reaching FPM worker processes". Only the env[…] entries in the pool file get through. Setting it to no "will make all environment variables available to PHP code via getenv(), $_ENV and $_SERVER", including whatever the service manager started FPM with. Leave it at yes and pass what the application needs explicitly.

open_basedir and disable_functions: safety nets, not walls

Both are worth knowing, and the manual warns against relying on either (core php.ini directives):

  • open_basedir limits the paths PHP's file functions may open, and resolves symlinks before checking. But "open_basedir is just an extra safety net, that is in no way comprehensive, and can therefore not be relied upon when security is needed". It "does not restrict access to Unix domain sockets" either: PHP can still connect to a socket such as /var/run/docker.sock. It also disables the realpath cache.
  • disable_functions (for example exec,passthru,shell_exec,system,proc_open,popen) has to be set in php.ini, and in a pool file only through php_admin_value, where it appends to the php.ini list. The manual warns that it "can be circumvented and should not be considered a sufficient security measure for shared hosting environments". php.ini-production leaves it empty.

Use them to make an exploit's life harder, after the user separation above, not instead of it.

Limits that matter when something goes wrong

  • pm.max_children caps the number of workers, and so the memory a flood of requests can take.
  • pm.max_requests restarts each worker after that many requests. The upstream comment: "This can be useful to work around memory leaks in 3rd party libraries". The default is 0 (never).
  • request_terminate_timeout kills a worker that runs too long. Upstream says to use it "when the 'max_execution_time' ini option does not stop script execution for some reason". The default is 0 (off).
  • request_slowlog_timeout plus slowlog dump a PHP backtrace for slow requests. Both are off by default, and the backtrace shows you where a slow or abusive request spends its time.

systemd hardening, with the catch

The systemd documentation recommends ProtectSystem= "for all long-running services". There's a catch for PHP-FPM, though. With ProtectSystem=strict, "the entire file system hierarchy is mounted read-only, except for the API file system subtrees /dev/, /proc/ and /sys/". On its own, that would stop FPM from creating its socket, writing its logs, and stop PHP from writing sessions and uploads. So it only works together with ReadWritePaths= for those directories. A drop-in along these lines (paths depend on your distribution and sites), applied with systemctl edit php8.4-fpm:

[Service]
PrivateTmp=true
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/run/php /var/log/php-fpm /var/lib/php/sessions /srv/app1/files

I haven't run this exact drop-in. Check systemctl status and the FPM log after a restart, and test an upload. NoNewPrivileges=true stops PHP code from gaining privileges through setuid programs, such as sudo called through exec(). PrivateTmp=true gives FPM its own /tmp, which also means its temporary files are no longer in the system's /tmp. The settings are described in systemd.exec(5).

A short checklist

  1. One Unix user per application, code owned by someone else, writable only where needed.
  2. One pool per application, socket mode 0660, owned by or shared with the web server's group.
  3. security.limit_extensions = .php, and a PHP handler (or no document root) in every server block that points at PHP code.
  4. clear_env = yes. Pass secrets explicitly with env[…], and keep the pool file readable by root only.
  5. pm.max_children, request_terminate_timeout and a slowlog set to something deliberate.
  6. Then open_basedir, disable_functions and systemd hardening as extra layers, tested after every change.

Related reading

Filed under

Leave a comment

Comments are reviewed before they appear.

Basic HTML allowed: links, emphasis, code and lists. Web addresses become links automatically.