Linux cgroups v2 and Systemd Slices for PHP-FPM Resource Control

Limit PHP-FPM, cron and imports with systemd slices and cgroups v2

On a server that runs several WordPress sites, one site’s runaway import or a crawler storm on another can take all CPU and memory, and every site becomes slow. Linux cgroups v2 can cap a group of processes, and systemd exposes them through simple unit settings: CPUQuota, MemoryHigh, MemoryMax, IOWeight. Put each site’s PHP-FPM pool and its background jobs into their own slice, and one site can no longer starve the others.

Problem: A VPS hosts four client sites with one shared PHP-FPM service. When one shop runs its nightly product import with WP-CLI, the other three sites time out, and once the import used so much memory that the OOM killer stopped MySQL.

Solution: Run one PHP-FPM service per site (a templated systemd unit), place each in a slice with CPUQuota and MemoryHigh/MemoryMax, start imports and cron jobs with systemd-run --slice under the same limits, and check usage with systemd-cgtop.

Check that cgroups v2 is in use

stat -fc %T /sys/fs/cgroup/        # cgroup2fs = v2
systemctl --version | sed -n 1p

Current Debian, Ubuntu, RHEL and their derivatives use cgroups v2 by default. On v1 systems, most of the settings below exist under other names and behave differently.

A slice per site

# /etc/systemd/system/site-shop.slice
[Unit]
Description=Resources for shop.example.com

[Slice]
CPUQuota=150%
MemoryHigh=1500M
MemoryMax=2G
IOWeight=100

CPUQuota=150% allows one and a half CPU cores in total for everything in the slice. MemoryHigh is the soft limit: above it, the kernel reclaims memory from the group aggressively and slows it down. MemoryMax is the hard limit: above it, the OOM killer acts inside the group only, so a runaway process in this slice is killed instead of MySQL. IOWeight sets the share of disk bandwidth relative to other groups when the disk is busy.

PHP-FPM per site in its slice

# /etc/systemd/system/php-fpm@.service
[Unit]
Description=PHP-FPM for %i
After=network.target

[Service]
Type=notify
Slice=site-%i.slice
ExecStart=/usr/sbin/php-fpm8.3 --nodaemonize --fpm-config /etc/php/8.3/fpm/sites/%i.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now php-fpm@shop

Each site gets its own FPM master with its own pool config (and its own Unix socket for Nginx), and Slice=site-%i.slice puts the whole process tree into the slice. Separate pools per site are good practice anyway: own user, own open_basedir, own limits. Pool sizing inside the slice: tune the PHP-FPM pool.

Imports and cron jobs in the same slice

sudo systemd-run --slice=site-shop.slice --uid=www-shop --gid=www-shop \
    --property=MemoryMax=1G --wait --collect \
    /usr/local/bin/wp --path=/var/www/shop/public ha sync

systemd-run starts the command as a transient unit inside the slice, with an extra memory limit for this job. --wait returns the command’s exit code, which is what a cron job needs. The WP-CLI import now shares the shop’s CPU and memory budget instead of taking the server’s.

Watch it

systemd-cgtop                         # CPU, memory, IO per slice, like top
systemctl status site-shop.slice      # memory use and the processes in it
journalctl -k | grep -i 'memory cgroup out of memory'

An OOM kill inside a slice appears as “Memory cgroup out of memory” in the kernel log and names the slice, which tells you which site needs more memory or a fix.

NOTE: Limits trade one problem for another: a site at its CPUQuota becomes slow instead of making all sites slow. Size the slices from measurements, and leave headroom for MySQL and Redis outside of them. The server-wide view of memory and the OOM killer: the OOM killer and swap on a WordPress server.