CentOS Memcache Installation and Configuration Guide (Quick Setup with PHP and Memcache)

        This tutorial will guide you through installing the Memcache caching service on CentOS. Memcache is an in-memory caching plugin compatible with PHP. It can not only cache objects like variables but also work with MySQL to cache data queries. Because Memcache stores data in memory, its read and write speeds are extremely fast, providing high-speed caching for large, rapidly changing dynamic data.

Since compiling and installing Memcache involves relatively complex steps, this article will use the direct yum installation method on CentOS as an example, which is quick and simple.

1. Since the default CentOS repositories do not contain a memcache installation package, you need to import a third-party repository. Execute 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 online resources parrot the advice to use the RPMForge repository with yum. However, through actual testing by the VPS Management Encyclopedia, this repository does not contain the memcached package, so it cannot be installed normally. Follow the repository and method provided by the VPS Management Encyclopedia for 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

Finding the epel package indicates a successful installation.

3. Install Memcache server and PHP extension with yum

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

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

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

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

Returning “memcache” indicates it has been installed.

5. Set the memcached service 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 supports Memcache normally

Create a memcache.php file in the Apache website root directory

1 vi memcache.php

The content is 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 memcache extension have been successfully installed.

The default port for Memcached is 11211, so use this port in your PHP code. Below is a convenient method to clear all cache content from memcache:

Execute:

1 [root@www ~]# nc localhost 11211

Then enter:

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.