INSTALL SEPARATE MEMCACHE SERVER (SESSION) FOR MOODLE ON CENTOS 7

SCENARIO

We want to store session in a different server for our 2 web clients in the cluster

So we have 3 servers:

session=10.10.4.29 (session server)
web1=10.10.4.21
web2=10.10.4.22

SERVER (session:10.10.4.29)

$ sudo yum update
$ sudo yum install memcached
$ sudo vi /etc/sysconfig/memcached

PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”64″
OPTIONS=””

MEMCACHED COMMANDS
$ sudo systemctl start memcached
$ sudo systemctl status memcached
$ sudo systemctl enable memcached
$ sudo systemctl stop memcached
$ sudo systemctl restart memcached

FIREWALL
$ sudo firewall-cmd --permanent --zone=public --add-port=11211/tcp
$ sudo firewall-cmd --reload

confirm memcached running status
$ memcached-tool 127.0.0.1:11211 stats

#127.0.0.1:11211   Field             Value
             accepting_conns           1
                   auth_cmds           0
                 auth_errors           0
                      vbytes           0
              	  bytes_read           7
               bytes_written           0
                  cas_badval           0
                    cas_hits           0
                  cas_misses           0
                   cmd_flush           0
                     cmd_get           0
                     cmd_set           0
                   cmd_touch           0
             	 conn_yields           0
       connection_structures          11
            curr_connections          10
                  curr_items           0
                   decr_hits           0
             	 decr_misses           0
                 delete_hits           0
               delete_misses           0
      	   evicted_unfetched           0
                   evictions           0
       	   expired_unfetched           0
                    get_hits           0
                  get_misses           0
                  hash_bytes      524288
       	   hash_is_expanding           0
            hash_power_level          16
              	   incr_hits           0
                 incr_misses           0
      libevent 2.0.21-stable
              limit_maxbytes    67108864
         listen_disabled_num           0
                         pid        3988
                pointer_size          64
               	   reclaimed           0
                reserved_fds          20
               rusage_system    0.007483
                 rusage_user    0.001247
                     threads           4
                        time  1509482646
           total_connections          11
                 total_items           0
                  touch_hits           0
                touch_misses           0
                      uptime          73
                     version      1.4.15

$ sudo netstat -tulpn | grep :11211

tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      4106/memcached      
tcp6       0      0 :::11211                :::*                    LISTEN      4106/memcached      
udp        0      0 0.0.0.0:11211           0.0.0.0:*                           4106/memcached      
udp6       0      0 :::11211                :::*                                4106/memcached 

CLIENT (WEB1 and WEB2)

install first nmap and netstat
$ sudo yum install nmap netstat

install memcache
$ sudo yum install memcached

point session server both on web1 and web2
$ sudo vi /etc/hosts

10.10.4.29 session

check the remote connectivity from client server(web1 and web2)
$ echo stats | nc session 11211
$ echo stats | nc 10.10.4.29 11211

STAT pid 3988
STAT uptime 423
STAT time 1509482996
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.007939
STAT rusage_system 0.013232
STAT curr_connections 10
STAT total_connections 12
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 13
STAT bytes_written 1023
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

check if memcached exists
$ php -i | grep memcached

/etc/php.d/z-memcached.ini,
memcached
memcached support => enabled
libmemcached version => 1.0.16
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.default_binary_protocol => no value => no value
memcached.default_connect_timeout => 0 => 0
memcached.default_consistent_hash => no value => no value
memcached.serializer => igbinary => igbinary
memcached.sess_binary_protocol => 1 => 1
memcached.sess_connect_timeout => 1000 => 1000
memcached.sess_consistent_hash => 1 => 1
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => not set => not set
memcached.sess_lock_retries => 5 => 5
memcached.sess_lock_wait => not set => not set
memcached.sess_lock_wait_max => 2000 => 2000
memcached.sess_lock_wait_min => 1000 => 1000
memcached.sess_locking => 1 => 1
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_persistent => no value => no value
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => no value => no value
memcached.sess_remove_failed_servers => no value => no value
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.sess_server_failure_limit => 0 => 0
memcached.store_retry_count => 2 => 2
Registered save handlers => files user redis rediscluster memcached 
session.save_handler => memcached => memcached

or check the module itself
$ php -m | grep memcache

memcached

If you still don’t see the memcached then install the php memcache
$ sudo yum install php71w-pecl-memcached

uncomment save_handler to force it seen in phpinfo()
$ sudo vi /etc/php.d/z-memcached.ini

session.save_handler=memcached

restart memcached on the server and nginx & php-fpm on each web client
$ sudo systemctl restart memcached (on session server)
$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm

put info.php file under each web server /usr/share/nginx/html/info.php
$ sudo vi /usr/share/nginx/html/info.php

Now you should see memcached library in each part

http://10.10.4.21/info.php
http://10.10.4.22/info.php

TEST

implement a test script
$ sudo vi /usr/share/nginx/html/testmemcached.php

<?php 
addServer("10.10.4.29", 11211);

$result = $mc->get("test_key");

if($result) {
  echo $result;
} else {
  echo "No data on Cache. Please refresh page pressing F5";
  $mc->set("test_key", "test data pulled from Cache!") or die ("Failed to save data at Memcached server");
}
?>

go to web1 and web2 directly

http://10.10.4.21/testmemcached.php
http://10.10.4.22/testmemcached.php

Finally go to the config.php in moodle and enable the session handler block
$ sudo vi /usr/share/nginx/html/config.php

//   Memcached session handler (requires memcached server and extension):
$CFG->session_handler_class = '\core\session\memcached';
$CFG->session_memcached_save_path = ’10.10.4.29:11211';
$CFG->session_memcached_prefix = 'memc.sess.key.';
$CFG->session_memcached_acquire_lock_timeout = 120;
$CFG->session_memcached_lock_expire = 7200;       // Ignored if PECL memcached is below version 2.2.0
Advertisements

4 thoughts on “INSTALL SEPARATE MEMCACHE SERVER (SESSION) FOR MOODLE ON CENTOS 7

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s