Nginx or Apache : Best server for Magento
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
|If no CDN – i.e. images, css and js files served from server.
|If using CDN for all static assets on a single server
|Multiple app servers without external load balancer
We think production servers should be setup using nginx. Configuring is not the easiest, though. We will discuss configuration in a future article.