LEMP Field Notes
Stack Guides

Nginx vs Apache: Performance, Config, and When to Use Each

Nginx vs Apache: Performance, Config, and When to Use Each
In briefNginx and Apache are both mature, production-grade web servers; the core difference is architecture. Nginx uses an event-driven model where a few worker processes handle thousands of connections, making it faster and leaner for static files, high concurrency, and reverse proxying. Apache assigns processes or threads per connection and offers per-directory .htaccess overrides plus a deep module ecosystem. For PHP sites using PHP-FPM, real-world performance is similar; choose Nginx for efficiency, Apache for .htaccess flexibility.

Nginx vs Apache: What's the Real Difference?

Nginx and Apache are the two web servers you're most likely to meet on a Linux box, and the honest answer is that both are excellent. The real difference is architectural: Nginx uses an event-driven model that handles many connections per worker process, while Apache traditionally dedicates a process or thread to each connection. That makes Nginx leaner under heavy concurrency and a natural fit as a reverse proxy, while Apache offers per-directory .htaccess overrides and a deep module ecosystem that shared hosts and legacy apps depend on.

Neither choice will sink your project. But the differences in performance behavior, configuration style, and operational habits are real, and they matter more as your traffic grows. This guide walks through each one so you can pick deliberately rather than by default.

A Quick History (and Why It Explains Everything)

Apache HTTP Server dates back to 1995 and powered a huge share of the early web. It was built in an era when a server handling a few hundred simultaneous connections was doing well, so its design — spawn a process or thread per connection — was perfectly reasonable.

Nginx (pronounced "engine-x") was released in 2004 by Igor Sysoev, written specifically to address the C10K problem: how do you serve ten thousand concurrent connections on one machine? Its answer was an asynchronous, event-driven core where a small, fixed number of worker processes each juggle thousands of connections using non-blocking I/O.

That origin story explains almost every practical difference between the two. Apache optimizes for flexibility and per-site customization; Nginx optimizes for predictable resource use at scale.

How Each Server Handles Requests

Apache: processes, threads, and MPMs

Apache's connection handling is pluggable through Multi-Processing Modules (MPMs):

The catch: if you install mod_php, Apache typically falls back to prefork, the heaviest option. Running PHP-FPM behind Apache via mod_proxy_fcgi avoids this and lets you keep the event MPM.

Nginx: the event loop

Nginx starts a master process plus a small number of worker processes — commonly one per CPU core. Each worker runs an event loop that services thousands of connections without allocating a thread to each one. Memory use stays low and roughly flat as connection counts rise, which is why Nginx became the go-to choice for busy sites, reverse proxies, and load balancers.

The trade-off is that Nginx workers must never block. That's why Nginx doesn't embed language runtimes the way mod_php embeds PHP: dynamic requests are handed to a separate backend, usually PHP-FPM over FastCGI in a LEMP stack.

Nginx vs Apache Performance

Static content

For static files — images, CSS, JavaScript, downloads — Nginx generally serves more requests per second with less memory, and the advantage widens as concurrency climbs. Apache with the event MPM performs respectably, but Apache with prefork under high concurrency can exhaust RAM and start swapping, which is the failure mode behind many "server died under load" stories.

We won't quote specific benchmark numbers here because they vary enormously with hardware, kernel tuning, file sizes, and connection patterns. The honest general finding, consistent across years of independent testing, is: Nginx holds a clear edge on static content at high concurrency; at low traffic the difference is negligible.

Dynamic content (PHP)

For PHP applications the picture flattens, because the web server isn't doing the heavy work — PHP is. Whether the request path is Nginx → PHP-FPM or Apache → PHP-FPM, the time is dominated by PHP execution and database queries. A well-configured Apache event + PHP-FPM setup and a Nginx + PHP-FPM setup will feel very similar for typical dynamic workloads.

Where Nginx still pulls ahead is everything around the PHP request: serving the page's static assets, holding thousands of keep-alive connections cheaply, and acting as a caching or proxy layer.

Configuration: Two Different Philosophies

Central config vs .htaccess

This is the difference you'll feel daily.

Apache allows per-directory configuration through .htaccess files. Any directory can carry its own rewrite rules, access controls, and overrides, applied without touching the main config or reloading the server. It's why shared hosting standardized on Apache: customers can self-serve rules without root access. The cost is performance and debuggability — Apache must check for .htaccess files along the filesystem path on requests, and rules scattered across directories are harder to audit.

Nginx has no .htaccess equivalent, deliberately. All configuration lives centrally (on Debian/Ubuntu, under /etc/nginx/, with per-site files in sites-available/), and changes take effect only when you test with nginx -t and reload. That's more disciplined and faster at runtime, but it means every change requires shell access.

What the configs look like

A minimal Apache virtual host:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com
</VirtualHost>

The equivalent Nginx server block, with a PHP handoff included:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

(The socket path above matches PHP 8.3 on Ubuntu 24.04 — adjust it to your installed PHP version.) If the Nginx style appeals to you, our guide to Nginx server blocks walks through hosting multiple sites this way, line by line.

At a glance

Nginx Apache
Architecture Event-driven, async workers Process/thread per connection (MPM-dependent)
Static file performance Excellent, low memory at high concurrency Good with event MPM; weak with prefork
PHP handling External PHP-FPM via FastCGI Embedded mod_php or external PHP-FPM
Per-directory config No — central config only Yes — .htaccess
Config style Declarative blocks, must reload Directives + runtime overrides
Typical extra roles Reverse proxy, load balancer, cache Broad module ecosystem
Best known for Concurrency and efficiency Flexibility and compatibility

Apache or Nginx for WordPress?

WordPress runs well on both, and the WordPress ecosystem supports both officially. The practical considerations:

If you're building a fresh WordPress server today and are comfortable editing config files over SSH, Nginx is the mainstream recommendation for its efficiency and simpler request path. If you depend on .htaccess-driven workflows or tooling, Apache remains a fully legitimate choice.

When to Use Apache

Choose Apache when:

When to Use Nginx

Choose Nginx when:

Can You Use Both Together?

Yes, and it's a classic pattern: Nginx in front, Apache behind. Nginx faces the internet, serves static files directly, terminates TLS, and proxies dynamic requests to Apache listening on a local port. You get Nginx's concurrency at the edge and keep .htaccess compatibility for the application.

It's a genuinely useful migration path for legacy Apache apps. For a new build, though, it's usually unnecessary complexity — Nginx + PHP-FPM covers the same ground with one less moving part.

The Bottom Line

Is Nginx better than Apache? For high-concurrency serving, static content, and reverse proxying — yes, measurably. For per-directory flexibility, shared-hosting compatibility, and legacy module support — Apache still wins. For a typical PHP site with both servers configured well, the performance difference is smaller than most comparisons imply, because PHP-FPM does the heavy lifting either way.

Our default recommendation for a new self-managed server is Nginx: central config that's easy to audit, excellent efficiency, and a huge tutorial ecosystem — including the rest of our stack guides. Whichever you choose, configure it deliberately, test changes before reloading, and you'll be ahead of most of the internet.

FAQ

Is Nginx faster than Apache?

For static content at high concurrency, yes — Nginx's event-driven workers serve more requests with less memory, and the gap widens as simultaneous connections grow. For dynamic PHP content the difference largely disappears, because PHP-FPM and the database dominate response time regardless of which server sits in front. Apache's event MPM also narrows the static-file gap considerably compared with its older prefork mode.

Should I use Nginx or Apache for WordPress?

Both run WordPress well. On shared hosting you'll usually get Apache, where WordPress manages its own .htaccess rewrites automatically. On a self-managed VPS, Nginx with PHP-FPM is the mainstream choice: permalinks need only a single try_files directive, and most major plugins document Nginx equivalents for any .htaccess rules they'd otherwise write. Pick Apache if your workflow or tooling depends on .htaccess files.

What is the main difference between Nginx and Apache?

Architecture. Apache traditionally dedicates a process or thread to each connection, configurable through its MPMs (prefork, worker, event). Nginx runs a small, fixed set of worker processes, each handling thousands of connections through a non-blocking event loop. This makes Nginx's memory use low and predictable under load, while Apache trades some efficiency for flexibility, including per-directory .htaccess configuration that Nginx deliberately omits.

Does Nginx support .htaccess files?

No. Nginx has no .htaccess equivalent by design: all configuration lives in central files (under /etc/nginx/ on Debian and Ubuntu), and changes apply only after you test with nginx -t and reload. This improves runtime performance and makes configuration easier to audit, but it means every change requires shell access. Rules from an Apache .htaccess file must be translated into Nginx server-block directives.

Can I run Nginx and Apache together?

Yes — a classic setup puts Nginx in front as a reverse proxy while Apache runs on a local port behind it. Nginx terminates TLS, serves static files, and absorbs high concurrency; Apache handles dynamic requests and keeps .htaccess compatibility. It's a practical migration path for legacy Apache applications, though for new builds a single Nginx + PHP-FPM stack is usually simpler and sufficient.

Why does the LEMP stack use Nginx instead of Apache?

The E in LEMP stands for Nginx (pronounced engine-x). The stack pairs Nginx with PHP-FPM over FastCGI, which suits Nginx's non-blocking architecture: the web server never embeds a language runtime, so workers stay light while PHP runs in its own managed pool. The result is efficient memory use on small servers and clean separation between web serving and application execution.