Skip to content

Apache with PHP-FPM over mod_proxy_fcgi

How to serve PHP through PHP-FPM behind Apache with mod_proxy_fcgi — the SetHandler pattern, the socket vs TCP choice, and the pool sizing that holds up.

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

  1. 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 apache2

    The two a2dismod calls handle the most common state: a previous mod_php install must be disabled, prefork MPM must be swapped for event. If they were never enabled, the commands are no-ops.

  2. 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 = 500

    pm = dynamic sizes the pool between pm.min_spare_servers and pm.max_spare_servers. pm.max_children is the hard ceiling. For a small site, 20 children at ~40 MB each = ~800 MB headroom needed for PHP. Adjust based on ps -eo rss,cmd against the running fpm workers under load.

  3. 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>
  4. 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.php

    Confirm the Server API is FPM/FastCGI in the output, then delete the info file — leaving phpinfo() publicly readable is a low-effort information leak.

  5. 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.conf per site, then point each vhost’s SetHandler at 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 = 500

    Per-site pools mean one app cannot starve another and you get per-site top-style visibility.

  6. 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=2

    Restart PHP-FPM after the change. For deployment workflows that hot-swap code, opcache.revalidate_freq=2 is the safest non-zero value; 0 reparses every request.

Common pitfalls

  • The socket path varies by distro (/var/run/php/php8.2-fpm.sock on Ubuntu, /run/php-fpm/www.sock on Rocky/Alma). A copy-pasted vhost from the wrong distro silently returns 500 errors with connect to upstream in the error log.
  • Apache’s www-data user must be able to read/write the FPM socket — that means matching listen.owner or listen.group with listen.mode = 0660.
  • Without proxy_fcgi, the vhost returns 500s with no clear error; configtest passes. Always confirm apachectl -M | grep proxy_fcgi after a fresh install.
  • The default PHP-FPM slowlog is 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_children set 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.