This guide will help you install PHP Memcache on a CentOS server.
In my last ServInt University 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 it’s a little confusing that memcached shares the same name as the daemon itself, but it is a separate PHP wrapper.
In this article, I’ll focus on installing memcache as it is the more commonly used of the two. You can use the information to install memcached as well; the installation steps are the same. The main difference is that the memcached PHP extension requires the libMemcached library. You can see a comparison of the two different PHP clients here.
Installation, the quick method
The easiest installation method for memcache is to 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:
/usr/local/bin/pecl install memcache
Un-installation is just as easy:
/usr/local/bin/pecl uninstall memcache
Installation, the manual method
The quick install method for memcache uses the default configuration options and should serve most users’ 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/bin/ pecl download memcache
- Unpack the tar and enter into the newly extracted directory (be sure to replace the X’s with your downloaded version):
tar -xvzf memcache-X.X.X.tgz cd memcache-X.X.X
- Configure and install:
/usr/local/bin/phpize ./configure --with-php-config=/usr/local/bin/php-config
Note: If you are working on a server with cPanel, keep in mind that cPanel itself installs two versions of php:
For that reason you have to know which version your web server is actually using. Most of the time it is /usr/local/bin/php. It can get very confusing and can be different from server to server. If you are a ServInt customer, don’t hesitate to fill out a support ticket if you have any questions.
- 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, verify 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
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.