Memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load. Memcached is a very useful also in other cases, than only dynamic web applications.
This guide explains howto install Memcached 1.4.15/1.4.7/1.4.5/1.4.4 stable version of distributed memory object caching system on Fedora 18/17/16/15/14/13/12 and CentOS / Red Hat (RHEL) 6.3/6.2/6.1/6/5.9. Fedora 18/17/16/15/14/13/12, CentOS 6 and Red Hat (RHEL) 6 has Memcached on default repos, so extra repositories is not needed.
Install Memcached 1.4.15/1.4.7/1.4.5 on Fedora 18/17/16/15/14/13/12, CentOS/Red Hat (RHEL) 6.3/6.2/6.1/6/5.9
Install Remi repository (not needed on Fedora 18/17/16/15/14/13/12 and RHEL 6):
## Remi Dependency on CentOS and Red Hat (RHEL)
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
Install Memcached package
Fedora 18/17/16/15/14/13/12, CentOS/Red Hat (RHEL) 6.3/6.2/6.1/6
yum install memcached
CentOS/Red Hat (RHEL) 5.9
yum--enablerepo=remi install memcached
Configure memcached
Most important value to configure is CACHESIZE, which is cache size on Megabytes. Example Following configuration use 512 Mb memory for Memcached
# Set Memcached to start automatically on boot
chkconfig memcached on
# Start Memcached/etc/init.d/memcached start
## OR ##
service memcached start
Check that Memcached is Running and Working
echo stats | nc localhost 11211
STAT pid 7599
STAT uptime10
STAT time1265288542
STAT version 1.4.4
STAT pointer_size 32
STAT rusage_user 0.003999
STAT rusage_system 0.052991
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 6
STAT bytes_written 0
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END
# Try to get some valueecho get some_value | nc localhost 11211
END
# Not found, but check the stats againecho stats | nc localhost 11211
STAT pid 7599
STAT uptime10
STAT time1265288542
STAT version 1.4.4
[...]
STAT cmd_get 1
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 1
STAT delete_misses 0[...]
STAT evictions 0
END
Everything working nice. Then it’s time to test memcache example with some web application.
Open Memcached Port (11211) on Iptables Firewall (If the Memcached will also be used other local servers)
Edit /etc/sysconfig/iptables file:
nano-w/etc/sysconfig/iptables
Add following INPUT line:
-A INPUT -m state --state NEW -m tcp -p tcp --dport11211-j ACCEPT
Restart Iptables Firewall:
service iptables restart
## OR ##/etc/init.d/iptables restart
Test remote connection:
echo stats | nc memcache_host_name_or_ip 11211
Install Memcache and/or Memcached PHP Module on Fedora, CentOS and Red Hat (RHEL)
The above instructions work fine for me with Fedora 12. Fedora 13 wasn’t working. /usr/lib/php/modules/memcache.so was missing. Copying that file from Fedora 12 installation seems to work, but I wonder why it was missing. rpm -ql memcached doesn’t show memcache.so.
cant installl
—> Package perl-common-sense.i386 0:3.0-1.el5.rf set to be updated
–> Finished Dependency Resolution
perl-AnyEvent-5.240-1.el5.rf.noarch from rpmforge has depsolving problems
–> Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
Error: Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
You could try using –skip-broken to work around the problem
You could try running: package-cleanup –problems
package-cleanup –dupes
rpm -Va –nofiles –nodigest
Hi Jeff,
Thanks a lot, the installation is done, class Memcache work fine but the class Memcached is not working, i get the message : Fatal error: Class ‘Memcached’ not found !
have you any idea about that
Actually EPEL for RHEL installs and outdated version of memcached (1.4.5, released 2 years ago), I suggest using at least memcached 1.4.10. Starting this release there is a big performance and scalability improvements, see developer notes.
Using PowerStack repository you can easily run latest memcached stable version in your CentOS boxes (4, 5 and 6 releases supported, i686 + x86_64), last LAMP stack stable versions and other key packages like: nginx, HAproxy, node.js, redis, Varnish, etc.
All code + .spec files are available on GitHub, check out the wiki page for installation instructions:
I have to admit that I am not yet convinced quality of this repository. Then I believe this, when the repo has been in existence for a year or two and have still actively updated packages… ;)
Hey JR! Thought you could get rid of me once my nginx was setup I bet, didn’t you :) Just kidding.
If I want to have nginx and memcached on a different server, do I need to install memcached on both servers for them to talk properly? I am currently setting my virtual host on nginx to send items to memcached on a proxy server, and if it fails to launch php on the live server. – http://pastebin.com/zZ3VAePi
I am receiving a 404 like this, but if i switch it back to just php it works fine. Is that virtual host correct? Am I receiving the 404 because the memcached server is not working properly?
That was what I had above, but I noticed I had commented out the “error_page 404 = @cache_miss:” for some reason. Guess I just needed a little push in the right direction :)
Thanks again JR! You are amazing with helping and always so quick.
I have installed everything on Centos 5.7 but in the last step:
===========
Install Memcached Module and PHP
yum install php php-pecl-memcached
===========
i got this message:
===========
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* atomic: www5.atomicorp.com
* epel: mirrors.servercentral.net
* rpmforge: ftp-stud.fht-esslingen.de
1167 packages excluded due to repository priority protections
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package php-pecl-memcached.x86_64 0:1.0.0-1.el5 set to be updated
–> Processing Dependency: php-zend-abi = 20050922 for package: php-pecl-memcached
–> Finished Dependency Resolution
php-pecl-memcached-1.0.0-1.el5.x86_64 from epel has depsolving problems
–> Missing Dependency: php-zend-abi = 20050922 is needed by package php-pecl-memcached-1.0.0-1.el5.x86_64 (epel)
Error: Missing Dependency: php-zend-abi = 20050922 is needed by package php-pecl-memcached-1.0.0-1.el5.x86_64 (epel)
You could try using –skip-broken to work around the problem
You could try running: package-cleanup –problems
package-cleanup –dupes
rpm -Va –nofiles –nodigest
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* remi: remi-mirror.dedipower.com
87 packages excluded due to repository priority protections
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package php-pecl-memcached.x86_64 0:2.0.1-1.el6.remi will be installed
–> Processing Dependency: php-pecl-igbinary(x86-64) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Processing Dependency: libmemcached.so.9()(64bit) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Running transaction check
—> Package php-pecl-igbinary.x86_64 0:1.1.1-3.el6.remi will be installed
—> Package php-pecl-memcached.x86_64 0:2.0.1-1.el6.remi will be installed
–> Processing Dependency: libmemcached.so.9()(64bit) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Finished Dependency Resolution
Error: Package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64 (remi)
Requires: libmemcached.so.9()(64bit)
You could try using –skip-broken to work around the problem
You could try running: rpm -Va –nofiles –nodigest
Basically I installed and followed the instructions but when I try to access if class_exists from PHP it’s returning a false. (No info is visible either in phpinfo )
The even stranger thing is that I installed PHP using xampp and it states it is the 5.3.8 even in phpinfo (I hope this makes sense – if not sorry but I’m new to this)
I also noticed you where asking for this before:
[root@localhost /]# yum repolist
Loaded plugins: fastestmirror, protectbase, security
Loading mirror speeds from cached hostfile
* base: centos.fastbull.org
* epel: http://ftp.ps.pl
* extras: centos.fastbull.org
* rpmforge: fr2.rpmfind.net
* updates: swissmirror.silyus.net
0 packages excluded due to repository protections
repo id repo name status
base CentOS-5 - Base 2,725
epel Extra Packages for Enterprise Linux 5 - i386 5,675
extras CentOS-5 - Extras 276
rpmforge RHEL 5 - RPMforge.net - dag 11,352
updates CentOS-5 - Updates 214
repolist: 20,242
Hi JR,
I’ve succesfully installed memcached, I also enabled APC. Can I use memcached and APC together?
I have 2GB VPS with 3 virtual servers. And I use wordpress.
Do I still need the backend plugins for memcached and APC?
Currently I am also using WP Super Cache. So I have WP Super Cache as plugin, and APC + memcached as PHP extension.
Did I do is correct? Please help me.
Thanks,
The above instructions work fine for me with Fedora 12. Fedora 13 wasn’t working. /usr/lib/php/modules/memcache.so was missing. Copying that file from Fedora 12 installation seems to work, but I wonder why it was missing. rpm -ql memcached doesn’t show memcache.so.
Hi Charles,
I verified again that the this guide is working also with Fedora 13.
If you want install Memcache PHP module then you need to install following package:
yum install php-pecl-memcacheThen you get /usr/lib/php/modules/memcache.so file right place.
I added this also on end of this guide. :)
Nice site…and nice work ..
Keep up…..
Hi,
thank for this nice tutorial for installing memcached, i’d to know what the best memory size for memcached on vps with 512MB of ram?
Of course, it depends on the intended use and the fact that whatever else the server is running, but maybe about64-192 MB is good for memcached.
no response from: rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
site appears to be dead
Hi jeff,
For me it’s working normally…maybe it was down when you try to install it. So please try again.
Thanks!
cant installl
—> Package perl-common-sense.i386 0:3.0-1.el5.rf set to be updated
–> Finished Dependency Resolution
perl-AnyEvent-5.240-1.el5.rf.noarch from rpmforge has depsolving problems
–> Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
Error: Missing Dependency: perl(Net::SSLeay) >= 1.33 is needed by package perl-AnyEvent-5.240-1.el5.rf.noarch (rpmforge)
You could try using –skip-broken to work around the problem
You could try running: package-cleanup –problems
package-cleanup –dupes
rpm -Va –nofiles –nodigest
Hi onel0ve,
It seems that the problem is caused by rpmforge repo packages.
Could you first post output of following command:
yum repolistHi Jeff,
Thanks a lot, the installation is done, class Memcache work fine but the class Memcached is not working, i get the message : Fatal error: Class ‘Memcached’ not found !
have you any idea about that
Hi HaYsSeN,
If you want to install class Memcached then you need to install php-pecl-memcached package too:
yum install php-pecl-memcachedI added this to guide too.
Hi. Loved this very clear instruction.
I already have Memcached 1.2.0 installed:
> memcached -h
memcached 1.2.0
-p port number to listen on
-s unix socket path to listen on (disables network support)
....
I have the usual CentOS Base, EPEL, and even the REMI repositories installed now.
> yum repolist
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* epel: fedora-epel.mirror.lstn.net
* remi: rpms.famillecollet.com
repo id repo name status
addons CentOS-5 - Addons enabled : 0
base CentOS-5 - Base enabled : 3,566
centosplus CentOS-5 - Plus enabled : 57
contrib CentOS-5 - Contrib enabled : 0
epel Extra Packages for Enterprise Linux 5 - enabled : 6,851
extras CentOS-5 - Extras enabled : 266
remi Les RPM de remi pour Enterprise Linux 5 enabled : 641
updates CentOS-5 - Updates enabled : 569
repolist: 11,950
But when I do a:
yum --enablerepo=remi update memcached
It somehow updates it to “1.4.5″. So I restart memcached with “service memcached restart”.
> service memcached restart
Stopping memcached: [FAILED]
Starting memcached: [ OK ]
Then when I do “memcached -h” again, I still see 1.2.0.
What am I missing?
Thanks.
Hi Phoenix,
Nice to see you here!
Could you post output of following command:
rpm -qa memcachedThanks.
Actually EPEL for RHEL installs and outdated version of memcached (1.4.5, released 2 years ago), I suggest using at least memcached 1.4.10. Starting this release there is a big performance and scalability improvements, see developer notes.
Using PowerStack repository you can easily run latest memcached stable version in your CentOS boxes (4, 5 and 6 releases supported, i686 + x86_64), last LAMP stack stable versions and other key packages like: nginx, HAproxy, node.js, redis, Varnish, etc.
All code + .spec files are available on GitHub, check out the wiki page for installation instructions:
http://powerstack.org
Hi Santi Saez,
Thanks for sharing.
I have to admit that I am not yet convinced quality of this repository. Then I believe this, when the repo has been in existence for a year or two and have still actively updated packages… ;)
Hey JR! Thought you could get rid of me once my nginx was setup I bet, didn’t you :) Just kidding.
If I want to have nginx and memcached on a different server, do I need to install memcached on both servers for them to talk properly? I am currently setting my virtual host on nginx to send items to memcached on a proxy server, and if it fails to launch php on the live server. – http://pastebin.com/zZ3VAePi
I am receiving a 404 like this, but if i switch it back to just php it works fine. Is that virtual host correct? Am I receiving the 404 because the memcached server is not working properly?
Hi again Hogie! :)
First, you don’t need memcached on both servers.
Your virtual host should look like following (if you want cache PHP requests):
Do you open 11211 port on another server firewall?
You can test connection to another server with following command:
That was what I had above, but I noticed I had commented out the “error_page 404 = @cache_miss:” for some reason. Guess I just needed a little push in the right direction :)
Thanks again JR! You are amazing with helping and always so quick.
hi ! i tryed to install this on my centos 5.5 …while 5 hours and it was so simply ….. thanks a lot from a newbee
I have installed everything on Centos 5.7 but in the last step:
===========
Install Memcached Module and PHP
yum install php php-pecl-memcached
===========
i got this message:
===========
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* atomic: www5.atomicorp.com
* epel: mirrors.servercentral.net
* rpmforge: ftp-stud.fht-esslingen.de
1167 packages excluded due to repository priority protections
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package php-pecl-memcached.x86_64 0:1.0.0-1.el5 set to be updated
–> Processing Dependency: php-zend-abi = 20050922 for package: php-pecl-memcached
–> Finished Dependency Resolution
php-pecl-memcached-1.0.0-1.el5.x86_64 from epel has depsolving problems
–> Missing Dependency: php-zend-abi = 20050922 is needed by package php-pecl-memcached-1.0.0-1.el5.x86_64 (epel)
Error: Missing Dependency: php-zend-abi = 20050922 is needed by package php-pecl-memcached-1.0.0-1.el5.x86_64 (epel)
You could try using –skip-broken to work around the problem
You could try running: package-cleanup –problems
package-cleanup –dupes
rpm -Va –nofiles –nodigest
How do I fix it?
Hi Ivaldo,
Could you post output of following command:
Here is the results JR
[root@labs /etc/yum.repos.d]# rpm -qa php\* |sort
php-5.3.3-3.el6_2.6.x86_64
php-cli-5.3.3-3.el6_2.6.x86_64
php-common-5.3.3-3.el6_2.6.x86_64
php-pear-1.9.4-4.el6.noarch
php-pecl-memcache-3.0.5-3.el6.x86_64
I did every steps again, and now I got:
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
* remi: remi-mirror.dedipower.com
87 packages excluded due to repository priority protections
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package php-pecl-memcached.x86_64 0:2.0.1-1.el6.remi will be installed
–> Processing Dependency: php-pecl-igbinary(x86-64) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Processing Dependency: libmemcached.so.9()(64bit) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Running transaction check
—> Package php-pecl-igbinary.x86_64 0:1.1.1-3.el6.remi will be installed
—> Package php-pecl-memcached.x86_64 0:2.0.1-1.el6.remi will be installed
–> Processing Dependency: libmemcached.so.9()(64bit) for package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64
–> Finished Dependency Resolution
Error: Package: php-pecl-memcached-2.0.1-1.el6.remi.x86_64 (remi)
Requires: libmemcached.so.9()(64bit)
You could try using –skip-broken to work around the problem
You could try running: rpm -Va –nofiles –nodigest
and after rpm -qa php\* |sort
I got:
php-5.3.3-3.el6_2.6.x86_64
php-cli-5.3.3-3.el6_2.6.x86_64
php-common-5.3.3-3.el6_2.6.x86_64
php-pear-1.9.4-4.el6.noarch
php-pecl-memcache-3.0.5-3.el6.x86_64
You could try maybe update your PHP (to latest 5.3.10) from remi repo and then try to install it again?
Hi Jr,
Thanks for this post :)
Basically I installed and followed the instructions but when I try to access if class_exists from PHP it’s returning a false. (No info is visible either in phpinfo )
Here’s some data you that might help you:
[root@localhost /]# rpm -qa php\* |sort
php-5.1.6-32.el5
php-cli-5.1.6-32.el5
php-common-5.1.6-32.el5
php-devel-5.1.6-32.el5
php-pear-1.4.9-8.el5
php-pecl-memcache-2.2.6-1.el5
php-pecl-memcached-1.0.0-1.el5
The even stranger thing is that I installed PHP using xampp and it states it is the 5.3.8 even in phpinfo (I hope this makes sense – if not sorry but I’m new to this)
I also noticed you where asking for this before:
[root@localhost /]# yum repolist
Loaded plugins: fastestmirror, protectbase, security
Loading mirror speeds from cached hostfile
* base: centos.fastbull.org
* epel: http://ftp.ps.pl
* extras: centos.fastbull.org
* rpmforge: fr2.rpmfind.net
* updates: swissmirror.silyus.net
0 packages excluded due to repository protections
repo id repo name status
base CentOS-5 - Base 2,725
epel Extra Packages for Enterprise Linux 5 - i386 5,675
extras CentOS-5 - Extras 276
rpmforge RHEL 5 - RPMforge.net - dag 11,352
updates CentOS-5 - Updates 214
repolist: 20,242
Thanks in advance :)
Hi krs,
You have pecl-memcache(d) installed for PHP 5.1.6, but you run xampp installed PHP, so I recommend you to update/install your PHP to 5.3.10 version with memcache and memcached modules.
Thanks for your assistance :)
I followed the instructions there … (I had the rpms updated already but made sure they are updated once more).
Unfortunately same issue :(
Here’s something weird … I tried
php -r "print 'Exists:' . (int) class_exists('Memcache'). ' ';"and gave me a ’1′ which is great … but when I access the same command from a page it gives me a false …
Are you using same PHP on your web pages?
Could you check your php (-v|-i) on command line and phpinfo() output on web page? Are you using same PHP installation and do you see Memcache module?
bang!! You’re right … I had 2 installations of PHP …
Note for beginners: Do not use xampp installation if you need memcached.
Worked wonders JR :)
Rock n roll ;)
Excellent to hear that you got it working! :)
Hi JR,
I’ve succesfully installed memcached, I also enabled APC. Can I use memcached and APC together?
I have 2GB VPS with 3 virtual servers. And I use wordpress.
Do I still need the backend plugins for memcached and APC?
Currently I am also using WP Super Cache. So I have WP Super Cache as plugin, and APC + memcached as PHP extension.
Did I do is correct? Please help me.
Thanks,
dude !!! fantastic work man…
you save my time :)
i am not get any package, first installation successful, but next command say … no package with php-pecl-memcache availabel … what i do ?
Hi Anurag,
Could you post output of following commands: