FastCGI worker pools

The page processor is dual-mode. Invoked as plain CGI it behaves exactly as it always has; spawned with a FastCGI listen socket on fd 0 (the spawn-fcgi convention) it services requests from a persistent accept loop instead. Modules compile once per worker, per-request state resets inside the loop, and both paths share the same request handler - a pooled site renders pages identically to a CGI one.

What runs persistent

Because DOCROOT is fixed at spawn, one pool serves exactly one site and isolation stays process-level. A pool is used in one of two arrangements, chosen by the vhost template:

  • Split pool (vhost-fcgi). The pool serves the anonymous, visitor-facing pages - the hot path. Session-bearing traffic (login, the manager, WebDAV, plugin endpoints) is carved out by the vhost to the CGI auth wrapper.
  • Front-door pool (vhost-one-rule-pool, with FRONT_DOOR=1 in the pool file). The vhost sends every request to the pool and the worker routes it: pages, misses, static files and denials are answered in the worker. Since 0.15.12 the worker also decides who a signed-in request is and serves signed-in pages itself. The manager API, WebDAV, MCP, OAuth and plugin endpoints run in a process the worker forks for that request.

Measured effect

On the reference host, as the mean of 50 cache-hit requests with one worker: 62.2 ms as plain CGI, 0.4 ms pooled. The CGI figure is almost entirely process start-up, which the accept loop amortises away.

Plain-CGI equivalence

Plain CGI remains the default, the dev-server path, and the zero-dependency fallback. FCGI.pm is lazy-required, so a CGI-only host needs no FastCGI modules at all. The pool needs libfcgi-perl (plus libfcgi-procmanager-perl for prefork), both Recommends of the lazysite-common package. Wherever no pool is configured, plain CGI keeps working untouched.

Opting a site in

On a packaged host, describe the pool in /etc/lazysite/pools/ and enable the lazysite@ template unit for the domain:

# /etc/lazysite/pools/example.com.conf
DOCROOT=/home/siteuser/web/example.com/public_html
USER=siteuser
# optional: GROUP= (default www-data), WORKERS= (default 2),
#           MAX_REQUESTS= (default 500), SOCKET=
# front-door mode: FRONT_DOOR=1 (default off),
#           CGIBIN= (default: the cgi-bin beside the docroot),
#           RELAY_TIMEOUT= (default 120 seconds)

systemctl enable --now lazysite@example.com

The unit starts the pool launcher as root; the launcher binds /run/lazysite/example.com.sock (mode 0660, connectable by the web server group), drops privileges to the site user - USER=root is refused - and execs the processor with the socket on fd 0. Root leaves the request path and nothing writes into the site tree. The unit runs the worker under systemd sandboxing (NoNewPrivileges, ProtectSystem=full, PrivateTmp).

Then point the web server at the socket. Commented vhost-fcgi and vhost-one-rule-pool examples ship in the lazysite-apache and lazysite-nginx packages; for Apache mod_proxy_fcgi the core of it is:

SetHandler "proxy:unix:/run/lazysite/example.com.sock|fcgi://localhost/"

On HestiaCP, lazysite-hestia-domain add USER DOMAIN --fcgi writes the pool config and enables lazysite@DOMAIN, then prints the two template commands for the operator to run:

v-change-web-domain-tpl USER DOMAIN lazysite-fcgi yes
v-change-web-domain-proxy-tpl USER DOMAIN lazysite-proxy

Tuning

  • WORKERS - prefork size via FCGI::ProcManager; the pool launcher defaults to 2. With WORKERS=0 a single worker runs, exits after MAX_REQUESTS requests, and systemd's Restart= respawns it.
  • MAX_REQUESTS - a worker recycles after this many requests (default 500); memory hygiene for long-lived processes.

Edit the pool file, then systemctl restart lazysite@<domain>.