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.1.29 (session server)
web1=10.10.1.21
web2=10.10.1.22

SERVER (session:10.10.1.29)

$ sudo yum update
$ sudo yum install memcached
$ sudo vi /etc/sysconfig/memcached
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=“64″
OPTIONS=“”

start memcached

$ sudo systemctl start memcached
$ sudo systemctl enable memcached
$ sudo systemctl restart memcached (if needed later
$ 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

check the remote connectivity from another server, it should run

$ echo stats | nc session 11211
$ echo stats | nc 10.10.1.29 11211

 

CLIENT

install memcached PHP module to work with PHP5

$ sudo yum install php-memcached

check if memcached exists

$ php -i | grep 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 server)
$ sudo systemctl restart nginx
$ sudo systemctl restart php-fpm
$ sudo systemctl restart httpd (if apache)

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.1.21/info.php
http://10.10.1.22/info.php

TEST

implement a test script

$ sudo vi /usr/share/nginx/html/testmemcached.php

<?php
$mc = new Memcached();
$mc->addServer("10.10.1.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.1.21/testmemcached.php
http://10.10.1.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 = '127.0.0.1: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

One thought 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s