Install Memcached (Caching Server) on RHEL/CentOS 6.3/5.8 and Fedora 17-12
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.
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 program by using following command with YUM tool.
# yum install memcached
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!
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.
- PORT : The port used by memcached to run.
- USER : The start-up daemon for memcached service.
- 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.
- CACHESIZE : Set cache size memory to 2048. For busy servers you can increase upto 4GB.
- OPTIONS : Set IP address of server, so that Apache or Nginx web servers can connect to it.
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
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 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.