Did You Know?
Donate to TecMint

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

Install Memcached (Caching Server) on RHEL/CentOS 6.3/5.8 and Fedora 17-12

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

This article shows how to install Memcached Server on RHEL 6.3/6.2/6.1/6/5.8/5.6, CentOS 6.3/6.2/6.1/6/5.8/5.6 and Fedora 17,16,15,14,13,12 using package manager called YUM. By default, Memcached program 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.

Install Memcached in Linux

Install Memcached in Linux

Before, we move further steps for installation, let’s discuss what is Memcached? and what purpose it is used.

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.

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. (Note : Fedora doesn’t required EPEL repo, because it is part of fedora project).

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
Sample Output
Loaded plugins: fastestmirror
Determining fastest mirrors
epel: kartolo.sby.datautama.net.id
Dependencies Resolved

=====================================================================================================
 Package			Arch				Version					Repository					Size
=====================================================================================================
Installing:			
memcached			i386				1.4.5-1.el5				epel						71 k

Transaction Summary
=====================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 71 k
Is this ok [y/N]: y
Downloading Packages:
memcached-1.4.5-1.el5.i386.rpm													|  71 kB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : memcached														          1/1

Installed:
  memcached.i386 0:1.4.5-1.el5

Complete!

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

Cache MySQL Queries with Memcached

It isn’t an easy task for all, you need to use API’s to modify your PHP codes to enable MySQL caching. You can find the examples codes at Memcache with MySQL and PHP.

Enable Memcached on WordPress Sites

For WordPress based sites, there is a plugin called Memcached Object Cache that you need to install it on your WordPress CMS.

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Your name can also be listed here. Work as a Paid freelancer/writer at TecMint.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

8 Responses

  1. Ravi says:

    Good one ….

  2. FiFi says:

    In the Enable EPEL Repository section, under RHEL 5/CentOS 5 64-bit, the second command has an extra “wget” at the start. It should just be an “rpm” command like the second command in the other related sections, but it has “wget rpm …” and the “wget ” is extra.

  3. Umesh Gaire says:

    Hi, I am a subscriber of your site. And I found a confusion; at the firewall you have inserted the IP ranges from 172.16.1.1 to 172.16.1.10 for Port 11211

    what I need to put there ? should I need to insert there my vps’ ip address or local ipaddress ? Please help me. What I need to put there ?

  4. RAJ says:

    Hi,

    Every thing fine but now how can we route all the Users to Memcached server to avail the cache service?

  5. bala says:

    when editing memchached file pls put -l local host that is not “-1″ thats “l”

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.