Recently at work as well as in my own personal development I have found a lot of emphasis on caching technologies. Be this for keeping heavy database queries to a minimum or because you wish to keep heavily served files out of the disk I/O. In this post (my first post!!) I will demonstrate how to install and configure memcached on a linux (CentOS to be specific) box. As well how to install the memcache extension into PHP.

Installing Memcached

Make sure before you start tying to install memcache that you have gcc (# yum install gcc) and libevent-devel (# yum install libevent-devel) installed.

[root@nitrogen ~]# wget
[root@nitrogen ~]# tar -zxvf memcached-1.2.6.tar.gz
[root@nitrogen ~]# cd memcached-1.2.6
[root@nitrogen memcached-1.2.6]# ./configure
[root@nitrogen memcached-1.2.6]# make
[root@nitrogen memcached-1.2.6]# make install

Now if you are lazy like me and don’t want to have to remember what the memcached flags are every time you start it I would recommend using an init.d script. I recommend using the one located at To install the init.d script on your server you need to place the start-memcached perl script in /usr/local/bin/start-memcached. Make sure you have given this file chmod 755 and is chown root:root. Then you must place the memcached-init into /etc/init.d/memcached, make sure this script has the same permissions as the previous file. Finally create the configuration file /etc/memcached.conf, you can place any options that you want ran by the memcached binary in this file. I use the following in my file.

# /etc/memcached.conf
# user to run memcached as
-u apache

# we want to run memcached as a daemon

# amount of RAM we want to dedicate to memcached in MB
-m 512

# interface to listen to for memcached connections on

# port to listen for memcached connections on
-p 11211

Starting Memcached

Now you should be able to start and stop memcached quickly by using the following commands.

[root@nitrogen ~]# /sbin/service memcached start
[root@nitrogen ~]# /sbin/service memcached stop

If you did not install the init.d script then you can start memcached with the following command.

[root@nitrogen ~]# memcached -d -u apache -m 512 -l -p 11211

Installing the Memcache PHP extension

Installing the memcache PHP module from source is fairly straight forward, it is just like installing any other php module. Please note that you must have zlib-devel and php-devel packages installed.

[root@nitrogen ~]# wget
[root@nitrogen ~]# tar -zxvf memcache-3.0.2.tgz
[root@nitrogen ~]# cd memcache-3.0.2
[root@nitrogen memcache-3.0.2]# phpize
[root@nitrogen memcache-3.0.2]# ./configure
[root@nitrogen memcache-3.0.2]# make
[root@nitrogen memcache-3.0.2]# make install

Now we need to tell PHP to include our memcache extension when it starts up. We do this by putting the following configuration data in /etc/php.d/memcache.ini

; /etc/php.d/memcache.ini 
; include the memcache php extension

You will be able to verify that the memcache PHP extension has been installed by typing # php -m and looking for memcache in the php extension list.


  1. Good work! Thank you very much!
    I always wanted to write in my site something like that. Can I take part of your post to my site?
    Of course, I will add backlink?

    Regards, Timur Alhimenkov


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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