Memcached

From DreamHost
Jump to: navigation, search
The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this.

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. Many web applications (MediaWiki, Drupal, etc) offer support for memcached.

Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.

memcached is not available for non-PS accounts.

Installing the memcached server daemon

(This is the stuff that actually caches to RAM)

To install memcached for PHP you will need to login to your machine via SSH with your admin user. You can then run the following command to change to the root user.

sudo su

You can now install the memcached daemon (server-based) package from the Debian repository.

apt-get install memcached

After installing this you will need to download a client package to access the server and extend PHP to connect to your memcached daemon. Unfortunately, there are two such packages, the first named "memcache" and the second package (arguably better written but poorly named) is "memcached".

To use the PECL memcached client

see memcached documentation at php.net

We first need the prerequisite libmemcache library. *NOTE*: libmemcache requires the memcached daemon to be already installed!

#libmemcache - see https://launchpad.net/libmemcached
wget http://download.tangent.org/libmemcached-0.53.tar.gz
tar -zxvf libmemcached-0.53.tar.gz
cd libmemcached-0.53
./configure
make
make install

Next the memcached client

#PECL memcached
wget http://pecl.php.net/get/memcached-2.0.1.tgz
tar -zxvf memcached-2.0.1.tgz
cd memcached-2.0.1
# default PATH_TO_YOUR_PHP_INSTALL is /usr/local/php5 for PHP5.2, or /usr/local/php53 for PHP5.3 if installed using the Dreamhost control panel. Other custom installations may be elsewhere.
/PATH_TO_YOUR_PHP_INSTALL/bin/phpize
./configure --with-php-config=/PATH_TO_YOUR_PHP_INSTALL/bin/php-config
make
make install

The end of the status after the "make install" will show 'Installing shared extensions' - make note of this directory. (If you had already specified an extensions directory in your php.ini, it should be that directory. Edit your php.ini file and add the following at the end of the file:

extension="/PATH_TO_SHARED_EXTENSIONS/memcached.so"

Repeat as necessary for any additional custom PHP installations. (You only need the daemon once, but each install of PHP needs the client extension if you want to connect using the client.)

Run phpinfo() to check if memcached daemon is running.

Alternately, To use the PECL memcache client, download the source code and compile it:

#PECL memcache
wget http://pecl.php.net/get/memcache-2.2.6.tgz
tar -zxvf memcache-2.2.6.tgz
cd memcache-2.2.6

#Compile for PHP 5.2
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make
make install

#Compile for PHP 5.3
/usr/local/php53/bin/phpize
./configure --with-php-config=/usr/local/php53/bin/php-config
make
make install

Now that that shared object is compiled, you can add it to your PHP configuration.

NOTE: You'll get this message: "make: *** No rule to make target `clean'. Stop."

#PHP 5.2 configuration
mkdir -p /etc/php5/cgi/ini.d/
echo "extension=\"/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/memcache.so\"" > /etc/php5/cgi/ini.d/memcache.ini
make clean
#PHP 5.3 configuration
mkdir -p /etc/php53/conf.d/
echo "extension=\"/usr/local/php53/lib/php/extensions/no-debug-non-zts-20090626/memcache.so\"" > /etc/php53/conf.d/memcache.ini

You can check your PHP configuration via the command line by running

/usr/local/bin/php -i | grep memcache