Want more timely Centmin Mod News Updates?
Become a Member

Featured Redis How to install Redis server on Centmin Mod LEMP stack

Discussion in 'Centmin Mod Insights' started by eva2000, Sep 24, 2015.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5
    How to guide for installing Redis server for Redis caching usage for various web apps like Xenforo or Wordpress. You will need to consult and lookup how respective web apps addons or plugins utilise Redis caching and configure accordingly.

    This guide is just for installing the Redis server first and is also addressed on the official site's FAQ item 32. Redis has an official mailing list at Google Groups as well.

    Installing Redis Server



    Install Redis server via YUM. Centmin Mod's 3rd party YUM repos will take care of this by using Remi YUM repo for Redis 3.x. After install Redis server configuration file is at /etc/redis.conf

    Code (Text):
    yum -y install redis --enablerepo=remi --disableplugin=priorities
    chkconfig redis on
    service redis start


    Optionally setup Redis password. I usually don't set it to less troublesome operation.
    Code (Text):
    REDISPASS=$(openssl rand 10 -base64)
    echo "requirepass $REDISPASS" >> /etc/redis.conf
    service redis restart


    If on dedicated server or non-OpenVZ VPS like Xen or KVM or VMWare and using CentOS 6 add to /etc/sysctl.conf the setting below
    Code (Text):
    vm.overcommit_memory = 1

    Then type
    Code (Text):
    sysctl -p

    If on CentOS 7 using new Centmin Mod 123.09beta01 and higher/newer, see here. CentOS 7 uses /etc/sysctl.d/101-sysctl.conf in new 123.09beta01 installs and instead of sysctl -p command uses
    Code (Text):
    sysctl --system

    FYI. You can still use /etc/sysctl.conf and sysctl -p too.

    Using Redis as an LRU cache



    According to official site Redis can be used as a LRU cache. Basically. in /etc/redis.conf set a memory limit, eviction policy and LRU algorithm sample size.

    Example with 100MB max memory limit for Redis used as a LRU cache
    Code (Text):
    maxmemory 100mb
    maxmemory-policy allkeys-lru
    maxmemory-samples 10


    also might want to enable AOF by changing - details can be read on my post here and at redis.io/topics/persistence.

    from
    Code (Text):
    appendonly no

    to
    Code (Text):
    appendonly yes


    restart Redis server after making changes to /etc/redis.conf config file
    Code (Text):
    service redis restart


    Redis Server Logs



    The Redis server log file is at /var/log/redis/redis.log

    view the last 50 lines of log
    Code (Text):
    tail -50 /var/log/redis/redis.log 


    Redis Daemonize Mode



    If you need redis to create and write a pid file, you need to run Redis in daemonized mode by setting in /etc/redis.conf
    Code (Text):
    daemonize yes
    

    Then need to edit /etc/redis.conf to change location of pidfile to /var/run/redis/redis.pid
    Code (Text):
    pidfile /var/run/redis/redis.pid
    

    then run these commands in SSH
    Code (Text):
    mkdir /var/run/redis
    chown redis /var/run/redis
    

    and restart redis server

    info from /etc/redis.conf defines the pidfile = /var/run/redis_6379.pid which we change above to /var/run/redis/redis.pid
    Code (Text):
    cat /etc/redis.conf | grep pid
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    # If a pid file is specified, Redis writes it where specified at startup
    # When the server runs non daemonized, no pid file is created if none is
    # specified in the configuration. When the server is daemonized, the pid file
    # is used even if not specified, defaulting to "/var/run/redis.pid".
    # Creating a pid file is best effort: if Redis is not able to create it
    pidfile /var/run/redis_6379.pid

    Code (Text):
    cat /etc/redis.conf | grep daemonize
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    daemonize no
    # When the server runs non daemonized, no pid file is created if none is
    # specified in the configuration. When the server is daemonized, the pid file
    # output for logging but daemonize, logs will be sent to /dev/null
     
    Last edited: Dec 20, 2016
    • Like Like x 2
  2. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Viewing Redis Stats



    I posted a redisinfo.sh script on NodeBB forums ages back that might be useful along with some Redis information. Can be installed via SSH commands
    Code:
    mkdir -p /root/tools
    cd /root/tools/
    wget https://gist.github.com/centminmod/7d3e562fb87fa8ef263a/raw/redisinfo.sh
    chmod 0700 redisinfo.sh
    
    to run
    Code:
    /root/tools/redisinfo.sh
    Sample output
    Code:
    /root/tools/redisinfo.sh
    --------------------------------------------------------------------------
    redis-cli 3.0.4
    --------------------------------------------------------------------------
    redis-cli info
    # Server
    redis_version:3.0.4
    redis_git_sha1:00000000
    redis_git_dirty:0
    redis_build_id:4e722bd58502cba0
    redis_mode:standalone
    os:Linux 2.6.32-042stab093.5 x86_64
    arch_bits:64
    multiplexing_api:epoll
    gcc_version:4.4.7
    process_id:22791
    run_id:909dc403658ef918bba6d9ce1ef5f1afb3dbecac
    tcp_port:6379
    uptime_in_seconds:3
    uptime_in_days:0
    hz:10
    lru_clock:241515
    config_file:/etc/redis.conf
    
    # Clients
    connected_clients:1
    client_longest_output_list:0
    client_biggest_input_buf:0
    blocked_clients:0
    
    # Memory
    used_memory:815984
    used_memory_human:796.86K
    used_memory_rss:1986560
    used_memory_peak:815984
    used_memory_peak_human:796.86K
    used_memory_lua:36864
    mem_fragmentation_ratio:2.43
    mem_allocator:jemalloc-3.6.0
    
    # Persistence
    loading:0
    rdb_changes_since_last_save:0
    rdb_bgsave_in_progress:0
    rdb_last_save_time:1443082088
    rdb_last_bgsave_status:ok
    rdb_last_bgsave_time_sec:-1
    rdb_current_bgsave_time_sec:-1
    aof_enabled:0
    aof_rewrite_in_progress:0
    aof_rewrite_scheduled:0
    aof_last_rewrite_time_sec:-1
    aof_current_rewrite_time_sec:-1
    aof_last_bgrewrite_status:ok
    aof_last_write_status:ok
    
    # Stats
    total_connections_received:2
    total_commands_processed:1
    instantaneous_ops_per_sec:0
    total_net_input_bytes:28
    total_net_output_bytes:1906
    instantaneous_input_kbps:0.00
    instantaneous_output_kbps:0.00
    rejected_connections:0
    sync_full:0
    sync_partial_ok:0
    sync_partial_err:0
    expired_keys:0
    evicted_keys:0
    keyspace_hits:0
    keyspace_misses:0
    pubsub_channels:0
    pubsub_patterns:0
    latest_fork_usec:0
    migrate_cached_sockets:0
    
    # Replication
    role:master
    connected_slaves:0
    master_repl_offset:0
    repl_backlog_active:0
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:0
    repl_backlog_histlen:0
    
    # CPU
    used_cpu_sys:0.00
    used_cpu_user:0.00
    used_cpu_sys_children:0.00
    used_cpu_user_children:0.00
    
    # Cluster
    cluster_enabled:0
    
    # Keyspace
    
    --------------------------------------------------------------------------
    redis-cli --intrinsic-latency 10
    Max latency so far: 3 microseconds.
    Max latency so far: 11 microseconds.
    Max latency so far: 20 microseconds.
    Max latency so far: 28 microseconds.
    Max latency so far: 95 microseconds.
    
    3649898 total runs (avg latency: 2.7398 microseconds / 27398.03 nanoseconds per run).
    Worst run took 35x longer than the average latency.
    
    --------------------------------------------------------------------------
    vmstat 1 10
    procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
    r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
    0  0  65688  24420      0 794336  106  222    27   986    0   30  0  0 100  0  0
    0  0  65688  24436      0 794336    0    0     0     0    0  108  0  0 100  0  0
    0  0  65688  24436      0 794336    0    0     0     0    0  104  0  0 100  0  0
    0  0  65688  24436      0 794336    0    0     0     0    0  104  0  0 100  0  0
    0  0  65424  24172      0 794336  264    0     0     0    0  240  0  0 100  0  0
    0  0  65424  24172      0 794336    0    0     0     0    0  104  0  0 100  0  0
    0  0  65424  24172      0 794336    0    0     0     0    0  108  0  0 100  0  0
    0  0  65424  24172      0 794336    0    0     0     0    0  104  0  0 100  0  0
    0  0  65424  24172      0 794336    0    0     0     0    0  104  0  0 100  0  0
    0  0  65424  24172      0 794336    0    0     0     0    0  104  0  0 100  0  0
    --------------------------------------------------------------------------
    
    smem -kU redis
      PID User     Command                         Swap      USS      PSS      RSS
    22791 redis    /usr/bin/redis-server 127.0        0     1.4M     1.4M     2.0M
    --------------------------------------------------------------------------
    
    --------------------------------------------------------------------------
    smem -kmU redis
    Map                                       PIDs   AVGPSS      PSS
    [vdso]                                       1        0        0
    [vsyscall]                                   1        0        0
    /lib64/libdl-2.12.so                         1     8.0K     8.0K
    /lib64/libm-2.12.so                          1     8.0K     8.0K
    /lib64/ld-2.12.so                            1     9.0K     9.0K
    [stack]                                      1    12.0K    12.0K
    /lib64/libpthread-2.12.so                    1    13.0K    13.0K
    /usr/lib64/libjemalloc.so.1                  1    51.0K    51.0K
    /lib64/libc-2.12.so                          1    54.0K    54.0K
    /usr/bin/redis-server                        1   380.0K   380.0K
    <anonymous>                                  1   924.0K   924.0K
    --------------------------------------------------------------------------
    
    --------------------------------------------------------------------------
    smem -tpk
      PID User     Command                         Swap      USS      PSS      RSS
    32294 nsd      /usr/local/sbin/nsd -c /etc   572.0K        0    19.0K   208.0K
    1780 root     /sbin/udevd -d                256.0K     4.0K    21.0K   324.0K
    32295 nsd      /usr/local/sbin/nsd -c /etc   456.0K    20.0K    41.0K   228.0K
      509 root     /sbin/mingetty console         76.0K     4.0K    49.0K   144.0K
      510 root     /sbin/mingetty tty2            80.0K     4.0K    49.0K   144.0K
    32292 nsd      /usr/local/sbin/nsd -c /etc   340.0K    28.0K    67.0K   528.0K
    1197 root     /usr/sbin/sshd                608.0K    96.0K   147.0K   504.0K
    29074 nobody   /usr/local/bin/memcached -d   504.0K   144.0K   168.0K   588.0K
      369 root     crond                         560.0K   128.0K   169.0K   696.0K
    32321 root     /bin/sh /usr/bin/mysqld_saf   300.0K     8.0K   183.0K     1.1M
    15437 root     -bash                           2.4M     4.0K   286.0K     1.4M
        1 root     ini                           116.0K   284.0K   329.0K   424.0K
    22795 root     /bin/bash /root/tools/redis        0   200.0K   368.0K     1.3M
    11203 root     /usr/libexec/postfix/master   796.0K   148.0K   424.0K     2.2M
    32715 root     /sbin/rsyslogd -i /var/run/   336.0K   444.0K   488.0K     1.0M
    16493 root     pure-ftpd (SERVER)           1004.0K   340.0K   550.0K     1.3M
    11211 postfix  qmgr -l -t fifo -u            784.0K   292.0K   573.0K     2.3M
    22517 postfix  pickup -l -t fifo -u          812.0K   228.0K   577.0K     2.4M
    16616 root     -bash                           1.9M   620.0K   902.0K     2.0M
      735 postfix  tlsmgr -l -t unix -u            1.0M   572.0K   942.0K     2.8M
    22791 redis    /usr/bin/redis-server 127.0        0     1.4M     1.4M     2.0M
    15435 root     sshd: root@pts/0,pts/         844.0K     1.3M     1.7M     3.4M
    13064 root     lfd - sleepin                  17.6M     4.1M     4.2M     4.8M
    22822 root     python /usr/bin/smem -tpk          0     6.1M     6.1M     6.8M
    22698 root     nginx: master process /usr/        0   324.0K     7.3M    21.5M
    22716 root     php-fpm: master process (/u        0     7.7M     7.7M     8.2M
    22700 nginx    nginx: worker process              0     2.4M     9.7M    24.4M
    22699 nginx    nginx: worker process              0     2.7M    10.0M    24.8M
    32645 mysql    /usr/sbin/mysqld --basedir=    37.0M    19.9M    20.1M    21.6M
    -------------------------------------------------------------------------------
       29 7                                       68.2M    49.3M    74.4M   139.1M
    --------------------------------------------------------------------------
    
    --------------------------------------------------------------------------
    free -mlt
                 total       used       free     shared    buffers     cached
    Mem:          1024       1000         23        285          0        775
    Low:          1024       1000         23
    High:            0          0          0
    -/+ buffers/cache:        224        799
    Swap:         1024         63        960
    Total:        2048       1064        983
    --------------------------------------------------------------------------

    Other



    There's also native commands and other resources

    redis-stat



    Then there's redis-stat tool but requires Ruby/Rubygem and CentOS default Ruby version is quite old 1.8.7 while latest is 2.2.3.

    Centmin Mod has an addon at addons/passenger.sh which is for Phusion Passenger Nginx install but as part of the requirements it auto installs Ruby 2.2.x and Node.js as well. So you can run addons/passenger.sh to install Ruby/Rubygems and the output at end of addon install for Nginx Passenger setup instructions are not needed unless you want to enable Phusion Passenger Nginx module support in Nginx. Before running addons/passenger.sh ensure you update Centmin Mod code on your server as outlined at centminmod.com/upgrade.html as addons/passenger.sh was just updated from Ruby 2.2.2 to 2.2.3 and Node.js from 0.12.7 to 4.1.1.
    upload_2015-9-24_18-40-27.png

    In web browser via command
    Code:
    redis-stat --verbose --server=8080 5
    view at your server's main host.domain.com:8080 where host.domain.com is same as server_name listed in /usr/local/nginx/conf/conf.d/virtual.conf and from output of command

    Code:
    hostname
    upload_2015-9-24_18-42-46.png

    phpRedisAdmin



    Another statistics tool is ErikDubbelboer/phpRedisAdmin · GitHub You can install to your main host.domain.com web root at /usr/local/nginx/html. Note: this script can't handle too many keys being loaded by the php script as outlined at after redis-benchmark can not load redis admin page ? · Issue #78 · ErikDubbelboer/phpRedisAdmin · GitHub

    Code:
    cd /usr/local/nginx/html
    git clone https://github.com/ErikDubbelboer/phpRedisAdmin redisadmin
    cd redisadmin
    git clone https://github.com/nrk/predis.git vendor
    chown -R nginx:nginx /usr/local/nginx/html/redisadmin
    
    then setup HTTP password authentication where you set your own USERNAME and PASSWORD

    Code:
    /usr/local/nginx/conf/htpasswd.sh create /usr/local/nginx/conf/htpasswd_redisadmin USERNAME PASSWORD
    in /usr/local/nginx/conf/conf.d/virtual.conf add
    Code:
    location /redisadmin {
         auth_basic "Private";
         auth_basic_user_file /usr/local/nginx/conf/htpasswd_redisadmin;
            include /usr/local/nginx/conf/php.conf;
            #allow 127.0.0.1;
            #allow YOURIPADDRESS;
            #deny all;
    }
    restart nginx server and php-fpm
    Code:
    nprestart
    From github
    So if you set the redis password in /etc/redis.conf need to setup the includes/config.inc.php

    Code:
    cp -a /usr/local/nginx/html/redisadmin/includes/config.sample.inc.php /usr/local/nginx/html/redisadmin/includes/config.inc.php
    so need to edit includes/config.inc.php and set your redis password you set in /etc/redis.conf
    Code:
          // Optional Redis authentication.
          //'auth' => 'redispasswordhere' // Warning: The password is sent in plain-text to the Redis server.
    contents of includes/config.inc.php
    PHP:
    <?php
    //Copy this file to config.inc.php and make changes to that file to customize your configuration.

    $config = array(
      
    'servers' => array(
        array(
          
    'name'   => 'local server'// Optional name.
          
    'host'   => '127.0.0.1',
          
    'port'   => 6379,
          
    'filter' => '*',

          
    // Optional Redis authentication.
          //'auth' => 'redispasswordhere' // Warning: The password is sent in plain-text to the Redis server.
        
    ),

        
    /*array(
          'host' => 'localhost',
          'port' => 6380
        ),*/

        /*array(
          'name'      => 'local db 2',
          'host'      => 'localhost',
          'port'      => 6379,
          'db'        => 1,             // Optional database number, see http://redis.io/commands/select
          'databases' => 1,             // Optional number of databases (prevents use of CONFIG command).
          'filter'    => 'something:*', // Show only parts of database for speed or security reasons.
          'seperator' => '/',           // Use a different seperator on this database (default uses config default).
          'flush'     => false,         // Set to true to enable the flushdb button for this instance.
          'charset'   => 'cp1251',      // Keys and values are stored in redis using this encoding (default utf-8).
          'keys'      => false,         // Use the old KEYS command instead of SCAN to fetch all keys for this server (default uses config default).
          'scansize'  => 1000           // How many entries to fetch using each SCAN command for this server (default uses config default).
        ),*/
      
    ),


      
    'seperator' => ':',


      
    // Uncomment to show less information and make phpRedisAdmin fire less commands to the Redis server. Recommended for a really busy Redis server.
      //'faster' => true,


      // Uncomment to enable HTTP authentication
      /*'login' => array(
        // Username => Password
        // Multiple combinations can be used
        'admin' => array(
          'password' => 'adminpassword',
        ),
        'guest' => array(
          'password' => '',
          'servers'  => array(1) // Optional list of servers this user can access.
        )
      ),*/


      /*'serialization' => array(
        'foo*' => array( // Match like KEYS
          // Function called when saving to redis.
          'save' => function($data) { return json_encode(json_decode($data)); },
          // Function called when loading from redis.
          'load' => function($data) { return json_encode(json_decode($data), JSON_PRETTY_PRINT); },
        ),
      ),*/


      // You can ignore settings below this point.

      
    'maxkeylen'           => 100,
      
    'count_elements_page' => 100,

      
    // Use the old KEYS command instead of SCAN to fetch all keys.
      
    'keys' => false,

      
    // How many entries to fetch using each SCAN command.
      
    'scansize' => 1000
    );

    ?>
    and then go to your host.domain.com/redisadmin main domain where host.domain.com is same as server_name listed in /usr/local/nginx/conf/conf.d/virtual.conf and from output of command

    Code:
    hostname
    and log in with your set USERNAME and PASSWORD

    upload_2015-9-24_22-9-52.png

    Redmon



    If you installed addons/passenger.sh to get Ruby and Rubygem support, then you would be able to install Redmon

    Code:
    gem install redmon
    Code:
    redmon -h
    Usage: /usr/local/rvm/gems/ruby-2.2.3/bin/redmon (options)
        -a, --address ADDRESS            The thin bind address for the app (default: 0.0.0.0)
        -l, --lifespan MINUTES           Lifespan(in minutes) for polled data (default: 30)
        -n, --namespace NAMESPACE        The root Redis namespace (default: redmon)
        -i, --interval SECS              Poll interval in secs for the worker (default: 10)
        -p, --port PORT                  The thin bind port for the app (default: 4567)
        -r, --redis URL                  The Redis url for monitor (default: redis://127.0.0.1:6379, note: password is support, ie redis://:password@127.0.0.1:6379)
        -s, --secure CREDENTIALS         Use basic auth. Colon separated credentials, eg admin:admin.
            --no-app                     Do not run the web app to present stats (default: true)
            --no-worker                  Do not run a worker to collect the stats (default: true)
    running just command below will allow you to got to
    host.domain.com:4567 - where host.domain.com is same as server_name listed in /usr/local/nginx/conf/conf.d/virtual.conf

    Code:
    redmon
    You will need to also open up and whitelist in CSF Firewall port 4567 for TCP_IN and TCP6_IN - add to their comma separated list of ports and restart CSF Firewall

    Code:
    sed -i "s/TCP_IN = \"/TCP_IN = \"4567,/g" /etc/csf/csf.conf
    sed -i "s/TCP6_IN = \"/TCP6_IN = \"4567,/g" /etc/csf/csf.conf
    csf -r
    In web browser at host.domain.com:4567

    upload_2015-9-27_4-26-43.png

    can even run redis command line from the web browser

    upload_2015-9-27_4-27-26.png

    Connecting to a Redis server or Redis cluster other than default 127.0.0.1:6379 i.e. Redis Cluster's master at 127.0.0.1:23001

    Code:
    redmon -r redis://127.0.0.1:23001
    upload_2015-9-27_4-31-50.png
     
    Last edited: Sep 29, 2015
    • Useful Useful x 1
  3. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Using Redis Caching with Wordpress



    One common use for Redis caching is with Wordpress object caching. You can use Redis Object Cache Wordpress Plugin. If you installed Wordpress + WP Super Cache via centmin.sh menu option 22, you can combine it with Redis Object Cache Wordpress Plugin (an alternative to if you want to use Memcached server cache for Wordpress object caching via WordPress › MemcacheD Is Your Friend « WordPress Plugins.

    Note using Redis Object Cache you may need to flush cache when making caches to backend WP admin settings WordPress › Support » Flush cache is required whenever changing anything in the backend.

    When you use centmin.sh menu option 22 to create new Nginx vhost domain with Wordpress + WP Super Cache auto installed, part of output will have information on your Nginx vhost paths and where public web root is and where Wordpress was installed
    Code (Text):
    vhost for newdomain.com created successfully
    
    domain: http://newdomain.com
    vhost conf file for newdomain.com created: /usr/local/nginx/conf/conf.d/newdomain.com.conf
    
    vhost ssl for newdomain.com created successfully
    
    domain: https://newdomain.com
    vhost ssl conf file for newdomain.com created: /usr/local/nginx/conf/conf.d/newdomain.com.ssl.conf
    /usr/local/nginx/conf/ssl_include.conf created
    Self-signed SSL Certificate: /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.crt
    SSL Private Key: /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.key
    SSL CSR File: /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com.csr
    Backup SSL Private Key: /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com-backup.key
    Backup SSL CSR File: /usr/local/nginx/conf/ssl/newdomain.com/newdomain.com-backup.csr
    
    upload files to /home/nginx/domains/newdomain.com/public
    vhost log files directory is /home/nginx/domains/newdomain.com/log
    


    Public web root where you upload files is at /home/nginx/domains/newdomain.com/public and is where Wordpress is installed.

    To install Redis Object Cache WP Plugin, you can do it via Wordpress Add New Plugin admin interface search function and type Redis Object Cache

    upload_2015-9-24_18-18-22.png

    Or can install Redis Object Cache Wordpress Plugin via SSH command line using WP-CLI tool that is installed as part of the centmin.sh menu option 22 auto Wordpress installation routine.

    Change into Wordpress Nginx vhost domain web root at /home/nginx/domains/newdomain.com/public and run the command to install Redis Object Cache via WP-CLI tool. The actual plugin name is same as name listed in url link for the WP plugin in the WP directory at wordpress.org/plugins/redis-cache/ = redis-cache

    These commands auto installs Redis Object Cache WP plugin and activates it as well
    Code (Text):
    cd /home/nginx/domains/newdomain.com/public
    wp plugin install redis-cache --activate --allow-root


    output of
    Code (Text):
    wp plugin install redis-cache --activate --allow-root


    Code (Text):
    wp plugin install redis-cache --activate --allow-root
    Installing Redis Object Cache (1.2.1)
    Downloading install package from https://downloads.wordpress.org/plugin/redis-cache.1.2.1.zip...
    Unpacking the package...
    Installing the plugin...
    Plugin installed successfully.
    Success: Translations updates are not needed for the 'English (US)' locale.
    Activating 'redis-cache'...
    Success: Plugin 'redis-cache' activated.


    Installed and activated

    upload_2015-9-24_18-22-47.png

    Settings -> Redis and check Enable Object Cache

    upload_2015-9-24_18-24-17.png

    upload_2015-9-24_18-24-46.png

    I would also add to wp-config.php following Redis Object Cache configuration settings explained further below:
    Code (Text):
    define('WP_CACHE_KEY_SALT', md5( DB_NAME . $table_prefix . __FILE__ ) );
    define('WP_REDIS_MAXTTL', 28800);
    define('WP_REDIS_DATABASE', 7);


    Redis Object Cache Additional Notes



    Additional notes if you have multiple Wordpress installs and what you need to do at WordPress › Redis Object Cache « WordPress Plugins

    So in your wp-config.php i.e. at add this WP_CACHE_KEY_SALT line above or below the existing $table_prefix setting

    Code (Text):
    define('WP_CACHE_KEY_SALT', md5( DB_NAME . $table_prefix . __FILE__ ) );


    You may also want to set a max TTL setting via WP_REDIS_MAXTTL option for cached objects as Redis Object Cache needs flushing whenever you make backend WP Admin settings caches, so if you forget to flush, you may want to auto expire cached Redis backed objects automatically after XXX seconds. For example, after 900 seconds or 15 mins. Set in wp-config.php

    Code (Text):
    define('WP_REDIS_MAXTTL', 900);


    Restart Nginx and PHP-FPM and then check Settings > Redis you will see a Key Prefix setting set
    Code (Text):
    nprestart


    upload_2015-9-24_18-34-7.png

    or with WP_REDIS_MAXTTL set

    upload_2015-9-24_22-59-38.png

    Changing default Redis database from database 0 to database 7 or a number between 0 to 15 as Redis server defaults to preset creation of 16 databases numbered 0 to 15.

    Code (Text):
    define('WP_REDIS_DATABASE', 7);


    Then in future, instead of flushing all redis databases, you can specifically just flush database 7
    Code (Text):
    redis-cli -n 7 flushdb
    OK
    


    Other wp-config.php options for Redis Object Cache plugin

    Quick Benchmarks



    Using installed Siege benchmark tool installed by default in Centmin Mod LEMP stack

    Siege benchmark mode 50 concurrent users for 1000 reps
    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    Code:
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  10.06 secs
    Data transferred:             184.01 MB
    Response time:                  0.01 secs
    Transaction rate:            4970.18 trans/sec
    Throughput:                    18.29 MB/sec
    Concurrency:                   45.34
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.03
    Shortest transaction:           0.00
    FILE: /usr/local/var/siege.log
    You can disable this annoying message by editing
    the .siegerc file in your home directory; change
    the directive 'show-logfile' to false.
    Wasn't able to really stress Redis server cache as Wordpress has WP Super Cache plugin installed so such a stress test would of hit WP Super Cache's generated static file cache thus bypassing PHP-FPM and MariaDB MySQL usage.

    upload_2015-9-24_18-57-17.png

    If you disable WP Super Cache clear cache entries and restart nginx, php-fpm and redis server and retest you will see Siege bench loads hitting Redis cache server but at a much slower rate than WP Super Cache performance.

    Code:
    nprestart
    service redis restart
    I aborted the Siege benchmark test at ~360 second mark as it would of taken quite a long time to complete 50x 1000 = 50,000 requests at ~74.59 trans/sec much slower than WP Super Cache at 4,970 requests/s but Redis Object Cache can complement WP Super Cache.

    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    
    Transactions:                  26889 hits
    Availability:                 100.00 %
    Elapsed time:                 360.50 secs
    Data transferred:              97.45 MB
    Response time:                  0.67 secs
    Transaction rate:              74.59 trans/sec
    Throughput:                     0.27 MB/sec
    Concurrency:                   49.94
    Successful transactions:       26889
    Failed transactions:               0
    Longest transaction:            1.41
    Shortest transaction:           0.00
    started redis-stat with 1 second interval during Siege benchmark

    Code:
    redis-stat --verbose --server=8080 1
    shows up to 1.4K connections/sec to Redis server with ~473K total cmds with a 95.6% cache hit rate/s or around 1.26K cached connection hits/s and around 1.66MB memory usage

    upload_2015-9-24_19-8-42.png

    Compared to Memcached Wordpress Object Caching



    Let's compare to Memcached Wordpress Object caching only with WP Super Cache still disabled and Redis Object Cache disabled

    Install
    Code:
    cd /home/nginx/domains/newdomain.com/public
    wp plugin install memcached-is-your-friend --activate --allow-root
    Code:
    wp plugin install memcached-is-your-friend --activate --allow-root
    Installing MemcacheD Is Your Friend (2.0.0)
    Downloading install package from https://downloads.wordpress.org/plugin/memcached-is-your-friend.zip...
    Unpacking the package...
    Installing the plugin...
    Plugin installed successfully.
    Success: Translations updates are not needed for the 'English (US)' locale.
    Activating 'memcached-is-your-friend'...
    /home/nginx/domains/newdomain.com/public/wp-content/plugins/memcached-is-your-friend/memcached-is-your-friend.php: 1
    Success: Plugin 'memcached-is-your-friend' activated.
    upload_2015-9-24_19-21-12.png

    Memcached server stats page is installed in your main host.domain.com nginx vhost web root at /usr/local/nginx/html with random url suffix added - see details at PHP Opcode and Memcached statistics pages | Centmin Mod Community

    so at /usr/local/nginx/html/memcache_ac2ae76a.php which would be accessed at host.domain.com/memcache_ac2ae76a.php and user/pass is auto generated and unique to each install and is located in file itself
    Code:
    ls -lah /usr/local/nginx/html | grep memcache
    -rw-r--r-- 1 nginx nginx  29K Sep 22 17:18 memcache_ac2ae76a.php
    Code:
    grep "'ADMIN_" /usr/local/nginx/html/memcache_ac2ae76a.php
    define('ADMIN_USERNAME','memadminbcbfd5454c');  // Admin Username
    define('ADMIN_PASSWORD','9d8bf4b751540f84ec0a1e4a');    // Admin Password
    Restart Nginx, PHP-FPM, Memcached servers for fresh Siege benchmark

    Code:
    nprestart
    memcachedrestart
    I aborted at ~360 second mark again as like Redis cache it's slower than WP Super Cache static file caching and end result is around 58.97 requests/s so slower than Redis Object Cache at 74.59 requests/s (Redis is ~26% faster)

    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    
    Transactions:                  21058 hits
    Availability:                 100.00 %
    Elapsed time:                 357.12 secs
    Data transferred:              76.04 MB
    Response time:                  0.85 secs
    Transaction rate:              58.97 trans/sec
    Throughput:                     0.21 MB/sec
    Concurrency:                   49.93
    Successful transactions:       21058
    Failed transactions:               0
    Longest transaction:            1.47
    Shortest transaction:           0.39
    
    host.domain.com/memcache_ac2ae76a.php stats

    1,253 memcached requests/s with 95.4% memcached hit rate at 1,195 memcached hits/sec.

    upload_2015-9-24_19-35-21.png
     
    Last edited: Mar 17, 2016
  4. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Wordpress Redis Cache at Nginx Level



    Another way of using Redis server caching is to cache Wordpress at Nginx level. Meaning Nginx web server is the one that interfaces with Redis server via a few Nginx modules which Centmin Mod LEMP stack has installed by default or optionally need enabling as well.
    • --add-module=../ngx_http_redis-0.3.7
    • --add-module=../redis2-nginx-module-0.13
    • --add-module=../srcache-nginx-module-0.31
    • --add-module=../headers-more-nginx-module-0.31
    • --add-module=../set-misc-nginx-module-0.31
    And Centmin Mod PHP-FPM out of the box has Redis PHP extension loaded too

    Code:
    php --ri redis
    
    redis
    
    Redis Support => enabled
    Redis Version => 2.2.7
    This guide and setup is courtesy of RTCamp folks guide adapted for Centmin Mod Nginx.

    Centmin Mod LEMP stack default Nginx installed modules on 123.09beta01 branch

    Switch from WP Super Cache to Nginx Level Redis Cache



    To switch from Wordpress Super Cache setup via centmin.sh menu option 22 auto Wordpress autoinstaller to using Wordpress Redis cache at the Nginx level, you first need to install the nginx-helper Wordpress plugin. I am using nginx vhost domain = newdomain.com as an example.

    Can be installed via normal WP Admin Add new plugins gui or via WP-CLI command tool in SSH.

    Change to Wordpress install directory i.e. /home/nginx/domains/newdomain.com/public and install + activate nginx-helper wordpress plugin

    Code (Text):
    cd /home/nginx/domains/newdomain.com/public
    wp plugin install nginx-helper --activate --allow-root
    


    sample output

    Code (Text):
    wp plugin install nginx-helper --activate --allow-root
    Installing Nginx Helper (1.9.6)
    Downloading install package from https://downloads.wordpress.org/plugin/nginx-helper.1.9.6.zip...
    Unpacking the package...
    Installing the plugin...
    Plugin installed successfully.
    Success: Translations updates are not needed for the 'English (US)' locale.
    Activating 'nginx-helper'...
    Success: Plugin 'nginx-helper' activated.
    


    Then go into WP Admin Settings -> Nginx Helper options and enable Purging options, select Redis cache as Caching Method and check the relevant boxes and hit save.

    nginx-helper-rediscache-01.png

    Configuring Nginx Vhost for Nginx Level Redis Caching



    Revised August 24th, 2016 to match closer to Centmin Mod 123.09beta01 centmin.sh menu option 22 auto created Redis include files.

    For Centmin Mod 123.08stable users only, create include file named /usr/local/nginx/conf/redisupstream.conf in /usr/local/nginx/conf/nginx.conf after the /usr/local/nginx/conf/fastcgi_param_https_map.conf include file. For Centmin Mod 123.09beta01 users, /usr/local/nginx/conf/redisupstream.conf include file is auto created when you run centmin.sh menu option 22 to auto install Wordpress.
    Code (Text):
    include /usr/local/nginx/conf/fastcgi_param_https_map.conf;
    include /usr/local/nginx/conf/redisupstream.conf;
    

    add to /usr/local/nginx/conf/redisupstream.conf the following code
    Code (Text):
    map $http_user_agent $redis_device {
        default                                     'desktop';
        ~*(iPad|iPhone|Android|IEMobile|Blackberry) 'mobile';
        "~*Firefox.*Mobile"                         'mobile';
        "~*ipod.*mobile"                            'mobile';
        "~*Opera\ Mini"                             'mobile';
        "~*Opera\ Mobile"                           'mobile';
        "~*Mobile"                                  'mobile';
        "~*Tablet"                                  'mobile';
        "~*Kindle"                                  'mobile';
        "~*Windows\ Phone"                          'mobile';
    }
    
    upstream redisbackend {
      zone upstream_redis 64k;
      server 127.0.0.1:6379 weight=1 max_fails=3 fail_timeout=30s;
      keepalive 4096;
    }
    


    For Centmin Mod 123.08stable users only, create the include file /usr/local/nginx/conf/rediscache_newdomain.com.conf and add to it the following code. For Centmin Mod 123.09beta01 users, /usr/local/nginx/conf/rediscache_newdomain.com.conf include file is auto created when you run centmin.sh menu option 22 to auto install Wordpress.
    Code (Text):
       # Block nginx-help log from public viewing
       location ~* /wp-content/uploads/nginx-helper/ { deny all; }
    
        set $skip_cache 0;
    
       # exclude mobile devices from redis caching
       if ($redis_device = mobile) {
         set $skip_cache 1;
       }
    
        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
            set $skip_cache 1;
        }
        if ($query_string != "") {
            set $skip_cache 1;
        }
    
    # Don't cache uris containing the following segments
    if ($request_uri ~* "\?add-to-cart=|/cart/|/my-account/|/checkout/|/shop/checkout/|/store/checkout/|/customer-dashboard/|/addons/|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
      set $skip_cache 1;
    }
    
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|edd_items_in_cart|woocommerce_items_in_cart") {
      set $skip_cache 1;
    }
    
        location /redis-fetch {
            internal  ;
            set  $redis_key $args;
           redis_pass  redisbackend;
           redis_connect_timeout 60000;
           redis_read_timeout 60000;
           redis_send_timeout 60000;
        }
    
        location /redis-store {
            internal  ;
            set_unescape_uri $key $arg_key ;
            redis2_query set $key $echo_request_body;
            redis2_query expire $key 6h;
           redis2_pass  redisbackend;
           redis2_connect_timeout 60s;
           redis2_read_timeout 60s;
           redis2_send_timeout 60s;
        }
    


    In your wordpress nginx vhost files for yourdomain.com and yourdomain.ssl.conf (if you setup self-signed ssl at vhost creation time):
    • For Centmin Mod 123.08stable and Centmin Mod 123.09beta01 users, find
    Code (Text):
    include /usr/local/nginx/conf/wpsupercache_newdomain.com.conf;
    


    and comment it out and use the new include file /usr/local/nginx/conf/rediscache_newdomain.com.conf instead.

    Centmin Mod 123.09beta01 users, the include line is already added but commented out, so need to uncomment /usr/local/nginx/conf/rediscache_newdomain.com.conf and comment out /usr/local/nginx/conf/wpsupercache_newdomain.com.conf. For Centmin Mod 123.08stable users, you need to add the include line /usr/local/nginx/conf/rediscache_newdomain.com.conf and comment out /usr/local/nginx/conf/wpsupercache_newdomain.com.conf

    Code (Text):
    #include /usr/local/nginx/conf/wpsupercache_newdomain.com.conf;
    include /usr/local/nginx/conf/rediscache_newdomain.com.conf;
    


    For Centmin Mod 123.08stable, then create the php-rediscache.conf include file which will replace WP Super Cache's PHP include file at php-wpsc.conf. Centmin Mod 123.09beta01 users, /usr/local/nginx/conf/php-rediscache.conf include file is already created.

    For Centmin Mod 123.08stable only, create php-rediscache.conf by copying it from /usr/local/nginx/conf/php.conf
    Code (Text):
    \cp -f /usr/local/nginx/conf/php.conf /usr/local/nginx/conf/php-rediscache.conf
    


    For Centmin Mod 123.08stable only, add to /usr/local/nginx/conf/php-rediscache.conf from line 2 onwards the following.

    Code (Text):
        set $key "nginx-cache:$scheme$request_method$host$request_uri";
        srcache_fetch_skip $skip_cache;
        srcache_store_skip $skip_cache;
        srcache_response_cache_control off;
        set_escape_uri $escaped_key $key;
        srcache_fetch GET /redis-fetch $key;
        srcache_store PUT /redis-store key=$escaped_key;
        more_set_headers 'X-Cache $srcache_fetch_status';
        more_set_headers 'X-Cache-2 $srcache_store_status';
    


    so in after this line

    Code (Text):
    location ~ \.php$ {
    


    add the settings so it looks like this at top of file

    Code (Text):
    location ~ \.php$ {
        set $key "nginx-cache:$scheme$request_method$host$request_uri";
        srcache_fetch_skip $skip_cache;
        srcache_store_skip $skip_cache;
        srcache_response_cache_control off;
        set_escape_uri $escaped_key $key;
        srcache_fetch GET /redis-fetch $key;
        srcache_store PUT /redis-store key=$escaped_key;
        more_set_headers 'X-Cache $srcache_fetch_status';
        more_set_headers 'X-Cache-2 $srcache_store_status';
    


    For Centmin Mod 123.08stable and 123.09beta01, edit yourdomain.com and yourdomain.ssl.conf and comment out this try_files line.

    from

    Code (Text):
      # for wordpress super cache plugin
      try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?q=$uri&$args;
    
      # Wordpress Permalinks
      #try_files $uri $uri/ /index.php?q=$uri&$args;
    


    to

    Code (Text):
      # for wordpress super cache plugin
      #try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?q=$uri&$args;
    
      # Wordpress Permalinks
      try_files $uri $uri/ /index.php?$args;
    


    and find all instances - there will be more than one of the php-wpsc.conf include line and change

    from

    Code (Text):
      include /usr/local/nginx/conf/php-wpsc.conf;
    


    to

    Code (Text):
      #include /usr/local/nginx/conf/php-wpsc.conf;
      include /usr/local/nginx/conf/php-rediscache.conf;
    


    So full nginx vhost changes

    from

    Code (Text):
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name newdomain.com;
    #            return 301 $scheme://www.newdomain.com$request_uri;
    #       }
    
    server {
      server_name newdomain.com www.newdomain.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/newdomain.com/log/access.log combined buffer=256k flush=60m;
      error_log /home/nginx/domains/newdomain.com/log/error.log;
    
      root /home/nginx/domains/newdomain.com/public;
    
      # prevent access to ./directories and files
      #location ~ (?:^|/)\. {
      #deny all;
      #}
    
    include /usr/local/nginx/conf/wpsupercache_newdomain.com.conf;
    
      location / {
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # for wordpress super cache plugin
      try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?q=$uri&$args;
    
      # Wordpress Permalinks
      #try_files $uri $uri/ /index.php?q=$uri&$args;
    
      }
    
    location ~* /(wp-login\.php) {
        limit_req zone=xwplogin burst=1 nodelay;
        #limit_conn xwpconlimit 30;
        auth_basic "Private";
        auth_basic_user_file /home/nginx/domains/newdomain.com/htpasswd_wplogin;
        include /usr/local/nginx/conf/php-wpsc.conf;
    }
    
    location ~* /(xmlrpc\.php) {
        limit_req zone=xwplogin burst=2 nodelay;
        #limit_conn xwpconlimit 30;
        include /usr/local/nginx/conf/php-wpsc.conf;
    }
    
      include /usr/local/nginx/conf/wpsecure_newdomain.com.conf;
      include /usr/local/nginx/conf/php-wpsc.conf;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    


    to

    Code (Text):
    # Centmin Mod Getting Started Guide
    # must read http://centminmod.com/getstarted.html
    
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    # if unsure use return 302 before using return 301
    #server {
    #            listen   80;
    #            server_name newdomain.com;
    #            return 301 $scheme://www.newdomain.com$request_uri;
    #       }
    
    server {
      server_name newdomain.com www.newdomain.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    #include /usr/local/nginx/conf/pagespeed.conf;
    #include /usr/local/nginx/conf/pagespeedhandler.conf;
    #include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/newdomain.com/log/access.log combined buffer=256k flush=60m;
      error_log /home/nginx/domains/newdomain.com/log/error.log;
    
      root /home/nginx/domains/newdomain.com/public;
    
      # prevent access to ./directories and files
      location ~ (?:^|/)\. {
       deny all;
      }
    
    #include /usr/local/nginx/conf/wpsupercache_newdomain.com.conf;
    include /usr/local/nginx/conf/rediscache_newdomain.com.conf;
    
      location / {
    
      # Enables directory listings when index file not found
      #autoindex  on;
    
      # for wordpress super cache plugin
      #try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?q=$uri&$args;
    
      # Wordpress Permalinks
      try_files $uri $uri/ /index.php?$args;
    
      }
    
    location ~* /(wp-login\.php) {
        limit_req zone=xwplogin burst=1 nodelay;
        #limit_conn xwpconlimit 30;
        auth_basic "Private";
        auth_basic_user_file /home/nginx/domains/newdomain.com/htpasswd_wplogin;
    
       #include /usr/local/nginx/conf/php-wpsc.conf;
       include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
    location ~* /(xmlrpc\.php) {
        limit_req zone=xwplogin burst=2 nodelay;
        #limit_conn xwpconlimit 30;
        #include /usr/local/nginx/conf/php-wpsc.conf;
        include /usr/local/nginx/conf/php-rediscache.conf;
    }
    
      include /usr/local/nginx/conf/wpsecure_newdomain.com.conf;
      #include /usr/local/nginx/conf/php-wpsc.conf;
      include /usr/local/nginx/conf/php-rediscache.conf;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      include /usr/local/nginx/conf/vts_server.conf;
    }
    


    Then restart nginx server and php-fpm

    Code (Text):
    nprestart
    


    Then ensure WP Super Cache is disabled and cache cleared and load up your Wordpress site a few times to populate the cache and check it out. You can see it in Redis Admin stats if you installed it from above post outline.

    Notice the cache key named nginx-cache

    redisadmin-rediscache-nginx-helper-00.png

    Troubleshooting



    You can enable nginx-helper plugin's logging too. Setting up the log file in SSH commands

    Code:
    touch /home/nginx/domains/newdomain.com/public/wp-content/uploads/nginx-helper/nginx.log
    chmod 0660 /home/nginx/domains/newdomain.com/public/wp-content/uploads/nginx-helper/nginx.log
    chown nginx:nginx /home/nginx/domains/newdomain.com/public/wp-content/uploads/nginx-helper/nginx.log
    nginx-helper-rediscache-02.png

    Verify if it's working with nginx-helper source code embedded timestamp

    Code:
    curl -s http://newdomain.com | tail -3
    </html>
    <!--Cached using Nginx-Helper on 2015-09-24 16:02:50. It took 207 queries executed in 0.128 seconds.-->
    <!--Visit http://wordpress.org/extend/plugins/nginx-helper/faq/ for more details-->
    multiple curl runs to check timestamp in html source code shows home page index is cached as timestamp doesn't change

    Code:
    curl -s http://newdomain.com | tail -3
    </html>
    <!--Cached using Nginx-Helper on 2015-09-24 16:02:50. It took 207 queries executed in 0.128 seconds.-->
    check another post url

    Code:
     curl -s http://newdomain.com/160/promo-art-for-star-wars-ep-7-the-force-awakens/ | tail -3
    </html>
    <!--Cached using Nginx-Helper on 2015-09-24 16:17:48. It took 505 queries executed in 0.257 seconds.-->
    <!--Visit http://wordpress.org/extend/plugins/nginx-helper/faq/ for more details-->
    cached

    redisadmin-rediscache-nginx-helper-01.png

    Siege Benchmark



    Again Siege Benchmark run and this is comparable to WP Super Cache as it's full page caching unlike Redis and Memcached object only caching.

    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    Nginx level Redis Cache 2,939.45 trans/s versus WP Super Cache's 4,970 trans/s.
    Code:
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  17.01 secs
    Data transferred:             350.38 MB
    Response time:                  0.02 secs
    Transaction rate:            2939.45 trans/sec
    Throughput:                    20.60 MB/sec
    Concurrency:                   48.11
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.06
    Shortest transaction:           0.00
    Doing a second run pretty much the same

    Code:
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  17.10 secs
    Data transferred:             348.04 MB
    Response time:                  0.02 secs
    Transaction rate:            2923.98 trans/sec
    Throughput:                    20.35 MB/sec
    Concurrency:                   48.96
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.07
    Shortest transaction:           0.00
    compared to WP Super Cache benchmark run above notice data transferred amount, almost half so must be the added posts since that require re-benchmark for WP Super Cache

    WP Super Cache done earlier
    Code:
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  10.06 secs
    Data transferred:             184.01 MB
    Response time:                  0.01 secs
    Transaction rate:            4970.18 trans/sec
    Throughput:                    18.29 MB/sec
    Concurrency:                   45.34
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.03
    Shortest transaction:           0.00
    FILE: /usr/local/var/siege.log
    You can disable this annoying message by editing
    the .siegerc file in your home directory; change
    the directive 'show-logfile' to false.
    Whoops siege benchmark runs got my VPS server suspended :LOL: :oops: Thanks @Steve Tozer for fixing that.

    Wordpress Benchmark Nginx Level Redis Cache vs WP Super Cache



    So redoing tests with current wordpress added dummy posts. Seems Nginx level Redis Cache is ~45% faster than WP Super Cache with apples to apples comparison of same amount of posts on WP Index page.
    • WP Super Cache + Redis Object Caching 1,610.82 trans/sec & longest trans time = 0.16s
    • Nginx level Redis Cache + Redis Object Caching 2,337.54 trans/s & longest trans time = 0.09s
    WP Super Cache + Redis Object Caching
    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  31.04 secs
    Data transferred:             346.57 MB
    Response time:                  0.03 secs
    Transaction rate:            1610.82 trans/sec
    Throughput:                    11.17 MB/sec
    Concurrency:                   49.42
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.16
    Shortest transaction:           0.00
    Nginx level Redis Cache + Redis Object Caching
    Code:
    siege -b -c50 -r1000 http://newdomain.com/
    
    Transactions:                  50000 hits
    Availability:                 100.00 %
    Elapsed time:                  21.39 secs
    Data transferred:             346.57 MB
    Response time:                  0.02 secs
    Transaction rate:            2337.54 trans/sec
    Throughput:                    16.20 MB/sec
    Concurrency:                   49.44
    Successful transactions:       50000
    Failed transactions:               0
    Longest transaction:            0.09
    Shortest transaction:           0.00

    Further Performance Tweaks



    You can also remove the Redis TCP overhead in redis.conf by also enabling unix socket mode while keep TCP ports for Redis enabled too.

    Code (Text):
    # 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 /tmp/redis.sock
    unixsocketperm 755

    Then restart Redis server
    Code (Text):
    service redis restart

    Then in /usr/local/nginx/conf/redisupstream.conf defined upstream change Redis backend from TCP port 6379 to a Unix socket
    Code (Text):
    upstream redisbackend {
      server unix:/tmp/redis.sock;
      #server 127.0.0.1:6379;
      keepalive 4096;
    }
     
    Last edited: Nov 9, 2016
    • Informative Informative x 3
  5. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Redis Cache for PHP Sessions



    Just like Memcache/Memcached which can be used to cache and store PHP sessions, you can do the same with Redis for PHP sessions. Centmin Mod LEMP stack's PHP-FPM by default installs phpredis PHP extension.

    Code:
    php --ri redis
    
    redis
    
    Redis Support => enabled
    Redis Version => 2.2.7
    You can edit a few php settings to offload PHP sessions to Redis server. However, don't edit /usr/local/lib/php.ini settings directory but follow guide at centminmod.com/phpfpm.html#customphpini to setup a custom php settings .ini file with the php setting changes so they persist across PHP upgrades.

    current list of custom php .ini settings files
    Code:
    php --ini
    Configuration File (php.ini) Path: /usr/local/lib
    Loaded Configuration File:         /usr/local/lib/php.ini
    Scan for additional .ini files in: /etc/centminmod/php.d
    Additional .ini files parsed:      /etc/centminmod/php.d/a_customphp.ini,
    /etc/centminmod/php.d/curlcainfo.ini,
    /etc/centminmod/php.d/geoip.ini,
    /etc/centminmod/php.d/igbinary.ini,
    /etc/centminmod/php.d/imagick.ini,
    /etc/centminmod/php.d/memcache.ini,
    /etc/centminmod/php.d/memcached.ini,
    /etc/centminmod/php.d/mongodb.ini,
    /etc/centminmod/php.d/redis.ini,
    /etc/centminmod/php.d/zendopcache.ini
    As there is an order to how php settings are loaded, you'd want the custom php .ini file for PHP sessions to be last so as to ensure no other custom .ini files take priority.

    So create /etc/centminmod/php.d/zzzz_phpsessions.ini and add to the file the PHP settings to switch PHP sessions over to Redis server

    Code:
    session.gc_maxlifetime=86400
    session.save_handler=redis
    session.save_path="tcp://127.0.0.1:6379?weight=1&timeout=2.5&database=3"
    if you set redis password in /etc/redis.conf you need to also pass the &auth=REDISPASS flag too

    Code:
    session.gc_maxlifetime=86400
    session.save_handler=redis
    session.save_path="tcp://127.0.0.1:6379?weight=1&timeout=2.5&database=3&auth=REDISPASS"
    Then restart PHP-FPM server

    Code:
    fpmrestart
    check the custom PHP .ini file is loaded
    Code:
    php --ini
    Configuration File (php.ini) Path: /usr/local/lib
    Loaded Configuration File:         /usr/local/lib/php.ini
    Scan for additional .ini files in: /etc/centminmod/php.d
    Additional .ini files parsed:      /etc/centminmod/php.d/a_customphp.ini,
    /etc/centminmod/php.d/curlcainfo.ini,
    /etc/centminmod/php.d/geoip.ini,
    /etc/centminmod/php.d/igbinary.ini,
    /etc/centminmod/php.d/imagick.ini,
    /etc/centminmod/php.d/memcache.ini,
    /etc/centminmod/php.d/memcached.ini,
    /etc/centminmod/php.d/mongodb.ini,
    /etc/centminmod/php.d/redis.ini,
    /etc/centminmod/php.d/zendopcache.ini,
    /etc/centminmod/php.d/zzzz_phpsessions.ini
    Then check PHPINFO output for those settings

    Code:
    php -i | egrep 'session.gc_maxlifetime|session.save'
    Code:
    php -i | egrep 'session.gc_maxlifetime|session.save'
    session.gc_maxlifetime => 86400 => 86400
    session.save_handler => redis => redis
    session.save_path => tcp://127.0.0.1:6379?weight=1&timeout=2.5&database=3 => tcp://127.0.0.1:6379?weight=1&timeout=2.5&database=3
    upload_2015-9-25_7-34-19.png

    Testing PHP sessions create a php file called testsession.php on server

    PHP:
    <?php
    //simple counter to test sessions. should increment on each page reload.
    session_start();
    $count = isset($_SESSION['count']) ? $_SESSION['count'] : 1;

    echo 
    $count;

    $_SESSION['count'] = ++$count;
    Access php file from web browser and then check your PHP Redis Admin database 3

    upload_2015-9-25_8-2-40.png
     
    Last edited: Sep 25, 2015
    • Like Like x 1
    • Informative Informative x 1
  6. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Xenforo Redis Cache Usage



    There's now a Zend Redis Cache Xenforo Addon by Xon. Currently, for Xenforo I use Libmemcached/Memcached backend caching with Xenforo.

    upload_2015-10-3_22-36-34.png

    Xenforo Libmemcached/Memcached server backend cache

    For Centmin Mod LEMP web stack powered Xenforo forum which has Libememcach/Memcached PHP extension and Memcached server installed out of the box.

    As per official Xenforo manual, within your Xenforo library/config.php add or alter settings to the following, caching the cache_id_prefix value from xfxx_ to a unique one to each Xenforo installed instance on your server

    PHP:
    $config['cache']['enabled'] = true;
    $config['cache']['cacheSessions'] = true;
    $config['cache']['frontend'] = 'Core';
    $config['cache']['frontendOptions'] = array(
    'caching' => true,
    'cache_id_prefix' => 'xfxx_',
    'automatic_serialization' => false,
    'lifetime' => 0
    );

    $config['cache']['backend'] = 'Libmemcached';

    $config['cache']['backendOptions'] = array(
    'servers' => array(
    array(
    'host' => '127.0.0.1',
    'port' => 11211,
    'weight' => 1
    )
    )
    );
    Xenforo Redis Cache Server backend cache via Zend Redis Cache Xenforo Addon

    For Centmin Mod LEMP web stack powered Xenforo forum which has Redis server support via REMI Yum repository installation and out of box Redis PHP extension support as outlined above. Be sure to install the Zend Redis Cache Xenforo Addon by Xon for this to work.

    You'll need to setup Redis server /etc/redis.conf as a LRU cache as outline at Using Redis as an LRU cache – Redis and in above 1st post. Basically. in /etc/redis.conf set a memory limit, eviction policy and LRU algorithm sample size.

    Example with 100MB max memory limit for Redis used as a LRU cache
    Code (Text):
    maxmemory 100mb
    maxmemory-policy allkeys-lru
    maxmemory-samples 10


    restart Redis server after making changes to /etc/redis.conf config file
    Code (Text):
    service redis restart


    As per FAQ Zend Redis Cache - FAQ | XenForo Community

    Within your Xenforo library/config.php add or alter settings to the following, caching the cache_id_prefix value from xfxx_ to a unique one to each Xenforo installed instance on your server

    PHP:
    $config['cache']['enabled'] = true;
    $config['cache']['frontend'] = 'Core';
    $config['cache']['frontendOptions']['cache_id_prefix'] = 'xfxx_';
    $config['cache']['backend'] = 'Redis';

    $config['cache']['backendOptions'] = array(
           
    'server' => '127.0.0.1',
           
    'port' => 6379,
           
    'database' => 1,
           
    'connect_retries' => 2,
           
    'use_lua' => true,
           
    'compress_data' => 2,
           
    'read_timeout' => 1,
           
    'timeout' => 1,
       );
    require(
    XenForo_Application::getInstance()->getConfigDir().'/SV/RedisCache/Installer.php');
    Ensure to flush Redis server cache after change

    To flush all redis databases
    Code (Text):
    redis-cli FLUSHALL
    

    To flush just database 8 if you set it differently from default database 0
    Code (Text):
    redis-cli -n 8 flushdb


    You may need to re-login to your Xenforo forum. Browser your forums to populate the Redis cache and then check via PHP Redis Admin for database 0 or database 1 cache entries.

    upload_2015-9-25_23-31-30.png
     
    Last edited: Mar 6, 2017
    • Informative Informative x 1
    • Useful Useful x 1
  7. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Nginx TCP Stream Module and Redis Cluster Load Balancing



    Redis server can also been setup in Redis Cluster mode. I won't go into that, you'd be on your own in reading and learning about that.

    However, once you have a Redis Cluster setup i.e. 3x Redis Masters + 3x Redis Slaves. You will want to be able to load balance that Redis Cluster. One simple way is via Nginx's new TCP stream module outlined at TCP Load Balancing & Proxy and at TCP Load Balancing with NGINX 1.9.0 and NGINX Plus R6

    For example, if I setup the Redis Cluster on ports for 3 masters on 23001, 23002 and 23003 and 3 slaves on 23004, 23005, 23006, then I can setup Nginx TCP stream load balancing across the 3 masters on ports 23001, 23002 and 23003.

    Code:
    redis-cli -p 23001 cluster info
    cluster_state:ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:6
    cluster_size:3
    cluster_current_epoch:6
    cluster_my_epoch:1
    cluster_stats_messages_sent:29957
    cluster_stats_messages_received:29957
    Code:
    redis-cli -p 23001 cluster nodes
    a7c81bc25886da310e3fc11a5547dbe03afec86e 127.0.0.1:23005 slave f7a3d975b1835acb116078df325c049507b9ceb3 0 1443217898164 5 connected
    e0eb3c5c872e0733642d4daa7258cbd4fa4f8504 127.0.0.1:23001 myself,master - 0 0 1 connected 0-5460
    f7a3d975b1835acb116078df325c049507b9ceb3 127.0.0.1:23002 master - 0 1443217898766 2 connected 5461-10922
    4a454f8306a9ae1279a4479e0fc9577acb99f974 127.0.0.1:23003 master - 0 1443217898164 3 connected 10923-16383
    0ca5004c6578c7ba9dab8afc5a87b785340d6d58 127.0.0.1:23004 slave e0eb3c5c872e0733642d4daa7258cbd4fa4f8504 0 1443217898766 4 connected
    051bf28c01de5d08aee0232a74841e743839236f 127.0.0.1:23006 slave 4a454f8306a9ae1279a4479e0fc9577acb99f974 0 1443217898164 6 connected
    The very basic Nginx TCP load balancing stream setup has to be added to nginx.conf (cmd shortcut = nginxconf to invoke nano text editor to edit nginx.conf).

    TCP load balance 3x Redis master servers in a upstream called backend and have Nginx listen on 19001 port. So your web apps connecting to Redis server will instead of connect to 127.0.0.1:6379 now connects to 127.0.0.1:19001
    Code:
    stream {
      upstream backend {
        server 127.0.0.1:23001;
        server 127.0.0.1:23002;
        server 127.0.0.1:23003;
      }
    
      server {
        listen 19001;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
      }
    }
    The stream{} context is set on it's own and not within any other context in nginx.conf. For example:
    Code:
    events {
        worker_connections  4096;
        accept_mutex on;
        accept_mutex_delay 200ms;
        use epoll;
        #multi_accept on;
    }
    
    stream {
      upstream backend {
        server 127.0.0.1:23001;
        server 127.0.0.1:23002;
        server 127.0.0.1:23003;
      }
    
      server {
        listen 19001;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
      }
    }
    
    http {
    More advanced upstream options available at nginx.org/en/docs/stream/ngx_stream_upstream_module.html#upstream

    Code:
    stream {
      upstream backend {
        zone     upstream_backend 10m;
        least_conn;
    
        server 127.0.0.1:23001;
        server 127.0.0.1:23002;
        server 127.0.0.1:23003;
      }
    
      server {
        listen 19001 reuseport;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
      }
    }
    Unfortunately, unlike HTTP Nginx upstreams, Nginx TCP upstreams don't show up in Centmin Mod Nginx's Vhost Traffic Status module output

    upload_2015-9-26_8-22-48.png

    Redis Benchmark



    Doing some redis-benchmark runs. Note these benchmarks are run on same server as the Redis server(s) and Redis Cluster so numbers would reflect if there was remote client and remote Redis server(s) setups.

    Direct with single Redis server on 127.0.0.1:6379. Direct will always be faster when testing client tool is on same server as Redis server(s) but results will change when the Redis server is remote.
    Code:
    redis-benchmark -h 127.0.0.1 -p 6379 -r 1000000 -n 2000000 -t get,set,lpush,lpop -P 16 -q
    SET: 528401.56 requests per second
    GET: 669120.06 requests per second
    LPUSH: 806776.88 requests per second
    LPOP: 740192.50 requests per second
    Against the Redis Cluster itself on 127.0.0.1:23001 master
    Code:
    redis-benchmark -h 127.0.0.1 -p 23001 -r 1000000 -n 2000000 -t get,set,lpush,lpop -P 16 -q
    SET: 377358.47 requests per second
    GET: 544365.81 requests per second
    LPUSH: 342348.50 requests per second
    LPOP: 368188.50 requests per second
    With Nginx TCP load balanced stream on 127.0.0.1:19001
    Code:
    redis-benchmark -h 127.0.0.1 -p 19001 -r 1000000 -n 2000000 -t get,set,lpush,lpop -P 16 -q
    SET: 453309.12 requests per second
    GET: 677277.31 requests per second
    LPUSH: 657030.25 requests per second
    LPOP: 612557.44 requests per second
    Or testing 6x Redis Single Servers in Nginx TCP Load Balanced Setup

    Code:
      upstream backend-multi {
        zone     upstream_backend-multi 10m;
        least_conn;
    
        server 127.0.0.1:6379;
        server 127.0.0.1:6380;
        server 127.0.0.1:6381;
        server 127.0.0.1:6382;
        server 127.0.0.1:6383;
        server 127.0.0.1:6384;
      }
    
      server {
        listen 19002 reuseport backlog=4096;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend-multi;
      }
    Code:
    redis-benchmark -h 127.0.0.1 -p 19002 -r 1000000 -n 2000000 -t get,set,lpush,lpop -P 16 -q
    SET: 606796.12 requests per second
    GET: 626370.19 requests per second
    LPUSH: 694444.44 requests per second
    LPOP: 677048.06 requests per second
    Configuring PHP Redis Admin for multiple Redis server and Redis Cluster master stats

    redisadmin-multi-01.png
     
    Last edited: Sep 28, 2015
    • Informative Informative x 3
  8. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Wordpress Redis Cache at Nginx Level Revision



    Revisied the Wordpress Redis Cache at Nginx level to be closer to what gets created in Centmin Mod 123.09beta01's centmin.sh menu option 22 Wordpress auto installs for redis related include files outlined here.
     
  9. eva2000

    eva2000 Administrator Staff Member

    30,168
    6,785
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,137
    Local Time:
    7:13 AM
    Nginx 1.13.x
    MariaDB 5.5

    Redis Cluster & Replication Setups



    For local server testing, I created a tool called redis-generator.sh to be able to auto generate standalone, replication and cluster based Redis configurations on local 127.0.0.1 listener setups on CentOS 7.x 64bit systems only. Used for quick testing as production real world usage would require setting up multiple Redis servers on multiple servers and not just a single server.

    Tool is provided as is without support and can be found at https://github.com/centminmod/centminmod-redis.

    Examples
     
    Last edited: Mar 20, 2017
    • Like Like x 2
Thread Status:
Not open for further replies.