What Cache?
There are may layers of cache one can run on WordPress - ranking from farthest to closest:
- Browser: setup long expiry time to cache as much data as possible on user's machine.
- CDN: For static content like CSS, image, JS and rich media. Works wonder if the whole page can be cached altogether. Jetpack has free CDN for image and CSS without purging possibillity, while Cloudflare can be more flexible but your mileage may vary. Distributed.
- Cache on Load Balancer: Some LBs like LiteSpeed and Nginx can have cache enabled. Normally not distributed.
- "Supercache": Caching the whole page to disk, DB or RAM to turn the dynamic site into static one. Works great for CMS, but won't work that much for sites with tons of interactions, like WooCommerce. Most famous plugins works on this level, like WP Super Cache, or the LiteSpeed plugin with crawler. Can be distributed.
- Object Cache: Cache the raw DB query to ease up DB load, serialize object and save in RAM, like the Redis cache covered in this article. Can be distributed but this article only covers the most basic single instance config.
- OPCode cache: PHP can cache some calculations in RAM to save on CPU. Configured in
php.ini
. Not distributed.
Why go this far?
This task is trivial if you control the whole environment, however there are tons of restrictions on shared hosting. But if you have SSH access there are things you can take advantage of.
Setup
If your machine has gcc
Download latest Redis
SSH into your shared hosting box.
Download and unzip the latest version of Redis 6:
cd ~
wget http://download.redis.io/redis-stable.tar.gz
tar zxvf redis-stable.tar.gz
mv redis-stable redis
rm redis-stable.tar.gz
cd ~/redis
If you want easier setup, download Redis 5 at https://download.redis.io/releases/redis-5.0.14.tar.gz
.
Compile the server
make redis-server
Your redis-server
should be available at ~/redis/src/redis-server
.
If your machine does not have gcc
Find our your OS
Type uname -a. This will give you your kernel version, but might not mention the distribution your running.
To find out what distribution of linux your running (Ex. Ubuntu) try lsb_release -a or cat /etc/release or cat /etc/issue or cat /proc/version.
Compile Redis on that distro
Get a VM/container of that distro, follow similar steps and take redis-server
out.
Try at the target machine and see if everything's working. Redis is pretty easy to build.
Create Redis config file
Create a redis config file somewhere, say ~/redis/rediss.conf
:
You can't use /tmp
as you are on shared hosting. Create a temporary folder - this folder exists on DirectAdmin by default:
mkdir ~/tmp
cd ~/tmp
pwd
and keep the absolute path of your tmp folder.
Content of file for Redis 6:
bind 0
protected-mode yes
port 0
unixsocket {TMP_FOLDER}/redis.sock
unixsocketperm 700
timeout 0
tcp-keepalive 300
daemonize yes
pidfile {TMP_FOLDER}/redis_6379.pid
dir {TMP_FOLDER}/
maxmemory 50M
And for Redis 5:
# create a unix domain socket to listen on
unixsocket {TMP_FOLDER}/redis.sock
# set permissions for the socket
unixsocketperm 775
# No password
# requirepass passwordtouse
# Do not listen on IP
port 0
daemonize yes
stop-writes-on-bgsave-error no
rdbcompression yes
# maximum memory allowed for redis - 50MB for small site, 128MB+ for high traffic
maxmemory 50M
# how redis will evice old objects - least recently used
maxmemory-policy allkeys-lru
Save it.
Start the process
Most distros should come with flock
to ensure that only one instance of process can be run at the same time. If not, refer to https://unix.stackexchange.com/questions/150778/prevent-a-second-instance-of-my-software-from-starting and make your own flock
.
Run
flock -nx ~/tmp/redis.lock -c "~/redis/src/redis-server ~/redis/rediss.conf"
in the shell.
ls ~/tmp
and see whether the lock file and the socket file are generated. If everything's there, you should have a Redis instance running.
Configure WordPress
In wp-config.php
:
/** Redis object cache */
define( 'WP_REDIS_SCHEME', 'unix' );
define( 'WP_REDIS_PATH', '{TMP_FOLDER}/redis.sock' );
// define( 'WP_REDIS_PASSWORD', 'secret' );
define( 'WP_REDIS_TIMEOUT', 1 );
define( 'WP_REDIS_READ_TIMEOUT', 1 );
// change the database for each site to avoid cache collisions
define( 'WP_REDIS_DATABASE', 0 );
define( 'WP_REDIS_MAXTTL', 60 * 60 * 24 * 7 );
// define( 'WP_REDIS_DISABLED', true );
Save it.
Enable Redis Object Storage
Go to Plugins, and install Redis Object Storage
. Enable it. See whether it can talk to your local Redis.
Setup cronjob to ensure Redis is always up
Although we may not care contents inside Redis it's important to make sure it's always up.
In your shared hosting's control panel, add a cronjob for every minute, with content
flock -nx ~/tmp/redis.lock -c "~/redis/src/redis-server ~/redis/rediss.conf" >/dev/null 2>&1
to restart process and don't send Email.
Now you should enjoy the new layer of caching.
I started the cron and got this Message:
Die Aufgabe “flock -nx ~/tmp/redis.lock -c “~/redis/src/redis-server ~/redis/redis.conf” >/dev/null 2>&1″ wurde in 0 Sekunden abgeschlossen, jedoch traten Fehler auf.
What do i need to change?
Run flock -nx ~/tmp/redis.lock -c “~/redis/src/redis-server ~/redis/redis.conf” and see what the new error message is.
$1.5 there are some error in your redis.conf and another $1.5 SELinux/CloudLinux stopped you from doing so(process limit?).