Skip to content

Memcached – Installation and Configuration Guide

What is Memcached?

Memcached is an open source distributed memory object caching program that allows us to improve and speeding up performance of dynamic web applications by caching data and objects in Memory. Memcached is also used to cache entire database tables and queries to improve performance of database. It is the only one caching system available freely and used by many big sites like YouTube, Facebook, Twitter, Reddit, Drupal, Zynga etc.

This article shows how to install Memcached Server using package manager called YUM. By default, Memcached program is not available under Linux, you need to enable and install third party repository called EPEL to install Memcached program on Linux. The EPEL repository is provided by Fedora project that has collection up-to-date packages for RHEL/CentOS/Fedora.

Enable EPEL repository under RHEL/CentOS 6.3/5.8

The fastest and easiest way to install and enable EPEL repository using YUM. First, select the RPM that matches your Linux OS architecture from the provided links and install it using method shown in below. The EPEL repo will install all the required dependency packages for memcached.

For RHEL/CentOS 6 (32-Bit)

# wget http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-7.noarch.rpm

# rpm -Uvh epel-release-6-7.noarch.rpm

For RHEL/CentOS 6 (64-Bit)

# wget http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm

# rpm -Uvh epel-release-6-7.noarch.rpm

For RHEL/CentOS 5 (32-Bit)

# wget http://mirrors.kernel.org/fedora-epel/5/i386/epel-release-5-4.noarch.rpm

# rpm -Uvh epel-release-5-4.noarch.rpm

For RHEL/CentOS 5 (64-Bit)

# wget http://mirrors.kernel.org/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm

# rpm -Uvh epel-release-5-4.noarch.rpm

Install Memcached

Install Memcached program by using following command with YUM tool.

# yum install memcached

Configure Memcached

Open the file called /etc/sysconfig/memcached with VI editor.

# vi /etc/sysconfig/memcached

Set or update parameters as follows, save the file and exit.

# Running on Port 11211

PORT=”11211″

# Start as memcached daemon

USER=”memcached”

# Set max simultaneous connections to 1024

MAXCONN=”1024″

# Set Memory size to 2048 – 4GB(4096)

CACHESIZE=”2048″

#Set server IP address

OPTIONS=”-l 127.0.0.1″

Let’s discuss each of the above parameters in details.

  1. PORT : The port used by memcached to run.
  2. USER : The start-up daemon for memcached service.
  3. MAXCONN : The value used to set max simultaneous connections to 1024. For busy web servers you can increase to any number based on your requirements.
  4. CACHESIZE : Set cache size memory to 2048. For busy servers you can increase upto 4GB.
  5. OPTIONS : Set IP address of server, so that Apache or Nginx web servers can connect to it.

Start Memcached

Type the following commands to start and restart the Memcached daemon.

# chkconfig –levels 235 memcached on

# /etc/init.d/memcached start

# /etc/init.d/memcached restart

To stop and check status, use the following commands.

# /etc/init.d/memcached stop

# /etc/init.d/memcached status

Verify Memcached

Use netstat command to verify Memcached is running.

# netstat -tulpn | grep :11211

tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 20775/memcached

udp 0 0 127.0.0.1:11211 0.0.0.0:* 20775/memcached

Check the stats of the server using memcached-tool.

# memcached-tool 127.0.0.1 stats

Install Memcached PHP extension

Now, install PHP extension to work with Memcached daemon.

# yum install php-pecl-memcache

Install Memcached Perl Library

Install perl library for Memcached.

# yum install perl-Cache-Memcached

Install Memcached Python Library

Install python library for Memcached.

# yum install python-memcached

Restart Apache

Restart the Apache service to reflect changes.

# /etc/init.d/httpd restart

OR

# service httpd restart

Configure Firewall to Secure Memcached Server

Make sure you only have access to memcached server, to enable access to your own servers open file called /etc/sysconfig/iptables.

# vi /etc/sysconfig/iptables

Append the following iptables rules to allow access to your own servers.

## Enable access on IP ranges from 172.16.1.1 to 172.16.1.10 for Port 11211 ##

# iptables -A INPUT -p tcp –destination-port 11211 -m state –state NEW -m iprange –src-range 172.16.1.1-172.16.1.10 -j ACCEPT

# iptables -A INPUT -p udp –destination-port 11211 -m state –state NEW -m iprange –src-range 172.16.1.1-172.16.1.10 -j ACCEPT

Restart the iptables service to reflect changes.

# service iptables restart

OR

# /etc/init.d/iptables restart

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security