Setup Nginx + WordPress + W3 Total Cache In CentOS Server

Sunday, July 10th 2016. | how to

#howto ,#tutorial #VPS – Setup Nginx + WordPress + W3 Total Cache In CentOS Server – This tutorial explains step by step guide (with pics) about Setup Nginx + WordPress + W3 Total Cache In CentOS Server

How can I install and use W3 Total Cache plugin in my WordPress blog hosted in CentOS Nginx server? Then you’ll need a special configuration in your Nginx server block. For a refresh, Nginx has no support for .htaccess file as what Apache does. However W3TC plugin needs special entries in .htaccess file which can be converted for special Nginx configuration put in server block aka virtual hosts file. Anyway, Nginx virtual hosts file is not the same as Apache .htaccess file.

Using any caching plugin is aimed to gain more performance in term of page load speed as well as to decrease server load at once. W3 Total Cache (aka W3TC) is world’s most popular caching plugin used by millions blogs globally. It offers complete features which not just about caching but also other necessary features to boost performance of a WordPress-based blog.

So, if you decided to use this caching plugin in your blog that is hosted in your CentOS server running Nginx in it, then you’ll need to read this guide.

Prerequisite

A working server running Nginx web server. In this example I use CentOS. I wrote several articles you can follow to build the same Ubuntu server with LNMP stack. Read:

  1. Basic Setup for CentOS Server
  2. How to Install Nginx and PHP5 with FPM on CentOS
  3. How to Install MySQL server on CentOS
  4. How to Install Postfix mail server on CentOS
  5. How to Add New Site and Setup Nginx Virtual Hosts
  6. How to Install WordPress on CentOS with Nginx

Assumptions: By reading this guide I assume you have enough basic knowledge about WordPress, about how to SSH-ing to your server, and also have a server with Nginx on it.

Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.

switch-root-login

You may also need to type this command to go to the root directory:

cd ~

Step 2 – Setup and configure Nginx virtual hosts file (server blocks) for your website. I’ve posted about how to add new site on Nginx-based CentOS server here. Issue this command syntax to edit it:

nano /etc/nginx/conf.d/virtual.conf

The previous entries for WordPress site in virtual.conf file are below (created following my previous tutorial):

nginx virtual.conf setting for wordpress

Step 3 – Now you can delete all of those entries and replace with following:

server {
    listen  80;
    server_name fikitips.com www.fikitips.com;

    root /var/www/fikitips.com/public_html;
    index index.php;

    set $cache_uri $request_uri;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $cache_uri 'null cache';
    }  
    if ($query_string != "") {
        set $cache_uri 'null cache';
    }  

    # Don't cache uris containing the following segments
    if ($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 commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
    }

    # Use cached or actual file if they exists, otherwise pass request to WordPress
    location / {
        try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args ;
    }   

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }

    location ~ .php$ {
        try_files $uri /index.php;
        include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    # Cache static files for as long as possible
    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
               expires max; log_not_found off; access_log off;
    }
}

Hit Control+O to save then Control+X to exit.

Step 4 – Now test that new configuration and restart Nginx service as well. This command may help you..

nginx -t && service nginx restart

That’s it. Now you can install W3 Total Cache plugin and use following setting:

  • Enable Page Caching : Disk Basic
  • Minify : Disabled
  • Database Cache : Disk
  • Object Cache : Disk
  • Browser Cache : Disabled.

Enjoy.

You can see your blog runs W3TC well by viewing its source code and you should see this:

w3tc enabled

This post Setup Nginx + WordPress + W3 Total Cache In CentOS Server is part of ServerMom.

Source link