Nginx or Apache : Best server for Magento

Nginx or Apache : Best server for Magento

Introduction

Apache server has been for years been the default http server linux hosts use. However, recently there have been many newer “lighter” http servers. This blog article focuses on Magento hosting. Magento is a php based web eCommerce framework. Nginx requires php-fpm to process php requests. So, this comparison is really apache vs nginx + php-fpm. Apache offers MPM (Multi-Processing Module) configurations pre-fork, worker and event. In this discussion we will use the “event” MPM.
This discussion is very popular. Examples include this. We focus on Magento here.

Key Differences between apache and nginx

There are some differences architecturally that make nginx look slightly better for Magento hosting.

  • Apache uses mod-php for interpreting php. That means each thread of apache that processes any request is capable of interpreting php. If php did not leak memory this would not be a big problem. But, php does leak memory and the process running php keeps growing. Both php-fpm and apache give a way to restart the thread every max_connections (or MaxRequestsPerChild in apache). However, in case of php-fpm only php requests are counted towards the max_connections while any request is counted for apache. By separating handling of static content from php, nginx + php-fpm solution has a superior architecture.
  • Apache’s flexibility is in its use of .htaccess. However, this requires apache to read and interpret .htaccess on each access. On the other hand, nginx does not support .htaccess and as a result is more efficient.
  • Nginx is a superior software load balancer – supporting http, https and fast-cgi. We like the fast-cgi for Magento with https terminating on the load balancer. The weighted load balancing allows uneven distribution, typical in Magento when cloud servers may be added to handle peak loads. Apache mod_proxy_balancer is not as configurable.

Here is a comparison for the various configurations

Nginx + php-fpm Apache
If no CDN – i.e. images, css and js files served from server.
  • Lower Memory footprint
  • Easy to configure php processes

 

  • Higher memory footprint
  • Harder to configure – leading to even more memory usage
If using CDN for all static assets on a single server
  • Nginx is used as a front for php – one more hop

 

  • Apache processes the php in threads directly

 

Multiple app servers without external load balancer
  • fastcgi_pass can be used to load balance. Only one server needs nginx, other servers only need php-fpm <code>
  • Even with reverse proxy on apache, all servers need apache.

Conclusion

We think production servers should be setup using nginx. Configuring is not the easiest, though. We will discuss configuration in a future article.

Share this post