Posts Tagged ‘memcache’

Posted by 6bytes at 16, March, 2010

Category: PHP

Tags: , ,

In last two posts I described how to install memcached daemon and memcache extension for PHP. Today I’ll show you how to configure your PHP to use memcache to handle sessions.

One thing I should mention is that when installing memcache you should answer yes when asked

1. Enable memcache session handler support? : yes

First of all lets start two memcached processes just in case one crashes, we’ll have a second one ready right away.

memcached -u root -d -m 512 -l 127.0.0.1 -p 11211
memcached -u root -d -m 512 -l 127.0.0.1 -p 11212

Worth mentioning is that when one memcached process crashes session information will not be transferred to another. New session will be started.

Next edit your php.ini file. Comment the line with your current session handler setting

; session.save_handler = files

and add two new lines

session.save_handler = memcache
session.save_path = "tcp://localhost:11211, tcp://localhost:11212"

If for any reason you don’t want to edit php.ini file you can set those options directly in your PHP script.

ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://localhost:11211, tcp://localhost:11212');

Restart your web server

service httpd restart

For full list of runtime configuration options go here.

Posted by 6bytes at 13, March, 2010

Category: CentOS, Linux, PHP

Tags: , , ,

After installing memcached daemon we have to install memcache PHP extension.

yum install php-pecl-memcache

or

apt-get install php5-memcache

If above is not an option try below method.

pecl install memcache

After successful installation add memcache.so extension to your php.ini file.

vim /etc/php.ini

Add this line:

extension=memcache.so

Restart web server

service httpd restart

Test out our installation.

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ('Can\'t connect!');
$version = $memcache->getVersion();
echo 'Server version: '.$version;

You should see your server’s version number. If you see a blank page make sure that memcached daemon is working. If you’re getting “Can’t connect!” message despite running daemon try changing ‘localhost’ to ‘127.0.0.1’.