Nginx best high performance caching configuration for WordPress

August 25, 2019 262 views
Nginx PHP Caching WordPress Server Optimization Ubuntu 18.04

I am planning to deploy an optimized LEMP stack on DigitalOcean on Ubuntu 18.04 for my WordPress site. I have many question and confusion related to caching. I hope I will get all my answer here.

  1. Are PHP-FPM and Varnish technology the same. Which one should I go for, Varnish or PHP-FPM for my Nginx server?

  2. According to my research, a combination of Redis and PHP-FPM gives best performance. Is this right or should I go for radis+varnish?

  3. As I know, Redis cache database query. So, if Redis only cache database then how PHP-FPM/Varnish works together with Redis to produce the web page very fast?

  4. As I know Nginx can’t process PHP request, so by default PHP-FPM comes here to serve the PHP request, Right? If right then PHP-FPM should already be configured in all the server set up. Am I right? I am using all the DIGITALOCEAN tutorials to set-up my server.

  5. After all the above set-up, do I need to use any caching plugin on top of my website?, e.g., WP-rocket or Swift Pro.

  6. A caching plugin also generates static HTML pages of a website and saves it on the server as PHP-FPM or Varnish do. So why we should use both caching plugin and server-level cache?

  7. Without installing a caching plugin, will my website give browser cache facility with the above set-up? Or it is necessary to use plugin to get the browser caching facility.

If you have more recommendation then please guide me. Thank you!

1 Answer
bobbyiliev MOD August 27, 2019
Accepted Answer

Hello,

I’ll try to answer all of your questions:

  1. PHP-FPM and Varnish are not the same in any way, you should not confuse the two, they can not substitute each other. Varnish is simply a HTTP accelerator it would simply cache your HTTP requests. PHP-FPM on the other side is actually going to do some work and is going to be processing your PHP requests passed from Nginx.

  2. Yes PHP-FPM and Redis is a great combination, just make sure that you have enough RAM available for Redis on your droplet or try suing the new Redis managed databases provided by DigitalOcean. You can of course add Varnish on top of that but it can not be a substitution for PHP-FPM in any way, it would only stay on port 80 and proxy all of the traffic to Nginx.

  3. Yes, you can configure PHP-FPM to work with Redis with the PHP-FPM redis extension

  4. Yes this is correct, Nginx would basically be proxying the PHP requests to PHP-FPM. It is configured in the LEMP droplet from the marketplace and it is included as a step in the tutorials.

  5. Yes, you can always add a caching plugin. If you have Nginx + PHP-FPM + Redis it would be quite beneficial to use a plugin.

  6. PHP-FPM does not actually generate static pages but it processes your PHP requests. On the other side Varnish would do that but Varnish is only able to listen on port 80 so you would need to either configure another service to listen on port 443 (https) and proxy the connections to Varnish so that your site could have an SSL certificate.

  7. You don’t need a plugin for browser caching, you can configure that in your Nginx config.

My recommendation would be stick to Nginx + PHP-FPM + Redis + A caching plugin at first and see how it performs. Nginx is quite fast any way. As a test you could add Varnish as I already mentioned it does not support SSL, so you would need to either configure your Nginx to handle your https requests anyway and proxy to Varnish and then proxy back to Nginx, in this case you might have to do more tweaking to get the configuration and the proxying correct.

Hope that this helps!
Regards,
Bobby

  • Hello Bobby!

    Thank you so much for taking the time to explain everything to me.

    Please clear some more doubt that I have in my mind:-

    1. Can you tell me why cache plugin like WP-ROCKET is needed if there is already caching enabled on the server-side by PHP-FPM and Redis?

    2. PHP-FPM also cache all the page to serve it faster without rendering every time, and WP-ROCKET or any other cache plugin also cache all the website content to serve it more quickly. So my question is why to use both if PHP-fpm is doing the same as wp-rocket does?

    3. Wp-rocket cache all the website contents in a location e.g:- Html/www/wp-rocket/cache. I think PHP-fpm also does the same but in their respective location. So how a website will decide which cached file will be served to the user?

    4. How Redis works in between php-fpm and cache plugin? How it serves the cached database query to php-fpm and users.

    5. Please tell me how Redis, wp-rocket cache, and php-fpm cache work together to give the best performance.

    I hope you understand my question. Thank you in advance!

    Best regards,
    Brajmohan

    • Hello,

      So PHP-FPM is not a caching solution, it is your PHP handler which is handling your PHP requests. You could enable PHP modules like OPcache which improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

      But PHP-FPM would not store static files out of the box. PHP-FPM is no where near a caching service but a PHP handler which is required by WordPress as WordPress is written in PHP.

      Regarding the other questions, there are a lot of different caching types like browser caching and page caching, mobile and user caching and etc. WP Rocket would take care of those types of caching for you.

      Redis on the other side is a persistent object caching. So in order for your WordPress site to use it you would need a plugin like Redis Object Cache.

      Hope that this makes things clearer.
      Regards,
      bobby

Have another answer? Share your knowledge.