Maarten van Lieshout
Blog

article

Running PHP safely behind Caddy: php-fpm pools vs FrankenPHP

A practical comparison of php-fpm and FrankenPHP behind Caddy, covering site isolation, worker mode, file permissions, SELinux and encoded PHP apps, from someone replacing Plesk on a small VPS.

phpcaddyhostingsecuritylinux

For years my default stack was Plesk with nginx and php-fpm. It works, but for a handful of small websites it’s a lot of machinery. Plesk wants resources, and the licence gets more expensive every year. In 2021 a VPS licence for Web Pro (30 domains) cost €11 a month; the 2026 price list says €25.49. Even the smallest edition, Web Admin, went from €7.33 to €14.29. That’s more than double in five years.

And the server underneath isn’t getting cheaper either. Hetzner, long the go-to for affordable VPSes, raised its prices on 1 April 2026 and again on 15 June 2026, blaming hardware and memory costs. Hardware plus licences makes every new server noticeably more expensive than it was a year ago. That’s the main reason I think a lean setup is the better choice for small sites. So I started looking for something leaner to replace one of those servers, and kept reading good things about Caddy. It turned out to run comfortably on a low-spec VPS.

I already run Node apps behind Caddy with PM2 keeping them alive, and honestly that combination feels more solid than Plesk + nginx ever did. Automatic TLS, a config file you can read in one go, and a process manager that just restarts whatever falls over. The open question was PHP.

Search for “PHP on Caddy” and you’ll mostly find FrankenPHP. I assumed that was simply how you do it. After digging in, I don’t think it’s the right default, at least not if more than one site lives on the server. Caddy talks to php-fpm just as happily as nginx does.

Who does the cooking

The way I think about it: php-fpm is a row of separate kitchens, one per site. Each has its own cook (a Unix user) and its own pantry (a directory tree). Caddy is the waiter carrying orders to the right kitchen.

FrankenPHP puts the cook inside the waiter. PHP runs as threads inside the Caddy process, so everything that process runs shares one Unix user. FrankenPHP can serve many sites just fine. What it can’t do within one instance is separate them at the OS level. If you want that, you need one instance per site.

Option 1: Caddy + php-fpm, one pool per site

This is the model Plesk uses too, minus the panel. Paths below are for AlmaLinux with Remi’s PHP packages. On Debian/Ubuntu the pools live in /etc/php/8.3/fpm/pool.d/ and the sockets in /run/php/.

; /etc/php-fpm.d/site1.conf
[site1]
user = site1
group = site1
listen = /run/php-fpm/site1.sock
listen.owner = caddy
listen.group = caddy
listen.mode = 0660

pm = ondemand
pm.max_children = 10
pm.process_idle_timeout = 30s

security.limit_extensions = .php
php_admin_value[open_basedir] = /var/www/site1:/tmp
php_admin_value[disable_functions] = exec,passthru,shell_exec,system,proc_open,popen
php_admin_value[upload_tmp_dir] = /var/www/site1/tmp
php_admin_value[session.save_path] = /var/www/site1/sessions
php_admin_flag[expose_php] = off

pm = ondemand matters on a small VPS: a site without visitors uses no PHP processes at all. Do the maths on pm.max_children, though. Ten per site sounds modest, but ten sites means up to a hundred PHP processes under load. Size it to the RAM you actually have.

The Caddyfile for that site:

site1.example {
    root * /var/www/site1/public

    # Never execute PHP from the uploads directory (WordPress)
    @uploads_php path_regexp (?i)^/wp-content/uploads/.*\.(php|phtml|phar)$
    respond @uploads_php 403

    # Dotfiles and dot-directories at any depth (.env, .git), except .well-known
    @dotfiles {
        path_regexp /\.
        not path /.well-known /.well-known/*
    }
    respond @dotfiles 404

    # Dumps, logs, backups and Composer files
    @sensitive path_regexp (?i)(\.(sql|log|bak|old|orig|swp)$|^/vendor/|^/composer\.(json|lock)$)
    respond @sensitive 404

    php_fastcgi unix//run/php-fpm/site1.sock
    file_server
    encode zstd gzip

    header {
        -Server
        X-Content-Type-Options nosniff
        Referrer-Policy strict-origin-when-cross-origin
    }
}

Coming from nginx, what surprised me is how much php_fastcgi handles on its own: try_files, path splitting, the index file. The usual fastcgi_split_path_info puzzles don’t come up. I keep security.limit_extensions in the pool anyway, as a second line of defence.

Two things about those matchers caught me out. Caddy’s path matcher is case-insensitive, but path_regexp uses Go’s regular expressions, which are case-sensitive unless you add (?i). Without it, shell.PHP would slip through. And a wildcard in the middle of a path pattern only matches a single path segment, so /*/.* catches /wp-content/.env but not /wp-content/plugins/foo/.env. That’s why the dotfile rule uses a regex.

Don’t trust a block list until you’ve tested it:

for p in /.env /.git/config /wp-content/plugins/foo/.env /backup.sql \
         /wp-config.php.bak /vendor/composer/installed.json \
         /wp-content/uploads/x.PHP /.well-known/security.txt; do
  printf '%s %s\n' "$(curl -s -o /dev/null -w '%{http_code}' "https://site1.example$p")" "$p"
done

Every blocked path should return 403 or 404. Requests under .well-known skip the rule, so their status depends on whether the file exists. A 502 means the request reached PHP, so that path isn’t blocked.

These patterns catch the usual suspects, not everything. Keep secrets out of the web root in the first place (more on that below).

Certificates are handled by Caddy itself, so certbot can go.

Permissions

My rule is that the app can’t overwrite its own code. Code is owned by root:site1 (0640 for files, 0750 for directories). Only public/wp-content/uploads, tmp and sessions are writable by the pool user. If a plugin gets exploited, the attacker ends up in those few writable directories, and PHP won’t run there.

Caddy runs as caddy and has to read the static files. The quick way is adding it to the site’s group (usermod -aG site1 caddy), which is what Plesk does with its psaserv group. The downside: Caddy can then read everything the group can, including every site’s config files and database credentials.

Caddy doesn’t need that. It only serves files from public/, and for PHP it just checks whether the script exists, which only takes permission to traverse the directories. So give it exactly that, with ACLs:

setfacl -m u:caddy:x /var/www/site1                 # traverse only, no listing
setfacl -R -m u:caddy:rX /var/www/site1/public      # read public files
setfacl -R -d -m u:caddy:rX /var/www/site1/public   # same for new uploads

That still leaves wp-config.php, which sits in the web root. WordPress also looks for it one directory up, so move it to /var/www/site1/ and Caddy can’t read it any more. Same idea for .env files in other frameworks.

open_basedir in the pool is a useful extra fence, but not a security boundary. The real isolation comes from separate users, these permissions and SELinux.

SELinux

I run AlmaLinux with SELinux enforcing, so the labels need to match:

semanage fcontext -a -t httpd_sys_content_t    "/var/www/site1(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site1/public/wp-content/uploads(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/site1/(tmp|sessions)(/.*)?"
restorecon -Rv /var/www/site1

These labels are for php-fpm, which runs confined as httpd_t. Check what Caddy itself runs as with ps -eZ | grep caddy. A Caddy installed from a package or COPR often runs unconfined, and then SELinux doesn’t restrict Caddy at all.

If something is blocked, ausearch -m AVC -ts recent | audit2why explains why. Please don’t reach for setenforce 0.

Option 2: FrankenPHP

To be fair to FrankenPHP: for a single app you control, a Laravel or Symfony project of your own, it’s very attractive. Worker mode keeps the app booted in memory, so you skip the bootstrap on every request. You also get early hints and can ship the whole thing as one binary.

The catch with worker mode is that PHP stops starting every request from a clean slate. FrankenPHP resets most request superglobals, but not $_ENV: change it during a request and the change sticks. Statics, singletons and in-memory caches in your own code survive between requests too. Frameworks with explicit support (Laravel Octane, Symfony Runtime) mostly take care of that. Older or hand-written code usually doesn’t, and then data from one request can show up in the next.

You can run FrankenPHP for several sites: one instance per site, each in a container with a non-root user, and a central Caddy in front. It works. But you’ve basically rebuilt php-fpm pools out of containers, with more moving parts.

Encoded sources: php-fpm, no contest

The app I needed this for is partly encoded, which settled it. FrankenPHP needs a thread-safe (ZTS) build of PHP, and every extension has to match that build. ionCube does ship thread-safe loaders (the _ts.so files), so it isn’t impossible. But a loader that exists for ZTS PHP isn’t the same as a loader tested against FrankenPHP’s embedded, multi-threaded setup. And the vendor of an encoded app usually tests against standard NTS PHP. A closed-source blob is the last thing I want running on an untested combination.

With php-fpm it’s all standard: stock NTS PHP, the regular loader, Caddy in front. Make sure the loader loads before OPcache, hence the 00- prefix:

; /etc/php.d/00-ioncube.ini
zend_extension = ioncube_loader_lin_8.3.so

And check which PHP versions the vendor supports before you upgrade. Encoded apps tend to lag behind, and PHP itself breaks things on purpose between versions - worth reading before you bump anything in production.

So which one?

Situation My choice
Several sites that need to be isolated from each other Caddy + php-fpm, one pool per site
WordPress and other traditional PHP apps Caddy + php-fpm
Encoded apps with vendor-tested PHP requirements Caddy + php-fpm
Your own app, worker-compatible code FrankenPHP, worker mode after testing
Your own app, older code FrankenPHP in classic mode (no state between requests), or php-fpm

For me it’s Caddy in front, PM2 for the Node apps and php-fpm for PHP. Small VPS, no licence fees.