How To Install and Testing Memcache PHP Extension

I assume you have memcached daemon installed and running in your server.
If you still don't have memcached daemon running on your server, you need to install memcached daemon first.

You can follow this link for Installing Memcached daemon
[code]http://wowtutorial.org/en/tutorial/63.html [/code]

In order to connect to memcached daemon with PHP extension,
First we need to reconfigure PHP to support memcache extension

Reconfigure PHP

Login into your server with root access
Download PHP-Memcache package and install php memcache extension
[code]$wget http://pecl.php.net/get/memcache-3.0.0.tgz
$tar -zxvf memcache-3.0.0.tgz
$cd memcache-3.0.0
$phpize && ./configure --enable-memcache && make[/code]

If your PHP is binary version you can also compile memcache from apt-get or yum

Install from YUM DAG repository
[code]$wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-*.rpm
$rpm --install rpmforge-release-*.rpm
$yum install --enablerepo=rpmforge memcached[/code]

Install from apt-get Ubuntu / Debian
[code]$apt-get install php5-memcache[/code]

It will automatically modify your php.ini
To make sure your php.ini already have memcache extension.

Edit php.ini file, example your php.ini is in /etc/ please specify your php.ini path
[code]$vi /etc/php.ini

... ## make sure you php.ini have this line below
extension=memcache.so[/code]

Restart your webserver
[code]#/etc/init.d/apache2 restart or /etc/init.d/httpd restart[/code]

You need to make sure you php already have memcache extension on it.
you can take a look from test.php

Create file test.php

[code]$vi /home/www/public_html/test.php
<? phpinfo(); ?>[/code]

Execute test.php from your browser such as IE / Firefox
[code]http://athena.wowtutorial.org/test.php[/code]

If your php already support memcache, phpinfo will have memcache extension like below

Test Connection between memcached daemon and php script

Next step is to test a php file that contain a script to connect it to memcache daemon.
We need to create a memcache-test.php file
[code]$vi memcache-test.php

<?php
/* procedural API */

/* connect to memcached server */
$memcache_obj = memcache_connect('localhost', 11211);

/*
set value of item with key 'var_key'
using 0 as flag value, compression is not used
expire time is 30 seconds
*/
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);

echo memcache_get($memcache_obj, 'var_key');

?>[/code]

We can try to access this file from browser
[code]http://athena.wowtutorial.org/memcache-test.php[/code]

It will show a text "some variable" into your web browser like below

If your memcached daemon is not running it will shows like below

Warning: memcache_connect() [function.memcache-connect]: Can't connect to localhost:11211, Connection refused (111) in ......

 

 

 

 

Facebook Fans