<< there's no place like 127.0.0.1

This guide will help you install PHP Memcache on a CentOS server.

What is PHP memcache?

In my previous article, I showed you how to install Memcached, the service daemon. Now, if you would like your PHP software to interface with that daemon, you will want to install a PHP extension for it.

There are actually two separate implementations of a PHP Client that wraps the memcached (daemon); both are provided via the PECL library. One is called memcache and the other is called memcached. I know that it is a little confusing that ‘memcached’ shares the same name as the daemon itself, but it is a separate PHP wrapper.

This article will focus on installing ‘memcache’ as it is the most commonly used. You can use the information to install ‘memcached’ as well; the installation steps are the same. The main difference is that ‘memcached’ requires the libMemcached library. You can see a comparison of the two different PHP clients here: http://code.google.com/p/memcached/wiki/PHPClientComparison

Installation, the quick method

The easiest method is to simply use PECL’s install command. This will grab the latest stable release, configure it with the default options, and add it to the server’s php.ini:

pecl install memcache  

Note: Un-installation is just as easy -

pecl uninstall memcache  

Installation, the manual method

The quick ‘install’ method uses the default configuration options and should serve most peoples’ purposes. However, if you need more control over the installation options, you can manually install from source and add it flags as you need.

Download the source package via PECL:

    cd /usr/local/src/  
    pecl download memcache  

Unpack the tar and enter into the newly extracted directory (be sure to replace the * with your downloaded version):

    tar -xvzf memcache-*.tgz  
    cd memcache-*  

Configure and install:

    phpize  
    ./configure && make && make install  

Verify the install

Verify the installation by displaying all the installed PHP modules. See if ‘memcache’ is listed:

php -m  
memcache  

Problems with manual installation

The make install should load the module into your server’s extension directory automatically. However, if you do not see memcache listed in a php -m, you will need to add the module manually

First, you’ll want to very the location of your server’s extensions directory:

grep extension_dir /usr/local/lib/php.ini  

It should return something similar to the following:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"  

Next, copy the memcache module into that directory (be sure to replace the path with the one found on your server):

cp modules/memcache.so /usr/local/lib/php/extensions/no-debug-non-zts-20060613  

Finally, add the module to your php.ini:

echo 'extension=memcache.so' >> /usr/local/lib/php.ini  

Thoughts

It used to be a common issue that the quick ‘install’ method could not be used for servers where the /tmp partition was mounted ‘noexec’. The problem was that the ‘configure’ process could not execute because the PECL client downloaded the module into in /tmp. When /tmp is mounted ‘noexec’, servers cannot execute scripts from /tmp.

However, PECL now downloads and executes the script from /root/tmp. No need to worry about compiler errors with /tmp. You can use the ‘install’ method even with a secured /tmp.

Note about this article

This article is one I had written and shared with the ServInt blog as part of the ‘Tech bench’ series. You can view it on the ServInt blog here. They are using my article with my permission.

Running Gitit Wiki with Upstart

I know I've been rather quiet lately. I've been busy travelling for a little bit and found myself at a new job. Well, now I'm getting bac...… Continue reading

Installing ImageMagick & PHP Imagick

Published on March 26, 2015