Question

Should I use Nginx or Apache for my Wordpress Install?

Posted September 11, 2018 28.2k views
NginxWordPressLAMP StackLEMP

tl;dr - Nginx or Apache for WP install, and WHY?

Which of these web server options are the best option for installing and sustaining a Wordpress instance? After reading dozens of articles and forum posts I’m leaning towards Nginx, but there may be some drawbacks as mentioned by @jsamuel, in [this discussion response](https://www.digitalocean.com/community/questions/why-is-apache-used-over-nginx-for-wordpress-one-click?

“It is possible to host WordPress using only Nginx without Apache at all, but because WordPress won’t be able to use .htaccess files (.htaccess files are an Apache feature, they don’t exist in Nginx), you’ll need to regularly be reconfiguring Nginx for various WordPress plugins. You should only take that approach if you’re comfortable with the command line and server administration."answer=31242/)

  1. How To Install WordPress with LEMP on Ubuntu 18.04 - NGINX
  2. How To Install WordPress with LAMP on Ubuntu 18.04 - APACHE
  3. How To Use the WordPress One-Click Install on DigitalOcean - uses Apache

Is the ability to use .htaccess files still a concern if one was to use Nginx? Any clarification on this would be so helpful.

edited by kamaln7
3 comments
  • Not sure on the .htaccess requirement for wordpress plugins, but take a read through @jtittle ’s amazing answer to the “nginx vs apache for wordpress” question here for more context: https://www.digitalocean.com/community/questions/should-i-choose-apache-or-nginx-for-large-wordpress-site-or-best-environment?answer=25336

  • Hey @aha thanks for chiming in! I gleaned some insight from that question and the answer from @jtittle before posting, as I’ve been doing this research. I was hoping for some deeper discussion about this

  • 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 modphp. With modphp, 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 fastcgipass mechanism. This gives it an edge in the benchmarks when compared against apache with modphp.

    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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hey friend!

In terms of performance and sustaining high traffic, Nginx wins every time. It’s been that way for a while now, not quite one of those things that goes back and forth every month or so. Whether it’s a base install or both are tweaked for max performance, in any situation where it’s a 1:1 comparison, Nginx pulls ahead.

As for the .htaccess thing, I’m not sure I share the linked perspective. You need your basic rewrites and perhaps some additional ones to accommodate a good caching plugin. You should be able to find those in template form with light Google searching. Beyond that, I really have never met a plugin that wrote new items to .htaccess that I also recommended to people. There are a lot of very popular Wordpress plugins that I don’t like, and I think you can get along fine without. Security plugins being one of them. I’ll take http auth on wp-login any day, lightweight solution and additional security had by all.

Moving back to web server choices. There is, of course, more to life than benchmarks. When a visitor hits your page and it loads in an acceptable amount of time to not cause them to want to leave, performance is fine. As long as you’re not running Apache with Prefork MPM, most people see no issue based on the web server itself. I use Apache and Caddy, I never use Nginx. I know it’s the best, and I don’t use it. I just don’t like the configuration syntax. I don’t need it, it isn’t my preference, so I don’t use it. Caddy sytnax speaks to me:

domain.tld {
        root /var/www/html
        log /var/log/caddy/domain.log
        errors /var/log/caddy/domain_errors.log
        gzip
        fastcgi / /run/php/php7.2-fpm.sock php {
                ext .php
                split .php
                index index.php
        }
}

So never forget that you have choices. Use what you want unless you need something else. Make it as enjoyable for you as it can be, you’re the one that has to wake up at 3AM and fix it so it might as well represent your style.

Jarland

  • 👊 Awesome reply @jarland! Thank you so much for the explicit response. That’s exactly the type of facts I wanted to be sure that I was considering.

    I’ve read similar things about Nginx. But I couldn’t help but wonder why the “One-click Wordpress Installation” provided by Digital Ocean uses Apache instead or Nginx.

    With the relatively low amount of traffic I anticipate, I think either would be fine, but I’m so glad to have these additional things to consider ツ One thing is certain… I’m so glad I left Godaddy hosting and have come to the DO community!

    • Hi!

      I maintain the DigitalOcean one-click images and did some testing with a LEMP stack for the WordPress image a couple years ago.

      Originally the WordPress one-click was built using a LAMP stack because LAMP was at the time much more widely used and supported by the wider open source community. Most of WordPress’ documentation also assumes you are using Apache and some tweaking is required for WordPress rewrites on Nginx that is not required on Apache.

      When I performed my review we did so to see if switching to LEMP would reduce base memory use on 512MB droplets (not a plan we offer any longer). MySQL can be a memory hog so that was our primary concern. My testing of the two alternatives found little difference in the base memory use.

      The choice to stick with LAMP was made both for those technical reasons but also to ensure that a user spinning up a droplet with the stack would be less likely to encounter confusing information if they run into problems or want to customize their installation.

      Our one-clicks are generally intended to be a quick-start to get going with a particular software stack but they are also often the first experience someone may have with the software. Taking that in mind we try to ensure that the one-click experience encourages that learning and provides something that as much of the community documentation outside DO as well as in our library will be applicable for.

Nginx has been the king for many years. Apache is far behind when it comes to scaling performance on high traffic Wordpress sites and Apache also has no server-level cache. Nginx has blazing fast FastCGI Cache which is built in and scales to incredible numbers....

Your quoted info is simply 100% wrong. Htaccess is not required for Wordpress or plugins to function correctly and actually it is a big security risk because any PHP script can hack your htaccess, which is a very common for malware redirects.

Cache plugins for Wordpress are a very bad idea these days, if you have Nginx FastCGI Cache then you simply don’t need any cache plugins which are bloated anyways and use PHP resources too (in contrast, the FastCGI cache in Nginx is server-level caching, which is lighter/faster).

Lately also Litespeed is bragging about their LS Cache and Apache drop-in replacement being “faster” than Nginx, but its totally bogus and Nginx is surely faster and more scalable than even Litespeed (which includes the janky htaccess stuff just like Apache does). Apache and Litespeed are really aimed at shared hosting and cPanel, which is their official partners too… high traffic sites use Nginx always.

Anyway clouds like DO usually include Apache on backend of Nginx LEMP stacks just for easier file permissions and to make it more user friendly for newbies who use Google tutorials etc.