Apache vs Nginx for PHP: It Is mod_php vs PHP-FPM

Updated

Apache and Nginx web server illustration

The choice that changes how much memory your PHP app uses is mod_php versus PHP-FPM, and it is not the same choice as Apache versus nginx. Apache runs PHP-FPM perfectly well. Here it is doing so — Apache 2.4.67 with the event MPM, handing .php requests to a PHP-FPM pool over FastCGI:

$ curl -s http://127.0.0.1:18080/probe.php
sapi=fpm-fcgi
php=8.5.8
server_software=Apache/2.4.67 (Unix)

sapi=fpm-fcgi is PHP reporting its own SAPI. That is the process model people call “the nginx way”, running under Apache via mod_proxy_fcgi and a five-line config block. Any comparison that treats mod_php as Apache’s only option is comparing a 2010 deployment against a 2026 one.

Given httpd and php-fpm, apache-vs-nginx-php.sh starts both on a loopback high port, writes a throwaway config for them and asserts that PHP answers sapi=fpm-fcgi; it needs no root, touches no system configuration and stops everything it started.

Why there are no benchmark numbers here

This page has no response-time, CPU or memory figures for nginx against Apache: nginx is not installed on this machine, and no honest figures could be produced.

Ask any web server benchmark for its setup: core count, PHP version, opcache on or off, MPM and worker counts, keepalive, static or dynamic content, and the load-generator command line. Change one and the ranking can flip.

Why mod_php actually costs memory

The mechanism is not “Apache is heavy”. mod_php embeds the interpreter in the web server process, and PHP’s own installation manual then constrains which Apache process model you may use: “We do not recommend using a threaded MPM in production with Apache 2. Use the prefork MPM, which is the default MPM with Apache 2.0 and 2.2.”

Prefork means one process per concurrent connection, and with mod_php every one of them carries a PHP interpreter — including the ones serving a CSS file. That is where the memory goes, and it is a property of mod_php rather than of Apache: this build defaults to prefork, but mod_mpm_event.so and mod_mpm_worker.so ship alongside it and become usable the moment PHP moves out of the server process.

PHP-FPM decouples the two. The server keeps a small pool of connection handlers; PHP runs in its own pool with its own pm.max_children, sized against your memory budget rather than your connection count, and static files never touch a PHP process. That benefit belongs to FastCGI — Apache and nginx both get it. None of it decides how fast a given host is, which is a different measurement with its own reasons no page can benchmark it for you.

mod_php is going away on its own

Not as an opinion — as an inventory. The Apache 2.4.67 that ships with macOS 26.6 has 115 files in its module directory. Zero of them contain “php” in the name:

$ ls -1 /usr/libexec/apache2 | wc -l
     115
$ ls -1 /usr/libexec/apache2 | grep -ci php
       0
$ ls -1 /usr/libexec/apache2 | grep -E 'proxy_fcgi|mpm'
mod_mpm_event.so
mod_mpm_prefork.so
mod_mpm_worker.so
mod_proxy_fcgi.so

The same script enumerates that directory rather than grepping for a name it expects, so the zero is a count of what is there. This is one vendor’s build, not a statement about your distribution’s packages — but the FastCGI plumbing ships and the in-process module does not.

nginx modules, and the claim that it is more secure by default

Nginx has had loadable modules for a decade. Its documentation for the load_module directive says: “Loads a dynamic module”, and “This directive appeared in version 1.9.11.” That is 2016.

The claim that nginx is “often considered more secure by default” on a smaller attack surface has not been measured — “considered by whom” is doing the work in that sentence.

.htaccess is the real configuration difference, and Apache advises against it

Apache’s own documentation is blunter than any comparison article: “If you have access to the main server configuration file, you should put all of your configuration there instead of .htaccess files.” Its reason is specific: “permitting .htaccess files causes a performance hit, whether or not you actually even use them!” — and for a request under /www/htdocs/example, “there are 4 additional file-system accesses, even if none of those files are present.”

So .htaccess is not a performance advantage you give up by moving to nginx. It is a feature for shared and control-panel hosting, where users have no root, and Apache tells you to turn it off everywhere else. If your app ships a .htaccess you cannot control — WordPress, most PHP CMSes — that is a real reason to stay on Apache, and it is compatibility, not speed.

How to actually choose

  • cPanel, shared hosting, or an app shipping its own .htaccess — Apache. Nothing else reads that file. (cPanel can still put NGINX in front of Apache through its NGINX Manager — that is adding a front end, not replacing the server that reads .htaccess.)
  • Terminating TLS or fronting an app with static content — nginx is the usual choice for that role, and putting either in front of the other is normal.
  • Either way, run PHP under PHP-FPM — fastcgi_pass on nginx, mod_proxy_fcgi plus a non-prefork MPM on Apache. That is the change that moves memory, and both support it. It is also what you inherit by default in a container image, which is how a PHP app ends up on Kubernetes without anyone choosing an MPM.
  • A config you already understand beats a server you have only read about.
Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.