PHP-FPM: private sessions and log rotation

Separate session directories, guarded cleanup and rotation of pool and master logs, with actual permission and new-record checks.

Separate notebooks and paper rolls on a writing desk.

Once Nginx forwards requests to the right PHP-FPM pool, less visible work remains: who can read session files, who removes expired records, and where process errors go. In a manual run on a disposable Debian 13 VM, we configured this for two test applications, frontend and admin. This is a separate test environment; the settings were not deployed to the working server.

Sessions stay inside their own pool

Each pool has its own var/cache/sessions directory, mode 0700, owned by its runtime user. PHP creates session files with mode 0600. The neighbouring application gains no read access through a shared directory. Here is an excerpt from the PHP settings used:

session.use_strict_mode = 1
session.use_only_cookies = 1
session.cookie_httponly = 1
session.cookie_samesite = Lax
session.gc_probability = 0
session.gc_maxlifetime = 1440

Strict mode rejects an unknown session identifier supplied by a client. HttpOnly limits JavaScript access to the cookie, while SameSite controls its cross-site behaviour. These settings are covered by the PHP session documentation. The test environment uses loopback HTTP, so this example does not replace a separate secure-cookie policy for HTTPS.

Setting gc_probability = 0 disables random garbage collection inside an HTTP request. Instead, a dedicated oneshot service removes only admitted file sessions. Its threshold is 1440 seconds based on file modification time. This is a storage-cleanup criterion, not a promise of an exact application logout time.

File age is only one deletion condition

The cleaner walks from the filesystem root using directory descriptors with O_DIRECTORY and O_NOFOLLOW. It checks the final directory owner and mode 0700. A name must match the sess_ format, and the object must be a regular file owned by the expected user, with one hard link and mode 0600.

After opening with O_NOFOLLOW, the script compares device and inode, takes a nonblocking exclusive flock, and checks age. Immediately before unlink, it compares the name against the open file again: inode, owner, mode, link count and mtime. Locked files are skipped. This describes the specific test cleaner; it is not a complete general-purpose algorithm for arbitrary storage or processes that ignore locks.

The controlled run removed two expired files. Six fresh files and two locked files remained. Four other objects failed the ownership or type checks; two unrelated names were ignored. Symbolic links and their target files were preserved. This exercised both deletion and its permitted boundaries.

The master log and pool logs have different owners

Each pool writes PHP errors to its own var/log/php-error.log, mode 0600, owned by proof-web or proof-admin. The FPM master writes to /var/log/deploy-cli-php85/master-error.log, owned by root and also mode 0600. Its directory is persistent, with mode 0700, rather than temporary /run storage. The distinction between global error_log and pool settings is explained in the FPM documentation.

The run exposed an important discrepancy: the master file stayed empty while messages appeared in the journal. The runtime command contained --force-stderr, directing that stream to stderr. We removed the flag only from the service ExecStart. The syntax-check command retains --test --force-stderr, where diagnostic output must reach the checking process.

At this initial stage, the service used Type=simple: the active state appears before FPM finishes initialization. We therefore waited for actual HTTP responses from the correct pools through their Unix sockets and for ready to handle connections in the master log. A successful systemctl start alone does not establish this readiness. The switch to Type=notify followed in the next startup step.

Rotation requires reopening the file

The owned FPM logs use daily rotation, ten retained archives, compress and delaycompress. The last option postpones compression of the newest archive until the next cycle. This is an excerpt for the master log:

/var/log/deploy-cli-php85/master-error.log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    delaycompress
    create 0600 root root
    postrotate
        /usr/bin/systemctl kill --kill-whom=main --signal=USR1 deploy-cli-php85.service
    endscript
}

The equivalent frontend block uses create 0600 proof-web proof-web; admin uses its own user. The USR1 signal tells the master process to reopen its logs. The directives are described in the Debian logrotate manual.

Pool logs were rotated before the master-stream correction. The empty master file was skipped because of notifempty. After correcting the command and observing readiness, we performed only its first rotation without repeating completed actions. The archive retained the previous inode and initial content; the new master file had a different inode, mode 0600 and new messages. Deliberate application errors reached the new pool log files.

During the manual maintenance step, we configured dedicated systemd timers for the cleaner and logrotate and left them disabled. Cleanup and rotation here were started explicitly; startup was verified in a separate subsequent step. Monolog application logs have a separate rotating_file handler with max_files: 10; they were excluded from this rotation so two mechanisms do not manage the same file.

Repeated maintenance through the CLI

On a separate clean VM, the CLI first installed the full PHP profile, created two pools and verified their activation. Then maintenance-apply ran three owned oneshot services once and started three timers for the current boot. A separate maintenance-verify through a fresh SSH connection confirmed the same manifest. The session directories were empty; the deleted-file counter was zero.

The commands below use generalized private paths. PRIVATE_DIR holds protected operator files; the public-key SHA and approved-plan SHA have already been checked. php-care-deps.json refers to the original completed PHP activation plan.

PHP_CARE_ARGS=(
  --joint-start-policy
  --env-file "$PRIVATE_DIR/administrator.env"
  --request-file "$PRIVATE_DIR/request.json"
  --fpm-file "$PRIVATE_DIR/php-profile.json"
  --key-file "$PRIVATE_DIR/repository.gpg"
  --key-sha256 "$PUBLIC_KEY_SHA256"
  --dependencies-file "$PRIVATE_DIR/php-care-deps.json"
)
deploy-cli host fpm maintenance-plan "${PHP_CARE_ARGS[@]}" \
  --plan-file "$PRIVATE_DIR/php-maintenance-plan.json"

deploy-cli host fpm maintenance-apply "${PHP_CARE_ARGS[@]}" \
  --plan-file "$PRIVATE_DIR/php-maintenance-plan.json" \
  --plan-sha256 "$APPROVED_PLAN_SHA256" --approve-privileges

deploy-cli host fpm maintenance-verify "${PHP_CARE_ARGS[@]}" \
  --plan-file "$PRIVATE_DIR/php-maintenance-plan.json" \
  --plan-sha256 "$APPROVED_PLAN_SHA256"

These are reduced fields from the actual apply and verify results:

{
  "apply": {"status":"passed","action":"maintenance-apply","write_state":"changed"},
  "verify": {"status":"passed","action":"maintenance-verify","write_state":"none"},
  "same_manifest": true
}

The CLI implementation accounts for two details. A pool user can traverse protected parent directories with search permission without being allowed to list them. Also, systemd may unload a successful inactive oneshot unit and reset its start timestamp. The CLI therefore retains the first successful start outcome in its protected journal, while fresh verification checks service declarations, current state and timers. Startup after reboot has its own stage.

Reduced file recheck after the startup step: permissions, existing archives and owned logrotate; cleanup is not replayed.
  1. Operations & Security ACLs on Debian: allow public files and protect the private tree
  2. Architecture Debian service users: a separate identity for each application
  3. Architecture An application layout in /srv: code, public files, and writable data
  4. Operations & Security Startup and reboot: firewall, ready FPM, then Nginx
  5. Operations & Security Two PHP-FPM pools: separate UIDs, Unix sockets and file rights
  6. Platform Engineering PHP-FPM and OPcache: checking a pool’s effective settings