How to setup wordpress for nginx

Configure wordpress for nginx: In the article I describe how to configure caching plugins for wordpress on nginx (WP Super Cache, WP Rocket and W3TC).

How to setup wordpress for nginx

The contents of the article

  • How to setup wordpress for nginx
  • Prerequisites for running the tutorial
  • The best caching plugins for wordpress
  • Optimize wordpress with nginx
  • Configure WP Rocket with nginx
  • Configure W3 Total Cache with nginx
  • Configure WP Super Cache with nginx
  • Conclusions

Have you recently installed wordpress on LEMP stack with nginx and would like to configure a caching plugin to speed up your blog? Not sure which caching plugin to  use to make your website perform better? Don’t know how to configure wordpress for nginx ?

Then follow me because in this article I will show you how to best configure nginx and wordpress for the three main caching plugins  namely WP Super Cache , WP Rocket and W3 Total Cache

Do you want to know if the  performance  of your wordpress blog could be improved? Interested in knowing how to  configure a caching plugin for your blog on  LEMP stack with nginx ?

CONTACT ME

Prerequisites for running the tutorial

As an indispensable prerequisite you must have a dedicated VPS with pre-installed the LEMP stack or Linux (in this tutorial I use Ubuntu 18.04 ), nginx, MySQL, php and above all wordpress installed and running.

If you don’t know what the LEMP stack is or you want to know how to install it on a dedicated VPS then I recommend this article of mine:

 

YOU MAY BE INTERESTED IN …How to install LEMP stack on VPS (Linux NGINX MySQL php)

If you don’t know how to install wordpress on LEMP stack I recommend this article of mine:

 

YOU MAY BE INTERESTED IN …how to install wordpress on LEMP stack (Linux NGINX MySQL php)

Also you must have a user (possibly not root) with sudo access rights

The best caching plugins for wordpress

There are many caching plugins for wordpress or plugins that allow you to create copies on disk (or even in memory ) of the most used web pages of the blog. In this way, when a user requests a page, the cached version (static on file) is provided instead of the runtime version (generated by php with database access).

Using a caching plugin for wordpress is a good practice for a number of reasons such as:

  • speeding up page loading
  • reduction of the web server load
  • integration with external cdn (Cloudflare, KeyCDN, …)
  • compression of contents

In this article I will show you how to best configure the wordpress and nginx pair for the following caching plugins: WP Rocket , W3 Total Cache  and WP Super Cache

[sociallocker] [/ sociallocker]

Optimize wordpress with nginx

Before seeing how to configure caching plugins it is necessary to review the general configuration of nginx for optimization with wordpress.

If you followed my previous tutorials on how to install nginx and how to install wordpress you will find yourself in the situation where nginx is installed and configured in the minimal version for the correct functioning of wordpress.

I remind you that in the tutorials I will use the following directories for the nginx configuration file and the wwwroot that is the home directory of the webserver in which to install wordpress:

/ etc / nginx / sites-available / wordpress => nginx configuration file

/ var / wwwroot / html / => wordpress home directory

Then proceed with editing the configuration file with the command

$ cd / etc / nginx / sites-available / $ sudo nano wordpress

and add

location ~ /\.ht {     deny all;}

to inhibit any configurations made with apache .htaccess files

Configure WP Rocket with nginx

To configure WP Rocket with nginx you must first proceed with the installation and activation of the plugin that you can find at this link .

Once activated it is necessary to proceed with the installation of the rocket-nginx component of SatelliteWP , a simple tool that will allow you to obtain an optimal configuration of nginx with WP Rocket, the original guide can be found at this link .

First, access the default root of nginx and download the tool with the commands

cd / etc / nginxgit clone https://github.com/satellitewp/rocket-nginx.git

Then go to the newly generated directory and create the wprocket configuration file for nginx with the commands:

cd rocket-nginxcp rocket-nginx.ini.disabled rocket-nginx.iniphp rocket-parser.php

At the end you will have generated the new default.conf file with the necessary settings and that you will have to include in the nginx configuration file, using the command:

$ cd / etc / nginx / sites-available / $ sudo nano wordpress

Add bold text in the “location” section:

location / {             try_files $ uri $ uri / = 404;             # Rocket-Nginx Configuration              includes rocket-nginx / default.conf; }

At this point you can check the correctness of the settings with the command:

$ sudo nginx -t

If everything is ok then you can restart nginx

$ sudo systemctl reload nginx

The configuration is finished.

Configure W3 Total Cache with nginx

To configure W3 total cache with nginx you must first proceed with the installation and activation of the plugin.

Once activated, the plugin automatically understands (and it is the only one of the three that does) that the webserver you are using is nginx, so it creates the nginx.conf file in the wwwroot of your website:

/ var / wwwroot / html / nginx.conf

The file already contains all the necessary settings , the result of the plugin settings, for the correct functioning of the cache with nginx , so you will only have to enter the correct path of this file in the nginx configuration file, using the command:

$ cd / etc / nginx / sites-available / $ sudo nano wordpress

And add the bold text in the “location” section:

location / {             try_files $ uri $ uri / = 404;             # W3TC Configuration              include /var/www/html/nginx.conf; }

At this point you can check the correctness of the settings with the command:

$ sudo nginx -t

If everything is ok then you can restart nginx

$ sudo systemctl reload nginx

The configuration is finished.

Configure WP Super Cache with nginx

To configure WP Super Cache with nginx you must first proceed with the installation and activation of the plugin.

Once the plugin is activated it is necessary to make some manual changes to the configuration files to optimize it.

Start by creating the wp-supercache.conf file in the snippets directory with the command:

sudo nano /etc/nginx/snippets/wp-supercache.confinsert the following code in the file set $ cache_uri $ request_uri; # POST requests and urls with a query string should always go to PHPif ($ request_method = POST) {set $ cache_uri ‘null cache’;} if ($ query_string! = “”) {set $ cache_uri ‘null cache’;} # Don’t cache uris containing the following segmentsif ($ request_uri ~ * “(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php| wp -. *. php | / feed / | index.php | wp-comments-popup.php| wp-links-opml.php | wp-locations.php | sitemap (_index) ?. xml| [a-z0-9 _-] + – sitemap ([0-9] +) ?. xml) “) {set $ cache_uri ‘null cache’;} # Don’t use the cache for logged-in users or recent commentersif ($ http_cookie ~ * “comment_author | wordpress_ [a-f0-9] +| wp-postpass | wordpress_logged_in “) {set $ cache_uri ‘null cache’;} # Set the cache fileset $ cachefile “/wp-content/cache/supercache/$http_host/$cache_uri/index.html”; if ($ https ~ * “on”) {set $ cachefile “/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html”;} # Add cache file debug info as header#add_header X-Cache-File $ cachefile; # Try in the following order: (1) cachefile, (2) normal url, (3) phplocation / {#try_files $ cachefile $ uri $ uri / /index.php;try_files $ cachefile $ uri $ uri / /wp/index.php?q=uri&$args;}

Save the file and close the editor.

Now all you have to do is include the file in the main nginx file with the command

$ cd / etc / nginx / sites-available / $ sudo nano wordpress

Add bold text in the “location” section:

location / {             try_files $ uri $ uri / = 404;             #WP Super Cache Configuration              includes snippets / wp-supercache.conf; }

At this point you can check the correctness of the settings with the command:

$ sudo nginx -t

If everything is ok then you can restart nginx

$ sudo systemctl reload nginx

The configuration is finished.

Conclusions

In this article I showed you how best to configure the three main plugin for wordpress dedicated to caching php files as having nginx webserver.

All three plugins are excellent, in this article I compare their strengths and weaknesses in a standard configuration that is with apache and shared hosting.

What do you think of this tutorial? Do you think that you too will migrate to dedicated VPS with nginx and wordpress? Which of the three plugins do you prefer?

 

Leave a Comment

Scroll to Top