Guideschevron_rightServers

Servers · 6 min read · Updated

Nginx vs Apache

Nginx and Apache both serve websites reliably. Apache is configured per directory as well as globally, so applications and shared hosting can change its behaviour with .htaccess files. Nginx keeps all configuration in one place and handles connections with an event loop, which makes it light on memory and the usual choice as a reverse proxy in front of apps. On a new VPS, choose Nginx unless your application depends on .htaccess; choose Apache when it does, for example when moving a site from cPanel shared hosting unchanged.

Side by Side

Nginx and Apache compared
AspectNginxApache
Connection modelEvent loop: few workers, many connections eachModules (MPMs): event, worker or prefork
Per-directory configNo; all rules in the server configYes, with .htaccess files
PHPThrough PHP-FPMPHP-FPM, or mod_php with the prefork MPM
Reverse proxyThe common choiceCapable (mod_proxy)
Static files, many idle clientsVery light on memoryHeavier, especially with prefork
Typical useNew sites, apps behind a proxy, TLS in frontShared hosting, apps that ship .htaccess rules

How Apache Works

Apache loads a multi-processing module (MPM) that decides how requests are handled. The event MPM, the default on current Debian and Ubuntu, is efficient; prefork runs one process per connection and is needed for the old mod_php. Apache's strength is flexibility: with AllowOverride enabled it reads .htaccess files in each directory, so an application can set its own redirects and rewrite rules without touching the server configuration. That is why shared hosting runs on it, and why it checks the file system for those files on every request.

How Nginx Works

Nginx runs a few worker processes, each handling thousands of connections with an event loop. It serves static files directly and hands everything else to an application server: PHP-FPM, a Node.js or Python app, or another web server. There are no per-directory files; all rules live in the site's server block and take effect on reload. That makes it predictable and fast, at the cost of converting any .htaccess rules yourself.

Installing Either One

# Nginx
sudo apt install nginx
sudo nginx -t && sudo systemctl reload nginx

# Apache
sudo apt install apache2
sudo apachectl configtest && sudo systemctl reload apache2

Check the configuration before every reload, as above: a reload with a broken file is refused, but a restart takes the site down. Hosting a website on a VPS walks through a complete Nginx setup with HTTPS.

Which to Choose

  • A new site, a static site, or an app (Node.js, Python, Go) behind a proxy: Nginx.
  • A site moved from cPanel shared hosting that relies on .htaccess: Apache, at least at first. Moving from shared hosting covers the move.
  • WordPress: either, with PHP-FPM. Apache if your plugins write .htaccess rules.

Common Questions

  • Is Nginx faster than Apache?

    For serving static files and holding many idle connections, Nginx usually uses less memory. For dynamic sites the difference mostly disappears, because the time goes into PHP, Python or the database rather than the web server. Configure either one well and the web server is rarely the bottleneck.

  • Does Nginx support .htaccess?

    No. Nginx reads its configuration only at start and reload, so rules from .htaccess files have to be rewritten into the site's server block. Most common rules, such as redirects and WordPress permalinks, take a line or two.

  • Can I use Nginx and Apache together?

    Yes. A common setup puts Nginx in front to handle HTTPS and static files and pass the rest to Apache on a local port, which keeps .htaccess working for the application. It adds a moving part, so do it only when you need both.

  • Which is better for WordPress?

    Both run WordPress well with PHP-FPM. Apache is easier when plugins write .htaccess rules (some caching and security plugins do); Nginx is leaner when you are happy to put those rules in the server configuration yourself.

Run Nginx or Apache on a KVM VPS with full root access, NVMe storage and a free host firewall.

See VPS plans

Keep Reading

All guides
Nginx vs Apache | Virteche Cloud