How To Install memcached with memcache PHP Extension on CentOS 5.x

In this how to i will describe how to install memcached on your server with PHP Extension.  memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. My server is running CentOS 5.3 with CPanel.

1) Install memcached.

Enable rpmforge respository to install latest memcached rpm using yum.

For i386 / i686

rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm

For x86_64

rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS//rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

Use yum to install memcached

yum -y install memcached

As soon as memcached installation completed, edit options for memcached in /etc/sysconfig/memcached to meet your need.

vi /etc/sysconfig/memcached

PORT=”11211″                #define on which port to urn
USER=”nobody”           #same as apache user
MAXCONN=”1024″     #maximum number of connections allowed
CACHESIZE=”64″         #memory used for caching
OPTIONS=””                   #use for any custom options

Save the file. All options can be seen by using following command

memcached -h

Start memcached

/etc/init.d/memcached start
Starting Distributed memory caching (memcached):           [  OK  ]

to check the running status of memcached

/etc/init.d/memcached status
memcached (pid 6475) is running…

and

netstat -anp | grep 11211
tcp  0  0 :::11211    :::*  LISTEN      6475/memcached
udp 0  0 0.0.0.0:11211  0.0.0.0:* 6475/memcached

2) Install PHP Extension.

Download and install latest stable memcache version from PECL.

cd /usr/src
wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
phpize
./configure
make
make install

memcache.so will be install in php modules directory, now enable memcache.so extension in php.ini

To find out your php.ini location, execute following command

php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini

vi /usr/local/lib/php.ini

extension = “memcache.so”

save the file and restart httpd server.

/etc/init.d/httpd restart

To check is memcache extension loaded in php, execute following command.

php -i | grep memcache
memcache
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 8192 => 8192
memcache.default_port => 11211 => 11211
memcache.default_timeout_ms => 1000 => 1000
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => standard => standard
memcache.max_failover_attempts => 20 => 20
Registered save handlers => files user sqlite memcache
PWD => /usr/src/memcache-2.2.5
_SERVER[“PWD”] => /usr/src/memcache-2.2.5
_ENV[“PWD”] => /usr/src/memcache-2.2.5

This information can also be seen using phpinfo().

If you have question please comment.

Related Posts

36 thoughts on “How To Install memcached with memcache PHP Extension on CentOS 5.x

  1. @Islam: Which Linux Version you are using. You said it doesn’t work and in mean time you said when you compile php with libcurl it broke memcached?
    ————
    Regards,

  2. @Souhail: it’s a centos, kernel 2.6.18 ..
    there is no relationship between libcurl and memcached, the only thing is that after I recompiled PHP, libcurl worked, and memcached not, I tried many times with too versions of the package and nothing! PHP is version 5.3.2

  3. @Islam: I knew there is no relationship between libcurl and memcached. All you have to do is recompile PHP and then reinstall memcached using the source because it uses phpize to determine the PHP version, so you need to reinstall memcached everytime you recompile PHP.
    It will be good if you posted any error which you facing while installing memcached
    ——————–

  4. Thank you Sohail, I got it working now.
    I was phpizing against the wrong version of php, .. another thing is that extension = “memcache.so” doesn’t work with the ” ” , maybe you should fix that in the tutorial.
    thank you very much for your help.

  5. The check step could be done before starting apache.
    And if you are using drupal, dont forget to set:

    memcache.hash_strategy = “consistent”

    for drupal memcache module.

  6. Had these errors when ./configure

    checking for the location of ZLIB… no
    checking for the location of zlib… configure: error: memcache support requires ZLIB. Use –with-zlib-dir= to specify prefix where ZLIB include and library are located

    did this :
    ./configure –with-zlib-dir=/usr/include

    worked fine .

    when i typed make …
    /usr/src/memcache-2.2.5/memcache.c:34:18: error: zlib.h: No such file or directory
    /usr/src/memcache-2.2.5/memcache.c: In function ‘php_memcache_init_globals’:
    /usr/src/memcache-2.2.5/memcache.c:255: error: ‘Z_DEFAULT_COMPRESSION’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c:255: error: (Each undeclared identifier is reported only once
    /usr/src/memcache-2.2.5/memcache.c:255: error: for each function it appears in.)
    /usr/src/memcache-2.2.5/memcache.c: In function ‘mmc_compress’:
    /usr/src/memcache-2.2.5/memcache.c:839: error: ‘Z_OK’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c:846: error: ‘Z_MEM_ERROR’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c:849: error: ‘Z_BUF_ERROR’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c:852: error: ‘Z_STREAM_ERROR’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c: In function ‘mmc_uncompress’:
    /usr/src/memcache-2.2.5/memcache.c:876: error: ‘Z_BUF_ERROR’ undeclared (first use in this function)
    /usr/src/memcache-2.2.5/memcache.c:878: error: ‘Z_OK’ undeclared (first use in this function)

    any clues ?

  7. didn’t work on centos 5.5

    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

  8. root@v-88 [/home]# yum -y install memcached
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * addons: http://ftp.lug.udel.edu
    * base: hpc.arc.georgetown.edu
    * extras: mirror.batblue.com
    * rpmforge: fr2.rpmfind.net
    * updates: mirror.cs.vt.edu
    addons | 951 B 00:00
    base | 2.1 kB 00:00
    extras | 2.1 kB 00:00
    rpmforge | 1.1 kB 00:00
    updates | 1.9 kB 00:00
    Excluding Packages in global exclude list
    Finished
    Setting up Install Process
    Resolving Dependencies
    –> Running transaction check
    —> Package memcached.i386 0:1.4.5-1.el5.rf set to be updated
    –> Processing Dependency: perl(AnyEvent) for package: memcached
    –> Processing Dependency: perl(AnyEvent::Socket) for package: memcached
    –> Processing Dependency: perl(AnyEvent::Handle) for package: memcached
    –> Processing Dependency: libevent-1.1a.so.1 for package: memcached
    –> Processing Dependency: perl(YAML) for package: memcached
    –> Processing Dependency: perl(Term::ReadKey) for package: memcached
    –> Running transaction check
    —> Package compat-libevent-11a.i386 0:3.2.1-1.el5.rf set to be updated
    —> Package memcached.i386 0:1.4.5-1.el5.rf set to be updated
    –> Processing Dependency: perl(AnyEvent) for package: memcached
    –> Processing Dependency: perl(AnyEvent::Socket) for package: memcached
    –> Processing Dependency: perl(AnyEvent::Handle) for package: memcached
    –> Processing Dependency: perl(YAML) for package: memcached
    –> Processing Dependency: perl(Term::ReadKey) for package: memcached
    –> Finished Dependency Resolution
    memcached-1.4.5-1.el5.rf.i386 from rpmforge has depsolving problems
    –> Missing Dependency: perl(YAML) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    memcached-1.4.5-1.el5.rf.i386 from rpmforge has depsolving problems
    –> Missing Dependency: perl(Term::ReadKey) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    memcached-1.4.5-1.el5.rf.i386 from rpmforge has depsolving problems
    –> Missing Dependency: perl(AnyEvent::Socket) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    memcached-1.4.5-1.el5.rf.i386 from rpmforge has depsolving problems
    –> Missing Dependency: perl(AnyEvent::Handle) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    memcached-1.4.5-1.el5.rf.i386 from rpmforge has depsolving problems
    –> Missing Dependency: perl(AnyEvent) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    Error: Missing Dependency: perl(Term::ReadKey) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    Error: Missing Dependency: perl(AnyEvent) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    Error: Missing Dependency: perl(YAML) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    Error: Missing Dependency: perl(AnyEvent::Handle) is needed by package memcached-1.4.5-1.el5.rf.i386 (rpmforge)
    Error: Missing Dependency: perl(AnyEvent::Socket) is needed by package memcached-1.4.5-1.el5.rf.i386 (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
    The program package-cleanup is found in the yum-utils package.

    how to fix this

  9. Hi,
    I get this conflict message when I run the command

    $rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

    Retrieving http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
    Preparing… ########################################### [100%]
    file /etc/smart/channels/rpmforge.channel from install of rpmforge-release-0.3.6-1.el5.rf.x86_64 conflicts with file from package rpmforge-release-0.3.6-1.el5.rf.i386
    file /etc/sysconfig/rhn/sources.rpmforge.txt from install of rpmforge-release-0.3.6-1.el5.rf.x86_64 conflicts with file from package rpmforge-release-0.3.6-1.el5.rf.i386

    Any reason why I do not see the expected output?

  10. Hi

    I followed the tut, and everything worked fine until i got to check is memcache extension loaded in php then i get this error :

    [root@s******** memcache-2.2.5]# php -i | grep memcache
    Failed loading /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so: /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so: cannot open shared object file: No such file or directory
    PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/memcache.so’ – /usr/lib64/php/modules/memcache.so: cannot open shared object file: No such file or directory in Unknown on line 0
    PWD => /usr/src/memcache-2.2.5
    _SERVER[“PWD”] => /usr/src/memcache-2.2.5
    _ENV[“PWD”] => /usr/src/memcache-2.2.5

    I think the first to lines referencing xcache are from a deleted attempt to install xcache.
    the memcache.so file is where it says it is.
    all that is in the file is : []ELF-

    Has anyone got an idea what has gone wrong please

  11. Hello,

    I have a LAMP(Linux, apache, mysql, php) server i want to use memcahe for better performance but my web server and db server are on different machine and i don’t know on which machine i should install memcache and php-memcache rpm ?

  12. @you need to install memcached on both your web and db server. Then compile php with memcache module on web server only. Make your memcached from db server accessiable and configure your php to access it as memcached using IP address/port of db server. We have done this with individual memcached servers as middle server between web and db server.

    ——–
    Regards,

  13. Pingback: Install | djfueese

Leave a Reply

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