Page 1 of 2

[GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Wed May 24, 2017 9:50 pm
by rhyker2u
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

Code: Select all

sudo apt-get install redis-server php-redis
STEP2: edit redis configuration

Code: Select all

sudo nano /etc/redis/redis.conf
and to the bottom of redis.conf add:

Code: Select all

maxmemory 256mb
maxmemory-policy allkeys-lru
STEP3: then modify the wp-config.php of WP and add these 2 lines:

Code: Select all

define('WP_CACHE_KEY_SALT', 'example.com');
define('WP_CACHE', true);
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:

Code: Select all

redis-cli
monitor
... 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:

Code: Select all

sudo usermod -g www-data redis
sudo chown -R redis:www-data /var/run/redis
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.

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Sat Aug 19, 2017 4:25 am
by rizakyo
Hi,

Uhm... This might sounds a little bit off topic, but I saw that you might help me to encounter the issue that I face.

Currently VestaCP I use on my VPS has Apache+Nginx installed for a few wordpress blogs. I read from many sources that Nginx may significantly improve the speed of websites.

Do you have any idea how I can migrate my wordpress blogs from Apache to Nginx without reinstalling the VestaCP?
As so far, after reading a lot of articles from digitaloceans, etc. I still haven't found a clear articles to do so.

Please let me know.

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Mon Aug 21, 2017 2:25 pm
by rhyker2u
it is off-topic indeed, as I'm not using Apache at all (and using a clean install). Therefore it's better to open a new topic to ask your question. *edit* or just setup a new droplet, and then copy files + DBs. As changing things in Vesta? Has a high probability of screwing everything up and thus might take far more time.

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Wed Aug 23, 2017 3:15 am
by rizakyo
Hi,

Thanks for replying my comment.
I finally ended up rebuilt my server and started with Nginx+PHP fpm.
I am using Linode, actually.

:-)

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Thu Aug 24, 2017 1:02 am
by locus
This would be a good use for running Redis in Docker.
Did you actually benchmark the various options?

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Mon Oct 23, 2017 2:17 pm
by rhyker2u
locus wrote:Did you actually benchmark the various options?
Yap, back in 2016Q4 an 2017Q1 for many many weeks. See pic for summary:

Image

The https://tools.pingdom.com benchmarks were & are even more impressive from a 'waterfall' / parallel loading perspective by using a subdomain on the same server as a generic mirror 'emulating' a CDN. Something that can be done with https://wordpress.org/plugins/wp-original-media-path/ + you can do even more with W3TC cache. It was just as fast as using a 3rd party CDN without the additional cost overhead. And okay I do use CloudFlare (free) + JetPack (free) on the frontdoor which both provide CDN features, but used them all those all others years too with Apache2 +/- nginx as a caching server. What I'm referring to specifically is no need for Amazon S3 nor MaxCDN. Saving more money, but most importantly; a server performance / UX / SEO ranking increase that's simply off the charts.

*edit* Oh ... already wrote that in 1st/opening post too. :-) Anyway, when I was googling for a solution back in 2016Q3 already found plenty of great benchmarks on server load incl. comparisons with HHVM, APC/memcache, etc. I still haven't maxed out my current platform; rock-solid. When I will? Will just setup a HA-IP / LoadBalancer environment.

*edit2* IT can't be all positive, can it now? ;-) Okay there's one big drawback: Nginx does not interpret .htaccess files which can be challenging with both non-wordpress and wordpress environments when using complex rewrites. Nginx rewrite rules are preloaded per site, opposed to 'on webpage access' or a per directory basis. Which is another reason why it's so fast compared to Apache2. There's a great article on Digital Ocean on the Practical Considerations. Thus when anyone's thinking about migrating? Then prepare to spend most time at figuring out nginx.conf files syntax when VestaCP's nginx templates come up short. As most WP plugins cannot write nginx.conf syntax automatically and the ones that do can cause interesting conflicts or strange 404 / 500 / 504 errors. Let's throw in another BONUS TIP (pertaining bettering WP security) + one of the best (online) htaccess to nginx syntax converters -- which doesn't always produce the cleanest nor easiest to understand rewrite rules -- but gives the best results, can be found at http://www.anilcetin.com

And to finish up with BONUS TIP (number 6): how to save a further 10-40% on average on both bandwidth and decrease site loading times by using an image size optimizer like the free https://wordpress.org/plugins/ewww-image-optimizer/ + install these additional Ubuntu packages. The next step would be buying more Bandwidth. Haven't ran the comparison math on that yet. Maybe amazon AWS or any 3rd party CDN / bucket might be cheaper than the 2ct per GB I pay at TransIP / Vultr ? For now ... a long story short: the days of expensive Dedicated servers, slow shared hosting, overpaying for Managed WordPress / SAAS hosting solutions are definitely over as far as I'm concerned. And in large part all thanks to VestaCP for making things lightweight & easy to maintain!

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Tue Oct 24, 2017 11:00 am
by rhyker2u
Pertaining Step 2 in the opening post and WordPress MU / Multi-Site setups. It's useful to have a look at other wp-config.php parameters at https://wordpress.org/plugins/redis-cache/ ... do not forfeit the rest of my guide though! As you will get problems then, and there's no need to re-invent the wheel. ;-) After you've gone through my entire guide? It's worth keeping an eye out the first few days on /var/log/redis/redis-server.log as it occasionally giving away clues of additional configuration tuning. For example: https://stackoverflow.com/questions/368 ... -open-file

Pertaining someone directly linking to this thread, time for an *UPDATE* 2018-03-18 with clarification for step 4 in 1st post, it doesn't matter which WP caching solutions runs. I initially got started with W3TC, but have now switched back to ... any plugin. Comet Cache (free), WP fastest cache (free) and WP Rocket (premium) work just as great with minimal overhead and/or configuration headaches. Strictly speaking you don't even need a WP caching solution after having deployed Redis, but I like (speedier) performance tweaks ;-) Hence, see next post too:

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Sun Mar 18, 2018 11:03 am
by rhyker2u
Another reason my complex WP projects (of 100+ plugins each) have snappy loading and response times ain't just due to Redis (+ all 'bonus' tips), but due to fine-tuning of PHP-FPM's process manager configuration. Preview of a memo I wrote to myself a couple of months ago.

The culprit is in /usr/local/vesta/data/templates/web/php-fpm/default.tpl
More specifically this (in VestaCP prior to the december 2017, aka v0.9.8-17 or earlier):

Code: Select all

pm = dynamic
pm.max_children = 50
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 10
As just 50 children processes equals a ~ 2GB requirement per website. Run more than 2 websites (even with low traffic) on the same $5-10 VPS with those default settings? Then you need to upgrade to a higher VPS. IF ONLY you understood how the PHP-FPM process manager works and how there are 3: dynamic, ondemand and static. I will do an extensive blogpost on the differences and how to benchmark & configure optimal settings. Can't wait and/or like to dig deeper into this? External sources I used and found to be excellent: https://ma.ttias.be/a-better-way-to-run-php-fpm/ and https://haydenjames.io/php-fpm-tuning-u ... rformance/ . So for most of our vestacp sites (or of our clients) you will want to create and utilize a new php-fpm process manager template:

Code: Select all

sudo su
cd /usr/local/vesta/data/templates/web/php-fpm/
cp default.tpl ftl.tpl
nano ftl.tpl
Where you replace those 5 lines of dynamic process manager code by these 4 lines:

Code: Select all

pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 200
That way you can run 10-20 websites on just 1 vcore ’droplet’. And the real 1st bottle neck ain’t about RAM (or swap space) but CPU power. Hence a 2-4 vCore is more than enough in the most badass server setups. The good news is ? That the VestaCP v0.9.8-18 (or higher) comes with a new default (ondemand process manager) config:

Code: Select all

pm = ondemand
pm.max_children = 4
pm.process_idle_timeout = 10s
pm.max_requests = 4000
pm.max_requests defines how many requests (one at a time) a children will process before it gets destroyed. 4000 is on the high side (will eat RAM again like crazy). Setups of 50-500 depending on resources are more typical == ftl.tpl ... and although you can -- theoretically -- counter that by creating a bigger swap file, I read a topic at DO a while ago that swapfiles on a SSD powered VPS is something you like to steer clear from due to hardware degradation.

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Thu Mar 22, 2018 4:25 pm
by Gordon55M
SOLVED -- SEE POST BELOW
rhyker2u wrote:
Wed May 24, 2017 9:50 pm

STEP3: then modify the wp-config.php of WP and add these 2 lines:

Code: Select all

define('WP_CACHE_KEY_SALT', 'example.com’);
Awesome post, glad I came across it and thanks for writing it. Perhaps you can help me out, I'm using the Wordpress2 Template and the whole Redis install went great, however when I enter the line above with my domain, I get the white screen of death. This line enters fine:

Code: Select all

define('WP_CACHE', true);
I just have to comment out the Cache Key Salt for the site to load. When I monitor Redis, it seems to be working, but I wanted to know if you can think of any reason that specific line would throw the white screen, and is that Cache Key Salt needed to run Redis with WordPress? Between this guide and ServerMom's blog, I couldn't piece together the issue. If it helps for additional information, I am running my site from a remote MariaDB.

Re: [GUIDE] WordPress at Ubuntu 16.04 NGINX PHP(7)-FPM + Redis Object caching FIX == FTL

Posted: Thu Mar 22, 2018 5:08 pm
by Gordon55M
I figured it out. I copied and pasted this line from the tutorial into my wp-config.php file. I didn't catch the odd apostrophe after the example.com. When I changed that, it worked great.

Code: Select all

define('WP_CACHE_KEY_SALT', 'example.com’);
should become:

Code: Select all

define('WP_CACHE_KEY_SALT', 'example.com');
Thanks again for the post!