banner
moeyy

moeyy

一条有远大理想的咸鱼。
github
mastodon
email

Using Redis with Unix Sockets as an object cache to speed up WordPress sites.

Introduction#

When Redis uses Unix Sockets instead of TCP/IP, it can achieve about a 25% performance improvement.

Installation#

Using Ubuntu 16.04 LTS as an example,

  1. Install Redis
apt-get install redis-server
  1. Check which user Redis is currently running on
root@host:~# sudo ps aux  grep redis
sudo: unable to resolve host www.xx.com
redis    29409  0.0  0.8  37224  8796 ?        Ssl  10:43   0:00 /usr/bin/redis-server 127.0.0.1:6379

It can be seen that Redis is running on redis

  1. Add to the user group where Apache, Nginx, and PHP-FPM are located. Generally, software sources are installed under www-data, and compiled ones are under www, but this is not absolute. In my case, it is under www-data
usermod -g www-data redis
  1. Create the folder where the unix socket is located
mkdir -p /var/run/redis/
  1. Assign permissions
chown -R redis:www-data /var/run/redis
  1. Modify the configuration file, mine is located at /etc/redis/redis.conf
    Remove the # in front of unixsocket and unixsocketperm, and change the value of unixsocketperm from 700 to 777, otherwise the cache cannot be cleared
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /var/run/redis/redis.sock
unixsocketperm 777
  1. Restart
service redis-server restart
  1. If it is running normally, the ls -lh command will return the following result:
root@host:~# ls -lh /var/run/redis
total 4.0K
-rw-r--r-- 1 redis www-data 6 Mar 14 10:56 redis-server.pid
srwx------ 1 redis www-data 0 Mar 14 10:56 redis.sock 

WP Settings#

  1. Install the Redis Object Cache plugin

  2. Add the following after <?php in the wp-config.php file, the order must not be changed:

define('WP_REDIS_PATH', /var/run/redis/redis.sock
define('WP_REDIS_SCHEME', unix);
  1. Then the Redis Object Cache plugin will use Redis with Unix Sockets as the object cache instead of TCP/IP

Original article: Using Redis with Unix Sockets as an object cache in WP

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.