“MemCached” is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
If you are on a cPanel based server, you can install the PHP Extension by running the script /scrips/easyapache and enable memcache during the process. Once completed, you need to install memcached daemon by following the step 1 listed below. If you are not on cPanel based server follow the two steps listed below.
1. Steps to Install memcached
You have to activate the RPMForge custom repository to install latest memcached rpm.
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
or
rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
You can now use yum to install memcached
yum -y install memcached
Once the installation is completed, you will be able to locate the config file at the location /etc/sysconfig/memcached . Edit the file according to your requirement. It will be something like below.
PORT=”11211″ #define on which port to urn
USER=”nobody” #same as apache user
MAXCONN=”2048″ #maximum number of connections allowed
CACHESIZE=”32″ #memory used for caching
OPTIONS=”” #use for any custom options
Always specify a user for memcached when you start it from the shell. You can use nobody user or a user named memcached.
Now you can test it with the following command.
memcached -u memcached -d -m 1048 -l 127.0.0.1 -p 11211
or
memcached -d -u nobody -m 1048 -p 11211 127.0.0.1
Check if memcached is listening on port 11211
netstat -plan | grep “:11211″
Done.
Now you can go to step 2 to perform the installation of php extension.
2. Steps to Install PHP Memcache
Download and install the latest version of PHP extension from PECL.
cd /usr/share
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -xvf memcache-2.2.7.tgz
cd memcache-2.2.7.tgz
phpize
./configure
make
make install
After successful installation check if memcache.so is added to php.ini. If its not there add it manually to php.ini, restart apache and create a phpinfo page and check for memcache.
To locate php.ini
php -i | grep php.ini
vi /usr/local/lib/php.ini
Add the following
extension=”memcache.so”
Restart httpd
/etc/rc.d/init.d/httpd restart
Check if its loaded in command line by using the following.
php -i | grep memcache
It will show something like below.
memcache
memcache support => enabled
You can also test memcache by creating memcachetest.php file with the following code and see the results.
<?php
//memcached simple test
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
$key = md5(’42data’); //something unique
for ($k=0; $k<5; $k++) {
$data = $memcache->get($key);
if ($data == NULL) {
$data = array();
//generate an array of random shit
echo “expensive query”;
for ($i=0; $i<100; $i++) {
for ($j=0; $j<10; $j++) {
$data[$i][$j] = 42; //who cares
}
}
$memcache->set($key,$data,0,3600);
} else {
echo “cached”;
}
}
If you are on a cPanel based server, you can install the PHP Extension by running the script /scrips/easyapache and enable memcache during the process. Once completed, you need to install memcached daemon by following the step 1 listed below. If you are not on cPanel based server follow the two steps listed below.
1. Steps to Install memcached
You have to activate the RPMForge custom repository to install latest memcached rpm.
rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
or
rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
You can now use yum to install memcached
yum -y install memcached
Once the installation is completed, you will be able to locate the config file at the location /etc/sysconfig/memcached . Edit the file according to your requirement. It will be something like below.
PORT=”11211″ #define on which port to urn
USER=”nobody” #same as apache user
MAXCONN=”2048″ #maximum number of connections allowed
CACHESIZE=”32″ #memory used for caching
OPTIONS=”” #use for any custom options
Always specify a user for memcached when you start it from the shell. You can use nobody user or a user named memcached.
Now you can test it with the following command.
memcached -u memcached -d -m 1048 -l 127.0.0.1 -p 11211
or
memcached -d -u nobody -m 1048 -p 11211 127.0.0.1
Check if memcached is listening on port 11211
netstat -plan | grep “:11211″
Done.
Now you can go to step 2 to perform the installation of php extension.
2. Steps to Install PHP Memcache
Download and install the latest version of PHP extension from PECL.
cd /usr/share
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -xvf memcache-2.2.7.tgz
cd memcache-2.2.7.tgz
phpize
./configure
make
make install
After successful installation check if memcache.so is added to php.ini. If its not there add it manually to php.ini, restart apache and create a phpinfo page and check for memcache.
To locate php.ini
php -i | grep php.ini
vi /usr/local/lib/php.ini
Add the following
extension=”memcache.so”
Restart httpd
/etc/rc.d/init.d/httpd restart
Check if its loaded in command line by using the following.
php -i | grep memcache
It will show something like below.
memcache
memcache support => enabled
You can also test memcache by creating memcachetest.php file with the following code and see the results.
<?php
//memcached simple test
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
$key = md5(’42data’); //something unique
for ($k=0; $k<5; $k++) {
$data = $memcache->get($key);
if ($data == NULL) {
$data = array();
//generate an array of random shit
echo “expensive query”;
for ($i=0; $i<100; $i++) {
for ($j=0; $j<10; $j++) {
$data[$i][$j] = 42; //who cares
}
}
$memcache->set($key,$data,0,3600);
} else {
echo “cached”;
}
}
No comments:
Post a Comment