Apache mod_php vs PHP-FPM: Which Should You Use?

Apache can run PHP through two fundamentally different methods — mod_php (embedded directly in Apache) or PHP-FPM (a separate FastCGI process manager). This guide explains the difference and helps you choose.

mod_php: The Traditional Approach

PHP runs embedded directly within each Apache worker process — simple to set up, but means every Apache process carries PHP's full memory overhead, even when serving non-PHP requests.

PHP-FPM: The Modern, Decoupled Approach

PHP runs as a separate pool of worker processes, with Apache (or Nginx) forwarding PHP requests to it via FastCGI — more efficient memory usage, and the same PHP-FPM setup can be shared between Apache and Nginx if you're ever migrating between them.

Key Differences

Factormod_phpPHP-FPM
Memory efficiencyLower — every worker carries PHP overheadHigher — PHP processes are separate and can be tuned independently
Setup complexitySimpler, fewer moving partsSlightly more configuration
Process isolationPHP tied directly to Apache's process model (prefork MPM required)Independent process pool, works with more efficient Apache MPMs
Performance under loadGenerally lowerGenerally better, especially at scale

Why PHP-FPM Enables Better Apache Performance

mod_php requires Apache's older "prefork" MPM (Multi-Processing Module); PHP-FPM lets you use Apache's more efficient "event" or "worker" MPM instead, since PHP processing is decoupled from Apache's own worker processes — a meaningful performance advantage under concurrent load.

Installing PHP-FPM

sudo apt install php-fpm libapache2-mod-fcgid -y

Configuring Apache to Use PHP-FPM

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>

Switching Apache to the Event MPM

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo a2enmod proxy_fcgi
sudo systemctl restart apache2

Verifying PHP-FPM Is Active

<?php phpinfo(); ?>

Check the "Server API" value in the output — should show "FPM/FastCGI" rather than "Apache 2.0 Handler" (which indicates mod_php).

When mod_php Might Still Be Reasonable

For very small, low-traffic sites where the performance difference is negligible and simplicity is prioritized, mod_php remains a functional (if not optimal) choice — but for anything with meaningful traffic or where you want headroom to scale, PHP-FPM is the better foundation.

Tuning PHP-FPM Process Pools

sudo nano /etc/php/8.2/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6

Tune based on available RAM and expected concurrent PHP request volume — too few children causes request queuing under load; too many risks exhausting server memory.

Common Errors

502 Bad Gateway after switching to PHP-FPM — verify the socket path in the Apache configuration exactly matches PHP-FPM's actual listening socket, and that PHP-FPM is running (systemctl status php8.2-fpm).

Continue Reading

Browse more articles in Web Servers.

  • mod_php vs php-fpm, apache php configuration, php-fpm apache, apache mpm event
  • 0 Kasutajad peavad seda kasulikuks
Kas see vastus oli kasulik?

Seotud artiklid

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...