Running PHP through Apache’s bundled mod_php forces you onto the prefork MPM and pays a process-per-connection memory tax. Running PHP through PHP-FPM behind mod_proxy_fcgi is the modern path: Apache uses the event MPM, PHP runs in a separate pool tuned for the workload, and the two communicate over a Unix socket or a localhost TCP socket. This article is the wiring and the sizing math.
How to verify
Confirm the moving parts: PHP-FPM is running and listening on a known socket, mod_proxy_fcgi is loaded, mod_php is not, and the vhost routes PHP requests through the proxy:
systemctl status php8.2-fpm --no-pager
ss -lnxp | grep php8.2-fpm.sock
apachectl -M | grep -E 'proxy_fcgi|php'
grep -r 'SetHandler.*proxy:' /etc/apache2/sites-enabled/
curl -sI https://example.com/info.php
info.php containing <?php phpinfo(); ?> is the smoke test — the response should include the PHP version in the X-Powered-By header (if not stripped) and a successful 200. The Server API row in the phpinfo output should read FPM/FastCGI, not Apache 2.0 Handler.
What’s happening
mod_proxy_fcgi is a FastCGI client built on top of mod_proxy. Apache receives the HTTP request, normalizes headers, and forwards the request to PHP-FPM over a Unix socket or TCP. PHP-FPM dispatches to one of its pool workers, the worker executes the script, returns the response over the same socket. Apache wraps the response back into HTTP and sends it to the client.
The pattern that makes this clean is SetHandler "proxy:unix:..." inside a <FilesMatch>:
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost/"
</FilesMatch>
proxy: is the scheme, unix:/var/run/php/php8.2-fpm.sock is the transport, fcgi://localhost/ is the FastCGI worker the request should go to. The localhost part is opaque to FastCGI — it just needs to be non-empty.
The Unix socket vs TCP choice:
- Unix socket — faster (no TCP stack), no port to allocate, file permissions for access control. Use when Apache and PHP-FPM live on the same host.
- TCP socket on 127.0.0.1:9000 — slightly slower, but lets you point Apache at a remote PHP-FPM (rare for Apache; common in container orchestration). Easier to monitor with
ss.
In practice, almost every Apache + PHP-FPM deployment we run uses the Unix socket.
The procedure
-
Install PHP-FPM and the Apache proxy module.
sudo apt install -y php8.2-fpm libapache2-mod-fcgid sudo a2enmod proxy proxy_fcgi setenvif sudo a2dismod php8.2 mpm_prefork 2>/dev/null || true sudo a2enmod mpm_event http2 sudo systemctl restart php8.2-fpm apache2The two
a2dismodcalls handle the most common state: a previousmod_phpinstall must be disabled, prefork MPM must be swapped for event. If they were never enabled, the commands are no-ops. -
Confirm the PHP-FPM pool exists. A default install creates a pool at
/etc/php/8.2/fpm/pool.d/www.conf. The relevant lines:user = www-data group = www-data listen = /var/run/php/php8.2-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0660 pm = dynamic pm.max_children = 20 pm.start_servers = 4 pm.min_spare_servers = 2 pm.max_spare_servers = 6 pm.max_requests = 500pm = dynamicsizes the pool betweenpm.min_spare_serversandpm.max_spare_servers.pm.max_childrenis the hard ceiling. For a small site, 20 children at ~40 MB each = ~800 MB headroom needed for PHP. Adjust based onps -eo rss,cmdagainst the running fpm workers under load. -
Wire the vhost to PHP-FPM. The single-vhost pattern:
<VirtualHost *:443> ServerName example.com DocumentRoot /var/www/example.com/public <Directory /var/www/example.com/public> Options -Indexes +FollowSymLinks AllowOverride None Require all granted DirectoryIndex index.php index.html </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/var/run/php/php8.2-fpm.sock|fcgi://localhost/" </FilesMatch> SSLEngine on SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost> -
Test. Configtest first, then drop a
phpinfo()page in the DocumentRoot:sudo apache2ctl configtest sudo systemctl reload apache2 echo '<?php phpinfo();' | sudo tee /var/www/example.com/public/info.php curl -sI https://example.com/info.phpConfirm the
Server APIisFPM/FastCGIin the output, then delete the info file — leaving phpinfo() publicly readable is a low-effort information leak. -
Set per-site pools for multi-tenant boxes. When several sites share a box, give each one its own PHP-FPM pool with its own user, socket, and resource limits. Create
/etc/php/8.2/fpm/pool.d/example.com.confper site, then point each vhost’sSetHandlerat the per-site socket:[example.com] user = example group = example listen = /var/run/php/example.com.sock listen.owner = www-data listen.group = www-data pm.max_children = 8 pm.max_requests = 500Per-site pools mean one app cannot starve another and you get per-site
top-style visibility. -
Enable opcache. PHP-FPM does not enable opcache by default on every install; without it, every request reparses PHP. In
/etc/php/8.2/fpm/php.ini:opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=2Restart PHP-FPM after the change. For deployment workflows that hot-swap code,
opcache.revalidate_freq=2is the safest non-zero value;0reparses every request.
Common pitfalls
- The socket path varies by distro (
/var/run/php/php8.2-fpm.sockon Ubuntu,/run/php-fpm/www.sockon Rocky/Alma). A copy-pasted vhost from the wrong distro silently returns 500 errors withconnect to upstreamin the error log. - Apache’s
www-datauser must be able to read/write the FPM socket — that means matchinglisten.ownerorlisten.groupwithlisten.mode = 0660. - Without
proxy_fcgi, the vhost returns 500s with no clear error; configtest passes. Always confirmapachectl -M | grep proxy_fcgiafter a fresh install. - The default PHP-FPM
slowlogis off — turn it on (slowlog = /var/log/php-fpm/example.com-slow.log,request_slowlog_timeout = 5s) and you get a stack trace for every request that took more than the timeout. pm.max_childrenset too high (e.g. 200 on a 2 GB box) lets PHP OOM the box under a spike. Multiply average per-worker RSS by max_children and confirm there is headroom.
Stack Harbor runs PHP-FPM pools sized to the workload, with per-site pools where multi-tenant, opcache tuned, and slowlogs flowing into the search index. If your Apache box is still running mod_php and the workers are eating all your RAM, the migration to PHP-FPM is a standard piece of work under Managed Operations.