Side by Side
| Apache | Nginx | |
|---|---|---|
| Connections | Processes or threads, set by the MPM | A few workers, each with an event loop |
| Per-folder files | .htaccess | None |
| PHP | mod_php or PHP-FPM | PHP-FPM |
| Modules | Loaded at runtime | Mostly built in |
| Config style | Directives in containers | Nested server and location blocks |
| Test command | apachectl configtest | nginx -t |
How Each Handles Connections
Apache delegates connection handling to a Multi-Processing Module (MPM). prefork runs one process per connection and is the MPM the distributions' mod_php packages are built to run under. worker serves connections with threads inside a few processes. event, the default on current Ubuntu, Debian, AlmaLinux, and Rocky Linux, works like worker but hands idle keep-alive connections to a listener thread, so threads stay available for active requests. Installing mod_php on Debian or Ubuntu switches Apache to prefork.
Nginx starts a small number of worker processes, often one per CPU core, and each one handles many connections with non-blocking input and output. With many idle or slow clients connected at once, this design needs less memory than a process or thread per connection. With a modest number of busy PHP requests, the PHP backend sets the limit, not the web server.
.htaccess and Configuration Style
Apache can read .htaccess files in each folder, so an application or a site owner can change rewrites and access rules without touching the main configuration. The cost is a file lookup per folder on every request, which disappears with AllowOverride None.
Nginx reads its configuration only at start and reload. Every rule lives in a server block, and rules from a .htaccess file must be rewritten, usually as location blocks and a try_files line. Some admins find that stricter; others find per-folder control essential.
Static Files and Proxying
Both serve static files and both work as reverse proxies, load balancers, and TLS endpoints: Apache through mod_proxy and mod_ssl, Nginx with proxy_pass and its built-in TLS support. Nginx is often placed in front of other servers for these jobs, which is why many setups combine the two.
PHP
Apache can run PHP inside its own processes with mod_php, the classic LAMP setup, or pass it to PHP-FPM through mod_proxy_fcgi. Nginx always passes PHP to a separate PHP-FPM service. With PHP-FPM, both servers use the same PHP processes and the same tuning; see what is PHP-FPM.
Modules
Apache loads modules at runtime: a2enmod on Debian and Ubuntu or a LoadModule line on AlmaLinux and Rocky Linux, with no rebuild. Nginx compiles most features in. It supports dynamic modules too, but each must be built for the matching Nginx version; distributions package the common ones.
Nginx in Front of Apache
In this combination, Nginx listens on ports 80 and 443, handles TLS and static files, and passes the rest to Apache on a local port. The application keeps its .htaccess rules.
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Apache then listens only on 127.0.0.1:8080. With mod_remoteip (enabled with sudo a2enmod remoteip on Debian and Ubuntu, loaded by default on AlmaLinux and Rocky Linux), a RemoteIPHeader X-Forwarded-For line, and 127.0.0.1 listed with RemoteIPInternalProxy, Apache sees the visitor's address instead of 127.0.0.1; the log format must use %a in place of %h to record it. The price is two servers to configure and keep updated.
Who Each One Suits
- Apache: applications that depend on .htaccess, servers where several people manage their own folders, and setups that need a specific Apache module.
- Nginx: reverse proxy and load balancer roles, sites with many open connections or much static content, and admins who prefer one central configuration.
- Either: a typical PHP site with PHP-FPM. The one you already know is usually the right start.
For a web server that aims at Apache compatibility, see LiteSpeed vs Apache. To set up Apache, see how to install Apache.