Nginx vs Apache: Which Web Server Should You Run?

Cover graphic: Nginx vs Apache: Which Web Server Should You Run?

Core takeaway

  • Both are sound choices: both are free, open-source, mature and secure when maintained. The real question is which fits your project, your habits and the software you plan to run.
  • Built differently: Apache uses processes and threads; Nginx uses events.
  • Biggest practical difference: .htaccess. The guide shows the same configuration written for both.
  • Rule: choose the web server you can configure, read and fix at 3 a.m.

Disclosure: con1ainer.com participates in affiliate programs. This article contains no affiliate links. See the Affiliate & Commercial Disclosure.

The 15-second version: pick the one you can fix at 3 a.m.

Most advice about Nginx and Apache is either tribal or out of date. One side says Apache is slow and old. The other says Nginx is a toy that cannot handle real applications. Both claims are wrong.

Both are free, open-source, mature, and secure when maintained. Both power an enormous share of the websites you visit. The real question is not which one is better. It is which one fits your project, your habits, and the software you plan to run.

This guide explains what a web server does, how the two are built differently, what those differences mean in practice, and how to choose. It shows the same configuration written for both, so you can see the difference instead of taking anyone’s word for it. Every term is explained as it appears.

What a Web Server Does

A web server is a program that listens for requests from browsers and sends back responses. When someone types your address, their browser sends a request over the network, the web server receives it, decides what the request is asking for, and answers.

It answers in one of two ways:

Kind of contentWhat the web server doesExample
StaticReads a file from disk and sends it as it isAn image, a stylesheet, a JavaScript file, a plain HTML page
DynamicPasses the request to an application that builds the page, then sends the resultA WordPress page built by PHP using data from a database

For dynamic content, the web server needs a way to talk to the application. For PHP, the standard method is FastCGI through PHP-FPM. PHP-FPM is a separate program that runs PHP code. The web server hands it the request, waits for the finished page, and passes it back to the visitor. Nginx always works this way. Apache can work this way too, or it can run PHP inside its own processes, which is an older method called mod_php.

The web server sits between the internet and your application. That makes its choice matter for configuration, memory use, and security. It matters less for raw speed than most people expect, and the section on performance explains why.

How They Are Built Differently

The real technical difference is how each program handles many visitors at once.

Apache: processes and threads

Apache uses a multi-processing module (MPM) to decide how to handle connections. There are three:

MPMHow it handles connectionsNotes
preforkOne separate process per connectionSafe with mod_php, but uses the most memory
workerSeveral processes, each running many threadsLighter than prefork
eventLike worker, but idle connections are handed to a dedicated threadThe modern default. Best with PHP-FPM

A process is a running copy of a program with its own memory. A thread is a smaller unit of work inside a process that shares the process’s memory. Threads are cheaper than processes, so the move from prefork to event made Apache far more efficient than its old reputation suggests.

Nginx: events

Nginx was designed around an event-driven model. A small, fixed number of worker processes, usually one per CPU core, each handle thousands of connections. A worker never waits on one slow visitor. When a connection has nothing to do, the worker moves on to another and returns when data arrives.

The result is that Nginx uses very little memory per connection, and a flood of slow or idle connections costs it far less than it costs a prefork Apache.

What this means for you

Under light or moderate traffic, you will not notice a difference. The architecture matters when you have thousands of simultaneous connections, large numbers of static files, or a small server with very little spare memory. On a 1 GB or 2 GB VPS, that last case is common, and it is why Nginx became the default recommendation for small servers.

The Big Practical Difference: .htaccess

This is where most people actually feel the choice.

.htaccess is a small configuration file that Apache looks for in every folder it serves. Whoever can write files into the folder can change how the server behaves for that folder: redirects, password protection, rewrite rules, caching headers, and more. No administrator access and no server restart is required. Changes apply on the next request.

That is why Apache dominates shared hosting. Thousands of customers can each control their own site’s behavior without being able to touch the server itself.

Nginx has no equivalent. It reads its configuration only from central files, and the change takes effect after a reload. Anything that used to live in a .htaccess file must be rewritten into the Nginx configuration by someone with administrator access.

The trade-offs are real on both sides.

Apache with .htaccessNginx without it
Who can change rulesAnyone who can write to the site folderOnly an administrator
SpeedSlightly slower, because Apache checks every folder in the path for the file on every requestFaster, because configuration is read once at startup
Plugin compatibilityWordPress plugins can write their own rulesPlugins that write to .htaccess do nothing. You add the rules by hand
SafetyA compromised site can rewrite its own rulesA compromised site cannot change server behavior
Moving from shared hostingExisting rules keep workingRules must be translated

The fourth row is why Nginx’s lack of .htaccess is sometimes described as a security feature. The third and fifth rows are why moving a site from shared hosting to Nginx takes more work than people expect, as covered in How to Move a Website to a VPS Without Downtime.

The Same Job, Written for Both

The clearest way to compare them is to give each the same task: serve a WordPress site over HTTPS, with working permalinks (readable page addresses), and send plain HTTP visitors to the HTTPS version.

Nginx

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    root /var/www/example.com;
    index index.php;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    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 permalink handling is the single try_files line. It tells Nginx to look for a real file, then a real folder, and otherwise to hand the request to WordPress.

Apache

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

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

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    <Directory /var/www/example.com>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

A VirtualHost is Apache’s name for one site’s settings, which lets one server host many domains. The important line is AllowOverride All. It permits .htaccess files in that folder to change behavior. WordPress then places its own permalink rules in the site’s .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Those five lines do the same job as Nginx’s one try_files line. For this particular task, Nginx is shorter. For a site with dozens of redirects and plugin-generated rules, the .htaccess approach can be easier to maintain, because the application keeps the rules up to date itself.

To run PHP under Apache with PHP-FPM, the Ubuntu setup is:

sudo apt install apache2 php-fpm
sudo a2enmod proxy_fcgi setenvif rewrite ssl
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2

a2enmod switches on an Apache module, and a2enconf switches on a prepared configuration file. Replace 8.3 with your PHP version. If the older mod_php was installed, disable it and the prefork MPM so that the event MPM can be used.

Performance, Honestly

The internet is full of benchmarks showing one server beating the other by a wide margin. Most of them test serving a single tiny static file under artificial load, which is not what a real WordPress site does.

For a typical site, a few facts matter more than the brand of web server:

  • The web server is rarely the slowest part. Time is spent in PHP and the database. If a page takes 900 ms to build, whether Nginx or Apache then sends it in 2 ms or 3 ms is irrelevant. Read Why Is My VPS Slow? How to Find the Bottleneck before changing web servers for speed.
  • Page caching beats everything else. A cached page served by either server is dramatically faster than an uncached page served by the other.
  • Apache with the event MPM and PHP-FPM is competitive. The comparison people remember, Nginx against prefork Apache with mod_php, describes a configuration few people should be running now.

Where Nginx does hold a real advantage is memory efficiency under many simultaneous connections and serving large volumes of static files. On a small VPS facing a traffic spike or a crawler, that headroom is valuable.

Features Side by Side

FeatureNginxApache
Configuration styleCompact blocks in central filesDirectives in central files plus optional .htaccess
.htaccess supportNoYes
PHP methodPHP-FPM onlyPHP-FPM, or mod_php
Memory use under many connectionsLowModerate with event, high with prefork
Static file servingExcellentGood
Reverse proxy and load balancingExcellent, a common useCapable, through modules
ModulesMostly built in. Dynamic modules are supported but fewerLarge module ecosystem, switched on and off with a2enmod
Shared hosting and control panelsLess commonStandard
Online tutorials for WordPress rulesPlentyPlenty, and more for older plugins
Reload without dropping connectionsYesYes

A reverse proxy is a server that stands in front of other applications. It receives visitors and passes each request to the right application behind it. Docker setups with several applications almost always use one. Read next: Docker on a VPS: A Practical Starting Point

Everyday Commands Compared

The workflow is nearly identical. Only the names change.

TaskNginxApache (Ubuntu)
Test configuration for mistakessudo nginx -tsudo apachectl configtest
Apply changes without downtimesudo systemctl reload nginxsudo systemctl reload apache2
Restart fullysudo systemctl restart nginxsudo systemctl restart apache2
Site configuration folder/etc/nginx/sites-available//etc/apache2/sites-available/
Enable a sitesudo ln -s into sites-enabledsudo a2ensite example.com
Error log/var/log/nginx/error.log/var/log/apache2/error.log
Access log/var/log/nginx/access.log/var/log/apache2/access.log

In both cases, test before you reload. A typing mistake in the configuration can stop the server from restarting, and the test catches it first.

Other Options Worth Knowing

Nginx and Apache are not the only choices.

ServerWhat is differentGood fit
CaddyGets and renews HTTPS certificates automatically, with a very short configuration fileSmall personal servers and Docker setups where simplicity matters
OpenLiteSpeedReads Apache’s .htaccess rewrite rules and has built-in page caching for WordPress through a pluginWordPress-heavy servers that want Apache compatibility with higher speed

For a first VPS, Nginx or Apache will have the widest documentation and the most people who can help you.

Running Both: Nginx in Front of Apache

Some servers run both. Nginx receives visitors on ports 80 and 443, serves static files itself, and passes dynamic requests to Apache running on a private local port. This gives you Nginx’s efficiency with Apache’s .htaccess compatibility.

It also gives you two programs to configure, update, and debug. Control panels sometimes set this up for you. For a single site you manage yourself, it adds complexity that rarely pays for itself. Choose one unless you have a specific reason not to.

How to Choose

Your situationPickWhy
Moving from shared hosting and the site depends on many .htaccess rules or plugin-written rulesApacheYour rules keep working with no translation
Small VPS with 1 or 2 GB of RAMNginxLower memory per connection leaves more headroom
Running several Docker applications behind one entry pointNginxReverse proxying is its strength
Using a control panel such as cPanel or PleskThe one the panel supportsFighting the panel causes trouble
A single WordPress site, and you are comfortable editing configurationEitherThe difference will be small
Many users or clients who need to edit their own rulesApache.htaccess is built for exactly this
You want to learn the most common server setup in modern tutorialsNginxMost current VPS tutorials, including this site’s, use it
You want automatic HTTPS and minimal configurationCaddyFewest moving parts

If you read the table and still cannot decide, choose Nginx for a new VPS, or Apache if you are bringing along a site that was built for it. Either choice is defensible, and neither is permanent.

Changing Your Mind Later

Switching web servers does not touch your site’s files or database. Only the server configuration changes. A safe way to switch:

  1. Install the new server but leave it stopped, or move it to a spare port such as 8080, because two programs cannot listen on port 80 at once. Check what is using a port with sudo ss -tlnp | grep ':80'.
  2. Translate your rules. Go through the site’s .htaccess file or Nginx configuration and recreate each redirect and rewrite in the other format. Recreate the PHP-FPM connection.
  3. Test the new server privately, using the curl --resolve method described in the migration guide, before any visitor reaches it.
  4. Switch the ports. Stop the old server, set the new one to ports 80 and 443, and start it. Check the site immediately.
  5. Keep the old configuration for a week, in case you need to go back.

Security Basics for Both

Neither server is safer by design. Security comes from how you run it.

  • Keep it updated. Run sudo apt update && sudo apt upgrade regularly.
  • Hide the version number. In Nginx, add server_tokens off; inside the http block. In Apache, set ServerTokens Prod and ServerSignature Off. This does not make the server secure, but it stops it from announcing its exact version to automated scanners.
  • Disable what you do not use. Every enabled module is extra code that could contain a vulnerability.
  • Block hidden files. Make sure .git, .env, and backup files cannot be downloaded from the website folder.
  • Use HTTPS everywhere, with the HTTP version redirecting to it.

The wider checklist is in How to Secure a VPS: Essential Setup Checklist.

Common Problems and What They Mean

SymptomLikely causeFix
Homepage works, every other page is a 404Nginx is missing try_files. Apache has mod_rewrite off or AllowOverride NoneAdd the try_files line. Run sudo a2enmod rewrite and set AllowOverride All
Redirects from .htaccess are ignoredThe server is Nginx, which never reads .htaccessTranslate the rules into the Nginx configuration
502 Bad Gateway or a proxy errorThe web server cannot reach PHP-FPM, usually a wrong socket filenameCheck ls /run/php/ and match the path in the configuration
Address already in use on startupAnother program already listens on port 80 or 443Find it with sudo ss -tlnp and stop it
Changes do not appearThe configuration was edited but never reloaded, or a cache is serving the old pageRun the test command, reload, and clear any page cache
403 ForbiddenWrong file ownership or permissions, or a wrong root or DocumentRoot pathCheck the path, and make files readable by the www-data user

Frequently Asked Questions

Which is faster, Nginx or Apache?

For a normal website, the difference is smaller than the difference made by caching, image size, and database speed. Nginx uses less memory under many simultaneous connections and handles static files very efficiently. A well-configured Apache with the event MPM and PHP-FPM is fast enough for most sites.

Which is more secure?

Neither. Both have strong security records when kept updated and configured sensibly. One structural point favors Nginx: it has no .htaccess, so a compromised site cannot rewrite the server’s rules. Another favors neither, because both depend entirely on your updates and your firewall.

Is Apache outdated?

No. It is actively maintained and continues to power a very large share of the web. Its reputation for slowness comes from the older prefork and mod_php setup, which is no longer the sensible default.

Does WordPress work better on one of them?

WordPress works on both. It was built around Apache’s .htaccess, so many plugins assume it and write rules there. On Nginx, those plugins still work, but any rules they would write have to be added to the Nginx configuration by hand.

Can I run both on the same VPS?

Yes, but they cannot both listen on ports 80 and 443. Run one in front on those ports and the other behind it on a local port, or give each its own IP address.

Which should a beginner choose?

The one with the most current tutorials for what you are building. For a fresh WordPress VPS, Nginx tutorials are abundant. If you are moving a site that already relies on .htaccess, start with Apache so the site works on the first day, and learn the translation later.

Pick the One You Can Fix

The web server you can read, test, and repair is better than the one that wins a benchmark. Both will serve your pages. Both will serve them securely if you update them. The differences that matter are .htaccess, memory on a small server, and what your software expects.

Choose on those, write the choice down, and move on to the work that actually makes your site better.

Choose the web server you can configure, read, and fix at 3 a.m.