[GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL
Posted: Wed May 24, 2017 9:50 pm
Prerequisites
+ Assuming you already installed / have a NGINX PHP-FPM setup: http://vestacp.com/install/
+ have a WP blog setup. When you haven't yet? Install one. Quick tip: Make sure though you've selected "WordPress2" instead of "default" as a template of your VestaCP "added web domain" for permalinks to work properly.
Quick introduction
One of the reasons I love NGINX PHP-FPM with PHP7 so much, is the performance increase + I/O efficiency compared to Apache2 (with or without using NGINX as static cache). And things become even better when installing Redis' Object Cache. Especially within WordPress. Hence this Tutorial plus Best Practices Guide with regular follow up posts too. Let's get started!
STEP1: Install Redis on the server
STEP2: edit redis configuration
and to the bottom of redis.conf add:
STEP3: then modify the wp-config.php of WP and add these 2 lines:
STEP4: enable Redis Cache in WordPress, search and install a plugin called “Redis Object Cache” by Till Kruss: https://wordpress.org/plugins/redis-cache/ and then go to Settings -> Redis and press "Enable Object cache"-button.
Done, or not? According to a diversity of tutorials online that's all there's to it, but .. it is not!!! Although when you execute:
... you can see Redis doing it's magic, but there's something missing!
STEP5: Redis fine tuning (DO NOT SKIP THIS STEP)
Make sure you execute:
that will save you from a ton of vague WP related problems and /var/log/nginx/domains/ logs with critical errors. Which is intel that comes from https://guides.wp-bullet.com/install-re ... ntu-16-04/ ... and I didn't know about till earlier today. And made me want to write this tutorial :)
BONUS TIP1: (saving you again lots of time): you can actually disable any caching plugin, after you've installed Redis. I know how it sounds, but you will be as surprised as I was. However, Wordpress has a nag. Sometimes when a caching plug is disabled, the ('WP_CACHE', true); statement -- usually on top of wp-config.php -- is removed. In that case make sure you add it again as explained in STEP3, or you will run into issues in your wp-backend (where things seem to get saved, but after reloading the page settings saved priorly are not retained ... and you won't find anything about this behaviour in any error log).
BONUS TIP2: Like to gain even more performance? Then replace STEP4 (or simply disable that particular plugin again) in favor of https://wordpress.org/plugins/w3-total-cache/ . To then go "Performance" in sidebar -> "Generic Settings" and go over all the dropdown menu to select "Redis". Additional benefits of W3TC are creating a generic CDN (for parallel loading of content without using a relatively expensive and unnecessary image bucket at Amazon/MaxCDN) and native support of Cloudflare (see: viewtopic.php?f=10&t=14346&p=60358#p60358) for additional layers of security.
BONUS TIP3: prior to learning/applying STEP 5 and TIP1 I used to have https://wordpress.org/plugins/nginx-helper/ which slightly improved the situation in some cases. And keeping it installed, as it doesn't conflict and adds some neat functionality. Infact it's a required plugin if you got a WPMU subfolder environment.
BONUS TIP4: And last but not least, when you use WP plugins like W3TC and/or iThemes Security (https://wordpress.org/plugins/better-wp-security/)? Then tune into this troubleshoot/tutorial I wrote earlier: viewtopic.php?f=11&t=13668 ... otherwise the settings those two plugins generate won't be loaded.
To wrap up; this setup and config has allowed me to go from a 8-core Xeon LAMP VPS with 16GB of RAM to a single core LEMP VPS with 1GB RAM (+ 512MB swap) at Vultr ... and cut page load response times in half. So yeah, must-have / must-do. Maybe VestaCP core team can make Redis part of the installation package/script? While replacing phpmyadmin by adminer? :-p as explained in viewtopic.php?t=14386 ... as IT really gives a 2nd to none experience.
+ Assuming you already installed / have a NGINX PHP-FPM setup: http://vestacp.com/install/
+ have a WP blog setup. When you haven't yet? Install one. Quick tip: Make sure though you've selected "WordPress2" instead of "default" as a template of your VestaCP "added web domain" for permalinks to work properly.
Quick introduction
One of the reasons I love NGINX PHP-FPM with PHP7 so much, is the performance increase + I/O efficiency compared to Apache2 (with or without using NGINX as static cache). And things become even better when installing Redis' Object Cache. Especially within WordPress. Hence this Tutorial plus Best Practices Guide with regular follow up posts too. Let's get started!
STEP1: Install Redis on the server
Code: Select all
sudo apt-get install redis-server php-redis
Code: Select all
sudo nano /etc/redis/redis.conf
Code: Select all
maxmemory 256mb
maxmemory-policy allkeys-lru
Code: Select all
define('WP_CACHE_KEY_SALT', 'example.com');
define('WP_CACHE', true);
Done, or not? According to a diversity of tutorials online that's all there's to it, but .. it is not!!! Although when you execute:
Code: Select all
redis-cli
monitor
STEP5: Redis fine tuning (DO NOT SKIP THIS STEP)
Make sure you execute:
Code: Select all
sudo usermod -g www-data redis
sudo chown -R redis:www-data /var/run/redis
BONUS TIP1: (saving you again lots of time): you can actually disable any caching plugin, after you've installed Redis. I know how it sounds, but you will be as surprised as I was. However, Wordpress has a nag. Sometimes when a caching plug is disabled, the ('WP_CACHE', true); statement -- usually on top of wp-config.php -- is removed. In that case make sure you add it again as explained in STEP3, or you will run into issues in your wp-backend (where things seem to get saved, but after reloading the page settings saved priorly are not retained ... and you won't find anything about this behaviour in any error log).
BONUS TIP2: Like to gain even more performance? Then replace STEP4 (or simply disable that particular plugin again) in favor of https://wordpress.org/plugins/w3-total-cache/ . To then go "Performance" in sidebar -> "Generic Settings" and go over all the dropdown menu to select "Redis". Additional benefits of W3TC are creating a generic CDN (for parallel loading of content without using a relatively expensive and unnecessary image bucket at Amazon/MaxCDN) and native support of Cloudflare (see: viewtopic.php?f=10&t=14346&p=60358#p60358) for additional layers of security.
BONUS TIP3: prior to learning/applying STEP 5 and TIP1 I used to have https://wordpress.org/plugins/nginx-helper/ which slightly improved the situation in some cases. And keeping it installed, as it doesn't conflict and adds some neat functionality. Infact it's a required plugin if you got a WPMU subfolder environment.
BONUS TIP4: And last but not least, when you use WP plugins like W3TC and/or iThemes Security (https://wordpress.org/plugins/better-wp-security/)? Then tune into this troubleshoot/tutorial I wrote earlier: viewtopic.php?f=11&t=13668 ... otherwise the settings those two plugins generate won't be loaded.
To wrap up; this setup and config has allowed me to go from a 8-core Xeon LAMP VPS with 16GB of RAM to a single core LEMP VPS with 1GB RAM (+ 512MB swap) at Vultr ... and cut page load response times in half. So yeah, must-have / must-do. Maybe VestaCP core team can make Redis part of the installation package/script? While replacing phpmyadmin by adminer? :-p as explained in viewtopic.php?t=14386 ... as IT really gives a 2nd to none experience.