Rahul Nagare
Posted on January 10, 2020
When it comes to scaling any PHP application, this is a common question. Apache or Nginx or Apache with Nginx? I think this is as important a question as tabs vs. spaces.
Apache gets much hate for not being scalable or reliable. This hate comes from the old days of poor defaults and mod_php. With mod_php, apache spawns a PHP parser even when static files are requested, and that results in higher memory usage per child process. In other words, you end up using ~200 MB memory when processing WordPress files and another ~200 MB memory to serve each image that goes along with the site.
Throw this configuration in a high traffic situation, and like clockwork, it crashes when the memory runs out.
In reality, apache httpd powers popular sites like apple.com and adobe.com. If you use php-fpm with httpd, it scales very well for WordPress, and you get to keep your .htaccess rules.
Nginx uses php-fpm by default with the fastcgi_pass mechanism. This gives it an edge in the benchmarks when compared against apache with mod_php.
If you compare Nginx and Apache side by side with both using php-fpm, the results are almost identical.
So which one is better?
Well, it comes down to the team that needs to support this site regularly. If the team is comfortable with httpd, go for it. If they are comfortable with nginx, use that instead.
Some people like to use nginx in front of httpd to serve static files and let apache handle php requests. While this provides the best of both worlds, I prefer keeping my stacks lean. Lean stacks make troubleshooting easier when things go wrong.
One argument I hear a lot against using only nginx is that you don’t get to use .htaccess, which may break WordPress plugins.
I manage ~15,000 wp sites, and for 99% of them, the following nginx config is enough to replace htaccess:
location / {
try_files $uri $uri/ /index.php?$args;
}
For anything else more complicated, offloading it to 3rd party services (WAFs, CDNs) scales better.
The bottom line is you can get identical performance with Apache or Nginx when using php-fpm. Go with the one you are comfortable with and look for optimizations on another layer. Unless there is a clear need for it, avoid apache+nginx and keep your stack lean.
P.S.: Apache httpd has come a long way from its mod_php days. You can even use Lua scripts with httpd just like Nginx and Resty!
Posted on January 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.