Apache vs Nginx for PrestaShop: Which Web Server Performs Better

388 views

Why the Web Server Choice Matters for PrestaShop

PrestaShop is a PHP application that generates HTML pages dynamically, serves static assets like images, CSS, and JavaScript files, and handles AJAX requests from both the front office and the back office. The web server sits between your visitors and the PHP application, handling every single HTTP request. Its architecture directly affects how many concurrent visitors your store can handle, how fast pages load, and how much server memory each visitor consumes.

Apache and Nginx are the two dominant web servers for hosting PrestaShop. Apache has been the default choice since PrestaShop's earliest versions, and PrestaShop ships with .htaccess files designed specifically for Apache. Nginx has gained massive adoption over the past decade because of its superior handling of concurrent connections and lower memory footprint. Both can run PrestaShop effectively, but they differ in ways that matter for store performance, configuration complexity, and operational overhead.

Architecture: Process Model vs Event Loop

The fundamental difference between Apache and Nginx is how they handle incoming connections. This architectural difference drives every performance difference between the two.

Apache's Process/Thread Model

Apache traditionally uses a process-based model through its prefork MPM (Multi-Processing Module). In this model, Apache spawns a pool of worker processes, and each process handles one request at a time. When a request comes in, one process is assigned to it. That process reads the request, executes the PHP code (if using mod_php), sends the response, and only then becomes available for the next request.

The worker MPM uses threads instead of separate processes, allowing more concurrent connections with less memory. The event MPM goes further by using an event-driven approach for keepalive connections while still using threads for active request processing. Modern Apache installations typically use the event MPM, but the fundamental model still involves dedicating a thread to each active request.

The practical implication is that Apache's concurrency is limited by the number of configured worker threads or processes. If you configure 150 workers and 151 requests arrive simultaneously, the last request waits in a queue. Each worker consumes memory (typically 10-30 MB per process with mod_php, less with PHP-FPM), so the maximum number of workers is constrained by available RAM.

Nginx's Event-Driven Model

Nginx uses an asynchronous, event-driven architecture. A small number of worker processes (typically one per CPU core) handle thousands of connections simultaneously using an event loop. When a request arrives, Nginx processes it in a non-blocking manner. If Nginx needs to wait for something (a response from PHP-FPM, a file read from disk, a response from a backend server), it does not block the worker. Instead, it moves on to handle other connections and comes back when the waited-on operation completes.

This means an Nginx worker process handling 1,000 concurrent connections uses roughly the same memory as when handling 10 connections. The memory footprint is determined by the number of worker processes (a handful), not the number of connections (potentially thousands). This is why Nginx excels under high concurrency and why it is the preferred choice for high-traffic sites.

.htaccess vs Server Configuration

One of the most significant practical differences between Apache and Nginx is how URL rewriting, access control, and other per-directory configurations are handled.

Apache and .htaccess

Apache supports .htaccess files, which are per-directory configuration files that can override server-wide settings. PrestaShop relies heavily on .htaccess for its friendly URL rewriting, access control, caching headers, and security headers. When you enable friendly URLs in PrestaShop, it generates an .htaccess file with mod_rewrite rules that translate clean URLs like /shoes/running-shoe into the internal dispatcher URL.

The advantage of .htaccess is convenience. You do not need root access to modify web server behavior, and changes take effect immediately without restarting the server. PrestaShop can write its own .htaccess file from the back office, which means features like friendly URLs, media server configuration, and certain security settings work out of the box.

The disadvantage is performance. Every request causes Apache to search for and parse .htaccess files in every directory from the document root to the requested file's directory. On a typical PrestaShop request, Apache might check for .htaccess in /, /var, /var/www, /var/www/html, and more. This adds disk I/O and processing time to every request. For a site handling hundreds of requests per second, this overhead is measurable.

If you have root access to the Apache configuration, you can move all .htaccess directives into the VirtualHost configuration and disable .htaccess processing with AllowOverride None. This eliminates the per-request overhead while keeping the same functionality. However, changes then require an Apache reload to take effect.

Nginx Configuration

Nginx does not support .htaccess files. All configuration lives in the server block configuration files, typically under /etc/nginx/sites-available/ or /etc/nginx/conf.d/. Every URL rewriting rule, access control directive, and caching header must be defined in these files.

This means that when you set up PrestaShop on Nginx, you must manually translate PrestaShop's .htaccess rules into Nginx configuration syntax. The rewrite rules are fundamentally different between the two servers. Apache uses RewriteRule with regex patterns and flags like [L,R=301], while Nginx uses location blocks with try_files, rewrite, and return directives.

The advantage of centralized configuration is performance (no per-request file scanning) and clarity (all rules in one place). The disadvantage is that PrestaShop cannot generate the Nginx configuration for you. You must write and maintain it yourself, and any change requires running nginx -t to test the syntax and systemctl reload nginx to apply it.

PHP Integration

Both web servers need to execute PHP code to generate PrestaShop pages. The method of PHP integration affects performance, stability, and resource management.

Apache with mod_php

The traditional approach is Apache with mod_php, where PHP runs as a module inside each Apache process. This is simple to set up and has zero inter-process communication overhead because PHP executes in the same process that handles the HTTP request. However, every Apache worker process carries the full PHP runtime in memory, even when serving static files like images or CSS. This wastes memory because the majority of requests to a PrestaShop store are for static assets, not PHP pages.

Apache or Nginx with PHP-FPM

PHP-FPM (FastCGI Process Manager) runs PHP as a separate process pool. The web server communicates with PHP-FPM over a Unix socket or TCP connection using the FastCGI protocol. When a request requires PHP processing, the web server forwards it to PHP-FPM. When the PHP processing is complete, PHP-FPM sends the result back to the web server, which sends it to the client.

With PHP-FPM, the web server processes that handle static files do not carry the PHP runtime, saving significant memory. PHP-FPM also offers its own process management with features like dynamic pool sizing, slow log (logging when a request takes longer than a threshold), and the ability to run multiple PHP versions simultaneously for different sites.

Nginx exclusively uses PHP-FPM because its event-driven architecture is incompatible with embedding PHP. Apache can use PHP-FPM through mod_proxy_fcgi. In modern deployments, PHP-FPM is the recommended approach for both servers.

PHP-FPM Configuration for PrestaShop

Regardless of which web server you choose, PHP-FPM configuration significantly affects PrestaShop performance. Key settings include:

pm = dynamic is usually the best process manager mode. It starts a base number of workers and spawns more under load, up to a configured maximum. This balances memory usage and responsiveness.

pm.max_children determines the maximum number of PHP processes. Each PrestaShop request typically uses 30-80 MB of memory, so divide your available RAM (minus what the web server and OS need) by 80 to get a conservative maximum. For a server with 4 GB of usable RAM, 50 children is a reasonable starting point.

pm.max_requests = 500 recycles each worker after 500 requests, preventing memory leaks from accumulating. PrestaShop modules occasionally have minor memory leaks, and this setting prevents them from becoming a problem.

Static File Serving

A PrestaShop store serves large numbers of static files: product images, CSS stylesheets, JavaScript files, fonts, and media uploads. Static file serving performance directly affects page load times and server resource usage.

Apache Static File Performance

Apache serves static files through its worker processes. Each static file request occupies a worker for the duration of the transfer. For small files (CSS, JS, small images), this is fast. For large files or slow client connections, the worker is tied up for longer. With mod_php loaded, each worker carries unnecessary PHP memory overhead even for static requests.

Apache supports sendfile, which allows the kernel to transfer files directly from disk to the network socket without copying data through user space. This is enabled by default and helps with large file transfers. Apache also supports content negotiation, byte ranges, and conditional requests (If-Modified-Since) out of the box.

Nginx Static File Performance

Nginx excels at static file serving because its event-driven model can handle thousands of concurrent file transfers without proportionally increasing resource usage. A single Nginx worker process can serve hundreds of simultaneous static file requests. Combined with Nginx's efficient use of the sendfile system call and its built-in support for open file cache (caching file descriptors for frequently accessed files), static file serving is remarkably fast.

For PrestaShop stores with many product images (which is most stores), Nginx's static file performance advantage is significant. Product pages with 5-10 images, plus CSS, JavaScript, and font files, generate 20-30 static file requests per page load. Under high traffic, Nginx handles these with substantially fewer resources than Apache.

SSL/TLS Termination

Every PrestaShop store should run on HTTPS, and the web server handles the SSL/TLS encryption and decryption (termination). Both servers support modern TLS well, but there are differences in configuration and performance.

Apache configures SSL through mod_ssl, with directives like SSLEngine on, SSLCertificateFile, and SSLProtocol in the VirtualHost block. OCSP stapling, session caching, and cipher suite selection are all configurable.

Nginx configures SSL in the server block with directives like ssl_certificate, ssl_protocols, and ssl_ciphers. Nginx also supports OCSP stapling and session caching, and its configuration tends to be more concise.

Performance-wise, the TLS handshake is CPU-intensive due to the asymmetric encryption involved. Nginx's ability to handle many concurrent connections with few worker processes means it can handle more TLS handshakes per second with less memory. For stores that receive large bursts of new visitors (during a sale, for example), Nginx's TLS performance advantage can prevent connection queuing.

Reverse Proxy and Load Balancing

For high-traffic PrestaShop stores, a reverse proxy architecture separates concerns and improves scalability. The most common setup uses Nginx as a reverse proxy in front of either Apache or another Nginx instance running PHP-FPM.

Nginx as a Reverse Proxy for Apache

This hybrid setup combines the strengths of both servers. Nginx sits in front, handling all incoming connections, serving static files directly, managing SSL termination, and forwarding only PHP requests to Apache. Apache handles the PHP processing, and because it only receives PHP requests (not static file requests), it needs far fewer worker processes.

This architecture gives you Nginx's connection handling efficiency and static file performance while preserving Apache's .htaccess support for PrestaShop's generated rewrite rules. It is a common migration path for stores that want Nginx's performance without rewriting all their Apache configuration.

The Nginx proxy configuration uses the proxy_pass directive to forward requests to Apache, typically running on a non-standard port like 8080. Static files are served directly by Nginx using a location block that matches file extensions like .jpg, .css, .js, and .png.

Full Nginx Setup

For maximum performance, Nginx serves everything: static files and PHP requests (via PHP-FPM). There is no Apache in the stack. This eliminates the inter-process communication between Nginx and Apache and removes the memory overhead of running two web servers. However, it requires manually creating and maintaining the Nginx configuration for PrestaShop's URL rewriting, which is more initial work.

Recommended Nginx Configuration for PrestaShop

A production Nginx configuration for PrestaShop needs to handle friendly URLs, admin panel access, static file caching, PHP-FPM communication, and security. The key elements include a server block listening on ports 80 and 443, an SSL configuration block with your certificates, a root directive pointing to your PrestaShop installation, and several location blocks.

The main location block uses try_files $uri $uri/ /index.php?$args to handle PrestaShop's friendly URLs. This tries to serve the request as a static file first, then as a directory, and finally passes it to the PrestaShop dispatcher through index.php.

A location block matching ~ \.php$ forwards PHP requests to PHP-FPM using fastcgi_pass. It includes the standard FastCGI parameters and sets the SCRIPT_FILENAME to the correct path.

A location block for static assets sets long cache expiration headers and turns off access logging to reduce I/O. Matching patterns like \.(jpg|jpeg|gif|png|svg|css|js|ico|woff|woff2|ttf|eot)$ capture the common static file types.

Security-related location blocks deny access to sensitive files and directories: .htaccess, .git, config/ (except config/xml/ which PrestaShop needs), vendor/, app/config/, and other directories that should never be web-accessible.

Recommended Apache Configuration for PrestaShop

Apache with the event MPM and PHP-FPM provides the best Apache-based PrestaShop hosting. The VirtualHost configuration should enable the modules rewrite, headers, expires, deflate, and proxy_fcgi.

The DocumentRoot points to your PrestaShop installation. AllowOverride All enables .htaccess processing, which is needed unless you move all PrestaShop's rewrite rules into the VirtualHost config.

PHP-FPM integration uses either SetHandler or ProxyPassMatch to forward .php requests to the PHP-FPM socket. The SetHandler approach with proxy:unix:/run/php/php-fpm.sock|fcgi://localhost is simpler and is recommended for most setups.

Enable gzip compression with mod_deflate for text-based content types: HTML, CSS, JavaScript, JSON, XML, and SVG. Enable browser caching with mod_expires, setting long expiration times for static assets and shorter times for HTML.

Performance Benchmarks and Real-World Differences

Raw benchmark numbers vary enormously based on hardware, configuration, PHP version, and PrestaShop version. Rather than presenting specific numbers that would be misleading out of context, here are the patterns that consistently emerge in PrestaShop hosting benchmarks.

For static file serving, Nginx is consistently 2-3 times faster than Apache and uses significantly less memory. This gap widens as concurrency increases. At 100 concurrent connections, the difference might be 20%. At 1,000 concurrent connections, Nginx might use 10 times less memory.

For PHP request processing, the web server is not the bottleneck. PHP-FPM processing time dominates the request lifecycle, and both web servers add negligible overhead compared to the time PHP spends executing PrestaShop code, querying the database, and rendering templates. The difference in PHP request handling between Apache with PHP-FPM and Nginx with PHP-FPM is typically under 5%, which is within measurement noise for most stores.

For mixed workloads (the realistic scenario), Nginx's advantage comes from its efficient handling of static files alongside PHP requests. A page load generates one PHP request and 20-30 static file requests. Nginx handles the 20-30 static requests with trivial resource usage, while Apache assigns a worker thread to each one. Under high traffic, this difference in resource consumption means Nginx can sustain higher concurrency before performance degrades.

The practical conclusion is that for stores with fewer than 50 concurrent visitors, the web server choice makes almost no noticeable difference. For stores approaching or exceeding 100 concurrent visitors, Nginx's architectural advantages become meaningful.

Migration Guide: Apache to Nginx

Migrating an existing PrestaShop store from Apache to Nginx involves translating the configuration, testing thoroughly, and switching over.

Step 1: Translate the .htaccess Rules

Open your PrestaShop .htaccess file and identify all the active rewrite rules. The critical section is the friendly URL rewriting, which typically starts with RewriteCond %{REQUEST_FILENAME} !-f and RewriteRule .* - [E=REWRITEBASE:/]. These translate to the Nginx try_files directive mentioned in the configuration section above.

Media server rewrites, language prefix handling, and any custom redirects also need translation. Each Apache RewriteRule and RewriteCond pair must be converted to the equivalent Nginx location, rewrite, or return directive.

Step 2: Set Up Nginx Alongside Apache

Install Nginx and configure it to listen on a different port (like 8080) while Apache continues running on port 80. This lets you test the Nginx configuration without affecting the live site. Point Nginx at the same document root as Apache so it serves the same files.

Step 3: Test Everything

Access the site through Nginx's port and test every aspect: the front page, category pages, product pages, the cart, checkout, the admin panel, friendly URLs, image loading, and multilingual URL routing. Pay particular attention to URL patterns that involve special characters or query parameters.

Step 4: Switch Over

Once testing is complete, stop Apache and reconfigure Nginx to listen on port 80 and 443. Reload Nginx and verify the live site works correctly. Keep Apache's configuration intact for a few days in case you need to roll back.

Common Migration Issues

The most common issue is missing rewrite rules for PrestaShop's multilingual URL routing. If your store uses multiple languages with language codes in the URL (like /en/, /de/, /fr/), make sure the Nginx configuration handles these prefixes correctly.

Another common issue is file upload size limits. Apache uses LimitRequestBody while Nginx uses client_max_body_size. If you import products with large images, set client_max_body_size to at least 20M.

Admin panel AJAX requests that rely on .htaccess rewriting may break if the corresponding Nginx rules are missing. Test the admin panel thoroughly, including product editing, order management, and module configuration.

Which Should You Choose

Choose Apache if you are on shared hosting where you do not control the web server, if you rely heavily on .htaccess for configuration (module-generated rules, security plugins), or if you are not comfortable writing and maintaining Nginx configuration files. Apache with the event MPM and PHP-FPM is a solid, well-supported setup for PrestaShop stores of moderate traffic.

Choose Nginx if you have root access to your server, your store handles significant traffic (hundreds or thousands of concurrent visitors), you want the lowest possible resource usage for a given traffic level, or you are setting up a new server and prefer the long-term benefits of Nginx's architecture. The initial configuration effort is a one-time cost that pays dividends in performance and resource efficiency.

Choose the Nginx reverse proxy in front of Apache approach if you want Nginx's performance for static files and connection handling but need Apache's .htaccess support for compatibility with PrestaShop modules that generate or depend on .htaccess rules.

For most new PrestaShop installations on a VPS or dedicated server, Nginx with PHP-FPM is the recommended choice. The configuration is well-documented, the performance advantages are real, and the operational simplicity of a single web server stack reduces maintenance overhead. For existing stores on Apache that are performing adequately, migration to Nginx is a worthwhile optimization but not an urgent necessity unless you are hitting performance limits.

For more details, read our guides: Choosing Hosting for PrestaShop: What Matters and What Is Marketing and Performance Tuning Your PrestaShop Store: From Database Queries to Full Page Cache.

Was this answer helpful?

Still have questions?

Can't find what you're looking for? Send us your question and we'll get back to you quickly.

Loading...
Back to top