How to Set Up Couchbase on a PHP Website

In my previous post Testing Couchbase as a NoSQL Server, I described my use of the NoSQL data store Couchbase. Today I’ll show you how to start using Couchbase with a PHP website. Note that because the Couchbase client PHP driver isn’t available for Windows, this can only be installed on a Linux system.

There are two versions, the stable 1.8 or the Beta version 2.0. For this article, I used the stable version 1.8.

Start by downloading and installing Couchbase. I had a cluster of two Windows and Linux PCs running, but found that this didn’t work well with the PHP driver. So if you want a cluster, make sure they’re all the same operating system and for PHP, make it Linux.

I used the latest Ubuntu 12:04 and set it up specifically with the 12:04 server and selected various servers, Apache and Samba. After that installed, I upgraded it to desktop with a couple of commands. One of the articles linked below explains the process nicely.

Use your browser and log into your server at http://localhost:8091/index.html to access the Couchbase administration page. The first time you do, you can specify memory size, type of default bucket and whether replication is enabled. You only need it if you have two or more servers. For our purposes 512MB RAM is probably fine.

The default bucket (it’s like a database in a relational database system such as MySQL) should be Couchbase. That persists objects to disk and can handle objects up to 20 MB. The other type (memcached) is for more transient data that doesn’t need persisting. It can only deal with objects up to 1 MB in size.

Follow the install instructions linked below for the Couchbase client. It took me an hour, because first you have to install the C client that the PHP client needs. Then install the PHP client. There isn’t a 12:04 Ubuntu PHP client yet, but the 11:04 worked fine for me. I untarred the tar file and extracted couchbase.so, which is an Apache .so file (the equivalent of a Windows dll) that must be copied somewhere.

On my Ubuntu 12:04 PC I copied it from the download folder into /usr/lib/php5 with this terminal command.

sudo cp couchbase.so /usr/lib/php5

Then you have to edit the php.ini file. Finding php.ini can be a bit challenging as there’s often one or two on your system. I created a one-line phpinfo.php file like this to show phpinfo(). Browsing to localhost/phpinfo.php told me where the loaded php.ini was located.

<?php phpinfo();?>

I used gedit to edit it and add the path to couchbase.so.

sudo gedit php.ini

If you don’t have gedit installed, use Nano, and add this line to php.ini and save it.

extension=/usr/lib/php/20090926/couchbase.so

Next restart Apache. Although you can do this easily enough from the command line, I took an easier way out and installed an excellent open source Admin tool called Webmin. On its “Servers” tab, you can get to Apache and restart it.

With everything installed, follow the verify instructions (linked below) to show it’s working, or use my version, which is similar: Create a php file, say test.php. Copy this below into test.php and then browse to localhost/index.php.

<?php
$cb = new Couchbase("127.0.0.1:8091");
$cb->set("a", 10);
echo $cb->get("a");
?>

It should output 10, proving that Couchbase is working. But that’s just the start. In the next article, I’ll show you what Couchbase can do.

Related Links

Post a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>