How to Install and Configure Memcache on CentOS (Quick PHP and Memcache Setup)

        This tutorial will guide you through installing the Memcache caching service on CentOS. Memcache is a high-performance, memory-based caching plugin compatible with PHP. It can cache not only variables and objects but also work alongside MySQL to cache database queries. Since Memcache stores data in memory, it delivers blazing-fast read and write speeds, providing high-speed caching for large volumes of rapidly changing dynamic data.

Because compiling and installing Memcache from source is relatively complex, this article will use the simpler approach of installing Memcache directly via yum on a CentOS system.

1. Since the default CentOS repositories do not include the memcache package, you need to import a third-party repository. Run the following two commands:

1 [root@www ~]# wget http://soft.bootf.com/rpm/epel-release-5-4.noarch.rpm
2 [root@www ~]# rpm -ivh epel-release-5-4.noarch.rpm

Note: Most tutorials online blindly suggest using the RPMForge repository for yum. However, after testing by VPS Management Encyclopedia, that repository does not contain the memcached package, so it will not work. Simply follow the repository and method provided by VPS Management Encyclopedia for a successful installation.

2. Check the installed repositories

01 [root@www ~]# yum repolist
02 Loaded plugins: fastestmirror
03 Loading mirror speeds from cached hostfile
04 * base: centos.ustc.edu.cn
05 * epel: mirrors.ustc.edu.cn
06 * extras: centos.ustc.edu.cn
07 * rpmforge: fr2.rpmfind.net
08 * updates: centos.ustc.edu.cn
09 repo id repo name status
10 base CentOS-5 - Base 2,705
11 epel Extra Packages for Enterprise Linux 5 - i386 5,579
12 extras CentOS-5 - Extras 282
13 updates CentOS-5 - Updates 455
14 repolist: 20,115

If you can see the epel repository listed, the installation was successful.

3. Install the Memcache server and PHP extension via yum

1 [root@www ~]# yum install memcached php-pecl-memcache

Both packages should now install successfully without any “not found” errors.

4. After installation, check if PHP has correctly loaded the memcache module:

1 [root@www ~]# php -m|grep memcache
2 memcache

If “memcache” is returned, it indicates the installation was successful.

5. Configure memcached to start automatically on boot

1 [root@www ~]# chkconfig --level 2345 memcached on

6. Start the memcached service and restart Apache

1 [root@www ~]# /etc/init.d/memcached start
2 Starting memcached: [OK]
3 [root@www ~]# /etc/init.d/httpd restart
4 Stopping httpd: [OK]
5 Starting httpd: [OK]

7. Test if PHP Memcache Support is Working

Create a file named memcache.php in the Apache document root:

1 vi memcache.php

Content as follows:

1 <?php
2 $memcache = new Memcache();
3 $memcache->connect('127.0.0.1', 11211);
4 $memcache->set('key', 'Memcache test successful!', 0, 60);
5 $result = $memcache->get('key');
6 unset($memcache);
7 echo $result;
8 ?>

If everything is normal, visiting this page should return “Memcache test successful”. At this point, Memcached and the PHP extension memcache have been successfully installed.

The default port for Memcached is 11211, so just use this port in PHP. Below is a handy method to clear all Memcached cache content:

Execute:

1 [root@www ~]# nc localhost 11211

Then input:

1 flush_all
2 quit

That’s it.

Source: http://www.bootf.com/442.html

Leave a Comment

Your email address will not be published.