Nginx and Apache are the two most widely used open-source web servers. According to W3Techs, on 4 November 2026 Nginx ran on 30.0% of websites and Apache on 21.1%, so together they power about half of the web. (Cloudflare's own server tops that list at 33.2%, but that's the CDN edge, not something you install.)
They share a lot, yet they're built on different ideas. Each one wins in its own scenarios.
Short answer: pick Nginx for static files, reverse proxying and lots of concurrent connections. Pick Apache when you need .htaccess, shared hosting or an app that ships Apache rewrite rules. Run PHP through PHP-FPM with either one. And if you need both, put Nginx in front of Apache.
The configs below were tested on Ubuntu 26.04 LTS with Apache 2.4.66, Nginx 1.28.3 and PHP 8.5 from the Ubuntu repositories, plus the official Nginx 1.30 and 1.31 Docker images for dynamic modules.
On Ubuntu 24.04 the same steps work with php8.3-fpm and the /run/php/php8.3-fpm.sock socket.
How they handle connections
This is the biggest difference between the two, and it explains most of the others.
Apache: Multi-Processing Modules (MPMs)
Apache delegates connection handling to a Multi-Processing Module. Exactly one MPM is loaded at a time, and Apache 2.4 ships three of them on Linux:
- prefork runs one single-threaded process per connection. It's the most isolated and the most memory-hungry.
- worker runs several processes with many threads each. A thread is busy for the whole life of a keep-alive connection.
- event is worker plus a dedicated listener thread that parks idle keep-alive connections, so worker threads only do real work. It's the default on Linux and on Ubuntu.
Check which one you're running:
shell
1apachectl -V | grep MPMshell
1Server MPM: eventThe flexibility is real, but every busy connection still costs a thread or a whole process. Under heavy load with prefork, Apache can eat a server's RAM for breakfast.
Nginx: an event loop per worker
Nginx was built around an asynchronous, non-blocking, event-driven model from day one. The master process starts a few worker processes (usually one per CPU core with worker_processes auto;), and each worker handles thousands of connections in a single loop using epoll on Linux.
An idle keep-alive connection costs Nginx a few hundred bytes, not a thread: the Nginx project puts 10,000 inactive keep-alive connections at about 2.5 MB. That's why it scales so well on small machines and under heavy load.
Static and dynamic content
Both servers serve static files straight from disk. Nginx is faster at it, especially with many concurrent connections (see the quick test below).
For dynamic content, the real difference today is smaller than it used to be. Nginx never runs PHP itself: it hands the request to an external process over FastCGI and waits for the result. Apache could run PHP inside its own processes with mod_php, but that comes with a catch.
mod_php only works with prefork
The standard PHP build isn't thread-safe, so mod_php requires the prefork MPM (PHP manual). On Ubuntu, installing libapache2-mod-php quietly switches Apache from event to prefork. If you force event back, Apache refuses to start:
shell
1Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.So mod_php locks you into the slowest MPM. The PHP manual itself recommends PHP-FPM with the default event MPM.
PHP-FPM with Apache
On Ubuntu, enable the FastCGI proxy and the PHP-FPM config that ships with the php8.5-fpm package (use your PHP version):
shell
1sudo apt install apache2 php8.5-fpm2sudo a2enmod proxy_fcgi setenvif3sudo a2enconf php8.5-fpm4sudo systemctl reload apache2The php8.5-fpm config sends every .php file to the PHP-FPM socket with a SetHandler "proxy:unix:/run/php/php8.5-fpm.sock|fcgi://localhost" directive, so a plain virtual host is enough:
shell
1<VirtualHost *:80>2 ServerName example.com3 DocumentRoot /var/www/example4
5 <Directory /var/www/example>6 AllowOverride None7 Require all granted8 </Directory>9</VirtualHost>Save it as /etc/apache2/sites-available/example.conf, then enable it:
shell
1sudo a2ensite example2sudo apachectl configtest3sudo systemctl reload apache2If you're moving an existing server off mod_php, switch the modules and do a full restart. A graceful reload can't change the MPM and fails with The MPM cannot be changed during restart.
shell
1sudo a2dismod php8.5 mpm_prefork2sudo a2enmod mpm_event proxy_fcgi setenvif3sudo a2enconf php8.5-fpm4sudo systemctl restart apache2PHP-FPM with Nginx
Ubuntu's Nginx package includes a ready-made FastCGI snippet:
shell
1sudo apt install nginx php8.5-fpmnginx
1server {2 listen 80;3 listen [::]:80;4 server_name example.com;5 root /var/www/example;6 index index.php index.html;7
8 location / {9 try_files $uri $uri/ =404;10 }11
12 location ~ \.php$ {13 include snippets/fastcgi-php.conf;14 fastcgi_pass unix:/run/php/php8.5-fpm.sock;15 }16}Save it as /etc/nginx/sites-available/example.conf and enable it:
shell
1sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/2sudo nginx -t3sudo systemctl reload nginxWith PHP-FPM on both sides, the same PHP page runs through the same PHP process manager. The web server only decides how fast connections and static files are handled.
Per-directory configuration: .htaccess
Apache can read extra configuration from .htaccess files in any directory. That lets users without root change rewrites, redirects or access rules for their part of the site. It's why shared hosting and many CMS installs still depend on Apache.
It has a price. With AllowOverride enabled, Apache looks for .htaccess in the requested directory and in every directory above it, on every request, whether or not the files exist (Apache docs). If you control the server config, put the rules in the virtual host and set AllowOverride None.
Nginx has no per-directory config at all. Everything lives in the main config files and is read once at startup or reload. That's part of why Nginx is fast, and it's also why you can't just drop a WordPress .htaccess into an Nginx site: the rules have to be rewritten as location blocks.
How requests map to files
Apache started as a file server and thinks in directories. <Directory> blocks and .htaccess files attach rules to places on disk.
Nginx was designed as both a web server and a reverse proxy, so it thinks in URIs. A server block lists location patterns and says what to do with each: serve a file, pass it to PHP-FPM or proxy it somewhere else. Nginx only touches the file system when it's ready to serve a file. That's why the same config style works for local files and for upstream servers, and why .htaccess doesn't fit its model. If you need a forward proxy for outgoing traffic instead, such as Squid, see our list of Linux proxy servers.
Modules: both can load them dynamically
Apache has loaded modules at runtime for a long time. You enable one with a2enmod (or a LoadModule line) and reload.
Nginx used to require every module to be compiled into the binary. That changed in Nginx 1.9.11 back in 2016: modules can now be built as dynamic .so files and loaded with the load_module directive (Nginx docs). The official Nginx packages ship njs, image filter, GeoIP and XSLT as dynamic modules, and Ubuntu has them as libnginx-mod-* packages that drop a load_module line into /etc/nginx/modules-enabled/.
A minimal example with njs (the JavaScript module) on the official Nginx image:
nginx
1load_module modules/ngx_http_js_module.so;2
3events {}4
5http {6 js_path /etc/nginx/njs/;7 js_import hello.js;8
9 server {10 listen 80;11 location /hello {12 js_content hello.hello;13 }14 }15}typescript
1function hello(r) {2 r.return(200, "Hello from njs " + njs.version + "\n");3}4export default { hello };load_module has to sit at the top level of nginx.conf, and a normal nginx -s reload picks it up without a restart. Without that line, nginx -t fails with unknown directive "js_path".
The catch is binary compatibility. A dynamic module must be built for the exact Nginx version that loads it. A module from Nginx 1.31.8 loaded into 1.30.6 is rejected:
shell
1nginx: [emerg] module "/etc/nginx/njs/js-131.so" version 1031008 instead of 1030006So with third-party modules you either install them from the same repository as Nginx or rebuild them against your exact version (with the --with-compat configure flag) on every upgrade. Apache modules built for 2.4 usually keep working across 2.4.x updates. Apache still has the bigger module catalog, while Nginx keeps the core small and lets you load only what you use.
Running Nginx in front of Apache
You don't have to choose. A common setup puts Nginx on ports 80 and 443 and keeps Apache behind it on localhost:
- Nginx accepts every connection, terminates TLS and serves static files from disk.
- PHP and anything Nginx can't find on disk go to Apache, so
.htaccessrewrites and Apache-only modules keep working.
It's worth it when an app depends on Apache features but you want Nginx's connection handling. If all you run is PHP, Nginx plus PHP-FPM without Apache is simpler.
Step 1: move Apache to localhost
In /etc/apache2/ports.conf, change Listen 80 to Listen 127.0.0.1:8080. If mod_ssl is enabled, remove its Listen 443 lines as well, because Nginx will handle TLS. Then enable mod_remoteip, so Apache and PHP see the real client IP instead of the proxy's:
shell
1sudo a2enmod remoteip proxy_fcgi setenvif2sudo a2enconf php8.5-fpmshell
1<VirtualHost 127.0.0.1:8080>2 ServerName example.com3 DocumentRoot /var/www/example4
5 RemoteIPHeader X-Forwarded-For6 RemoteIPInternalProxy 127.0.0.17
8 <Directory /var/www/example>9 AllowOverride All10 Require all granted11 </Directory>12
13 CustomLog ${APACHE_LOG_DIR}/example-access.log combined14</VirtualHost>Enable the site and do a full restart, since Apache has to drop port 80 and bind to the new address:
shell
1sudo a2ensite example2sudo apachectl configtest3sudo systemctl restart apache2RemoteIPInternalProxy trusts the header only from Nginx on 127.0.0.1. In our test a client that sent a fake X-Forwarded-For header still showed up with its real address in PHP and in the Apache log.
Step 2: put Nginx in front
Port 80 is free now. Install Nginx if it isn't there yet and save this as /etc/nginx/sites-available/example.conf:
shell
1sudo apt install nginxnginx
1server {2 listen 80;3 listen [::]:80;4 server_name example.com;5 root /var/www/example;6 index index.php index.html;7
8 location / {9 try_files $uri $uri/ @apache;10 }11
12 location ~ \.php$ {13 proxy_pass http://127.0.0.1:8080;14 include proxy_params;15 }16
17 location @apache {18 proxy_pass http://127.0.0.1:8080;19 include proxy_params;20 }21
22 # Apache's .htaccess rules don't apply to files nginx serves itself23 location ~ /\.ht {24 deny all;25 }26}proxy_params is Ubuntu's snippet that passes Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto. Enable the site and restart Nginx. A restart, not a reload: if Nginx was installed while Apache still held port 80, it isn't running yet.
shell
1sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/2sudo nginx -t3sudo systemctl restart nginxThen check that PHP pages come back with Apache behind them while static files never show up in the Apache access log.
⚠️ Don't rely on .htaccess for access control in this setup. Nginx serves static files without asking Apache, so a Require all denied in .htaccess doesn't protect them. In our test, a file in a directory that Apache answered with 403 came back as 200 through Nginx, and so did the .htaccess file itself. Block hidden files as shown above, and copy any deny rules for static paths into Nginx as location blocks with deny all;.
A quick local test
We served the same 1.4 KB static file from both servers in Docker on one laptop, with Ubuntu 26.04's default configs and wrk:
- At 50 concurrent connections, Nginx handled almost twice as many requests per second as Apache with the event MPM.
- At 1,000 connections, Nginx was about 2.4 times faster, and Apache dropped connections because of its default
MaxRequestWorkers 150andMaxKeepAliveRequests 100. - Turning on
AllowOverride Allfor a file three directories deep cost Apache 10-20% at 50 connections across our runs.
Treat these as direction, not numbers. A tuned Apache closes a lot of that gap, and for PHP pages the bottleneck is usually PHP and the database, not the web server.
Apache or Nginx: what to choose
Choose Nginx when:
- you serve a lot of static content or many concurrent and keep-alive connections;
- you need a reverse proxy, load balancer or TLS terminator in front of other services;
- you run PHP, Python, Node or Java apps that talk to the web server over FastCGI or HTTP anyway.
Choose Apache when:
- users or apps need
.htaccess, for example on shared hosting; - the app ships Apache-specific rewrite rules or depends on Apache modules;
- your team already knows Apache well and the load is moderate.
Choose both when an app needs Apache features but has to survive heavy traffic. Nginx takes the connections and the static files, Apache handles the rest.
What we see in production
In most of our projects Nginx is the better fit, either alone or in front of something else. Here's what that looks like on one of our clients' servers running Nginx with the application backend on the same machine. CPU load on a 4-core processor with Hyper-Threading stayed around 15-20%:
The same server handled about 11,200 requests per second on average over that period, with a peak of about 13,100:
Another server kept about 85,000 connections open at once at the daily peak (91,000 maximum). Most of them were idle keep-alive connections in the waiting state, which is exactly the load an event loop handles cheaply:
Want a second opinion on your web server setup? Our engineers can review it as part of a cloud infrastructure review and help you secure your website and server properly. If you're on Nginx already, see how to redirect HTTP to HTTPS in Nginx.
Live long and prosper!
