Skip to content

PHP-FPM graceful reload and zero-downtime restarts

PHP-FPM supports signal-based graceful reloads that replace workers without dropping in-flight requests. Learn the signals, the timing, and where it can still go wrong.

PHP-FPM’s reload mechanism replaces configuration and workers without closing the Unix socket or dropping in-flight requests — when it works correctly. The mechanism relies on Unix signals sent to the master process, and the behaviour differs meaningfully between a reload (SIGUSR2), a graceful stop (SIGQUIT), and a hard restart (SIGTERM). Operators who treat systemctl restart php-fpm and systemctl reload php-fpm as equivalent will eventually cause a brief but real outage during a daytime deployment.

How to verify

Confirm the master PID and test the reload path before using it in a deployment script:

systemctl show php8.3-fpm --property=MainPID
cat /run/php/php8.3-fpm.pid
# Confirm no listen queue during a reload
watch -n0.5 "curl -s 127.0.0.1/status | grep -E 'listen queue|active|total'"
# Test in a separate terminal
sudo systemctl reload php8.3-fpm
# The socket file should persist through the reload
ls -la /run/php/php8.3-fpm.sock

What’s happening

PHP-FPM’s master process interprets several POSIX signals:

Signalsystemctl verbBehaviour
SIGUSR2reloadRe-reads config, gracefully replaces workers, keeps socket open
SIGQUIT—Graceful stop: waits for active workers to finish, then exits
SIGTERMstopHard stop: closes socket immediately, workers are killed
SIGINT—Same as SIGTERM
SIGUSR1—Reopen log files (for log rotation)
SIGHUP—Same as SIGUSR2 in most configurations

When SIGUSR2 arrives, the master forks a new set of workers against the updated configuration while the old workers finish their current requests. Old workers enter the Finishing state — visible in ?full status — and are reaped when they complete. New workers take over the socket. No connections are dropped.

The key constraint: a worker in Finishing state counts against pm.max_children until it exits. During the transition window, the effective worker capacity is old_finishing + new_active, which can temporarily double memory usage. If the server is already near its memory limit, a reload under load can trigger OOM.

The procedure

1. Validate config before every reload.

php-fpm8.3 --test
# Or equivalently:
php-fpm8.3 -t

This validates php-fpm.conf and all included pool files without starting or restarting anything. It exits non-zero if any pool has an error. Run this in CI and as the first step of any deployment script that modifies FPM config.

2. Perform a graceful reload.

systemctl reload php8.3-fpm
# Equivalent manual signal:
kill -USR2 $(cat /run/php/php8.3-fpm.pid)

3. Monitor the transition.

After the reload signal, watch the worker state settle:

# Old workers should drain from 'Finishing' state within max_execution_time seconds
watch -n1 "curl -s 127.0.0.1/status?full | grep -E 'state|start since' | head -40"

If workers stay in Finishing for longer than request_terminate_timeout (default: 0, meaning never), FPM will eventually send them SIGTERM. Check for stuck workers:

curl -s '127.0.0.1/status?full&json' | \
  jq '.processes[] | select(.state=="Finishing") | {pid, state, "start since": ."start since"}'

4. Integrate into deployment pipelines.

A safe deployment sequence for a PHP application:

#!/usr/bin/env bash
set -euo pipefail

# 1. Deploy application code
rsync -a --delete dist/ /var/www/app/

# 2. Validate FPM config (catches syntax errors before they go live)
php-fpm8.3 -t

# 3. Warm OPcache for critical paths (optional but reduces first-request latency)
php artisan opcache:compile 2>/dev/null || true

# 4. Graceful reload
systemctl reload php8.3-fpm

# 5. Verify pool came back healthy
sleep 2
curl -sf http://127.0.0.1/ping | grep -q pong || { echo "FPM ping failed after reload"; exit 1; }

5. Handling pm = ondemand during reloads. With ondemand, there are no pre-forked idle workers. The reload is instantaneous (no workers to drain), but the first request after the reload incurs the full startup latency. For critical endpoints, send a synthetic warm-up request immediately after reload.

Common pitfalls

Using systemctl restart during business hours. restart sends SIGTERM then SIGSTART. The SIGTERM closes the socket immediately; Nginx gets ECONNREFUSED for any in-flight FastCGI connection and returns a 502. Use reload unless you need to pick up a new binary.

Ignoring OPcache state after reload. SIGUSR2 replaces worker processes but the OPcache shared memory segment is controlled by the master. OPcache persists across reloads — which is usually what you want, but means stale bytecode can survive a reload if you changed PHP files and expected OPcache to invalidate them. Either set opcache.validate_timestamps = 1 in development, or call opcache_reset() explicitly after deploying to production.

Not testing php-fpm -t in CI. A broken pool config discovered at deploy time means you shipped broken config that only fails when FPM restarts. Add php-fpm8.3 -t to every pipeline that touches pool.d/.

Long-running workers blocking the transition window. If you have background jobs running inside FPM workers with max_execution_time = 0 (unlimited), those workers never enter Finishing state — they block the old pool from being reaped indefinitely. Use a separate pool with its own configuration for long-running tasks, or run background jobs outside FPM entirely.

request_terminate_timeout set to 0. The default (never terminate) means a stuck worker from before a reload holds a process slot forever. Set request_terminate_timeout = 60s (or your application’s max expected request time) so FPM can reap stale workers automatically.

For production deployments where zero-downtime PHP restarts are part of the release pipeline — including config validation hooks, OPcache coordination, and health-check gates — the Stack Harbor managed operations service handles deployment workflow design and incident response.