Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Memcached : Setup and tests on Debian/Ubuntu  (Read 1330 times)

0 Members and 1 Guest are viewing this topic.

joseletk

  • Guest
Memcached : Setup and tests on Debian/Ubuntu
« on: April 20, 2018, 07:37:51 pm »
Memcached is a distributed memory caching system. It caches data and objects in RAM in order to reduce the frequency of access to the database or to an external API. Consequently, it helps to speed up dynamic websites.

To protect user data, sessions are stored in files on the servers themselves. This has several disadvantages:

  • The user must be sticked to a front to maintain it’s session
  • It generates a a lot of disk IO on servers

Using Memcached to store sessions:

  • Removes the disk IO related sessions
  • Allows not to stick a user to a server (and to complete round robin on a server pool

Install & Tests

We need Apache running. Let’s say Apache is listening on 192.168.1.10

edit /var/www/php-info.php like below:

Code: [Select]
<?php
php phpinfo
() ;
?>

The 2 packages that we need are :

  • Memcached
  • PHP5 memcache module

# apt-get install memcached php5-memcache

After the installation, memcached should already be running. You can check that by typing

Code: [Select]
server1:~# netstat -tap | grep memcached
tcp        0      0 *:11211                 *:*                     LISTEN     3053/memcached

By default, memcached is using port 11211 and is listening on all interfaces. So, to avoid that anyone (as memcached has no build-in authentication mechanisms) can connect to it and use it we have two solutions:

1. Close port 11211 in your firewall.
2. Configure memcached to listen only on localhost.

Here, I’ll use the second method.

Following are the steps:

1. Edit /etc/memcached.conf
2. Modify the list of the IP addresses to listen on (from all IP addresses to localhost address) by adding: -l 127.0.0.1 to the configuration.

You can also do (optional steps):

1. Adjust the other settings in the memcached configuration (the file contains explanations for each setting).
2. Make sure that memcached is listening on a firewalled interface (this is one of the only security measures taken by memcached.

Rerun netstat after restarting memcached:

Code: [Select]
/etc/init.d/memcached restart
netstat -tap | grep memcached

Memcached is now only listening on localhost:

Code: [Select]
server1:~# netstat -tap | grep memcached
tcp       0         0     localhost.localdo:11211         *:*           LISTEN    3092/memcached

Client session

We can use our script /var/www/php-info.php to know which php.ini is loaded

Edit the used php.ini and search for the ‘[Session]’ area as displayed below

Code: [Select]
[Session]
; Handler used to store/retrieve data.
; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler
session.save_handler = files

And change it to this:
Code: [Select]
[Session]
; Handler used to store/retrieve data.
; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler
session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

Now, we have finished with the required changes and the session_handler is using memcached.

For multiple memcached servers listening on IP1, IP2 and IP3 :

Code: [Select]
[Session]
; Handler used to store/retrieve data.
; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler
session.save_handler = memcached
session.save_path = "tcp://IP1:11211, tcp://IP2:11211, tcp://IP3:11211"

Restarting Apache is mandatory in order that the new PHP configuration takes effect:

/etc/init.d/apache2 restart

Now, open php-info.php again in a browser: http://192.168.0.10/php-info.php

Search for memcache ans you should see it mentioned on the page. It is working as expected.
=========================================================================