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:
- Basic Setup for CentOS Server
- How to Install Nginx and PHP5 with FPM on CentOS
- How to Install MySQL server on CentOS
- How to Install Postfix mail server on CentOS
- How to Add New Site and Setup Nginx Virtual Hosts
- 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.
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):
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:
any benchmark comparing nginx with no cache + W3 Total Cache vs. nginx with fast cgi cache + no W3 Total Cache?
I don’t have it. But seen it already in few other blogs
Help
It gives me a bad gateway error after the above changes in the file.
Regards
Hi,
You maybe know it already but I just found a much easier way to do it than that. This is all you need do to set up WordPress running on Nginx ~
# install easy-engine
curl -sL rt.cx/ee | sudo bash
# install nginx, php, mysql, postfix
ee system install
# install wordpress on example.com
ee site create example.com –wp
# or if you prefer…
# install wpmu-subdirectory + nginx fastcgi_cache
ee site create example.com –wpsubdir –wpfc
Check it out here ~ https://github.com/rtCamp/easyengine
Terence.
P.S. Oh by the way, it also installs W3 Total Cache for you too!
Thx Terence. I’ll check it out 🙂 sounds cool