Install the Couchbase PHP Client Connector for Centos 6.4

Introduction

If you need to connect to a Couchbase server using PHP you will need the PHP connector client. Now following the instructions here will get you some of the way but not all of it!

This guide does assume you have apache (httpd) and php installed as it relies on the php module and it’s associated file structure being in place.

Create a Couchbase Yum repo

From the command line use wget to fetch the repo definition file


wget -O /etc/yum.repos.d/Couchbase.repo http://packages.couchbase.com/rpm/couchbase-centos62-x86_64.repo

Install the Couchbase Libraries

Lets install the client C Library – we do need this even though we are going to be developing in PHP.

yum install -y libcouchbase2 libcouchbase-devel

Now let’s get the actual php module file for apache

Using wget again download it – in my example I am downloading it to roots home directory (It’s bad practise to stuff as root but for this example I am sure we can let it slide)


cd /root
wget http://packages.couchbase.com/clients/php/php-ext-couchbase-1.1.2-centos62-x86_64.tar.gz

untar it


tar -xvf php-ext-couchbase-1.1.2-centos62-x86_64.tar.gz

Now copy the file it unzipped to your php modules directory


cp php-ext-couchbase/couchbase.so /usr/lib64/php/module

Edit the php.ini

This is done in vi or through using a simple echo statement to append the entries to the file


echo "extension=json.so" >> /etc/php.ini
echo "extension=couchbase.so" >> /etc/php.ini

Restart Apache


service http restart

Check for errors

Have a look in the apache error_log for errors to see if it started okay

some thing like this is what you are looking for:


[Fri Jan 17 13:59:57 2014] [notice] caught SIGTERM, shutting down
[Fri Jan 17 13:59:57 2014] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Fri Jan 17 13:59:57 2014] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Fri Jan 17 13:59:57 2014] [notice] Digest: generating secret for digest authentication ...
[Fri Jan 17 13:59:57 2014] [notice] Digest: done
[Fri Jan 17 13:59:57 2014] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 configured -- resuming normal operations

Ready to start working with php and couchbase

That’s it you are now ready to go!

Here’s a simple example to help you get connecting to Couch with PHP

I am going to open an rss feed and read the items in as couchbase documents. The feed is a BBC news site

http://feeds.bbci.co.uk/news/world/rss.xml

If all goes well you should see a ton of documents in couchbase

<?php
 
class CouchbaseHelper
{
    private $cb;
 
	//Update to use parameters for couchbase constructor..
    public function __construct()
	{
    $this->cb = new Couchbase("couchbaseserverIP:8091", "bigbucket", "passw0rd", "bigbucket"); 
	}
 
	public function set($id, $arr)
	{
		    return $this->cb->set($id, json_encode($arr));
	} 
 
	public function add($id, $arr)
	{
	 	    return $this->cb->add($id, json_encode($arr));
	} 
 
	public function get($id)
	{
    return (array)json_decode($this->cb->get($id));
	}
}
 
 
 
//Open an RSS Feed and store it
$rss_feed = "http://feeds.bbci.co.uk/news/world/rss.xml";
$feed_to_array = simplexml_load_file($rss_feed);
 
 
$cb = new Couchbasehelper(); 
 
//grab elements of each item we want from the feed and store in array ready for couchbase
foreach($feed_to_array->channel->item as $item) {
  $doc_id = uniqid();  // we can't use the rss items unique id as it is a url so let's make one up with php uniqid function
  $doc_id = strval($doc_id); // make sure it's a string or the couchbase won't like it much
 
  $not_json = array(
				'guid' => (string)$item->guid,
				'title' => (string)$item->title ,
				'description' => (string)$item->description ,
				'link' => (string)$item->link ,
				);
 
 echo "<br><br>";
 print_r ($not_json);
 echo "<br><br>";
 
 //write to couchbase
 $cb->add($doc_id, $not_json);
 
 
}
 
 
?>

Here is the finished result displayed for us in the Couchbase Console – lots of documents created by our php handywork.

couchbase-full-of-bbc-news

Optionally turn off this repo

I don’t like leaving 3rd party repos attached to my server in enabled mode. So I turn them off after use – you can always turn them back on if/when you need it again.

Edit the repo file and add the line enabled=0

by opening the file in vi


vi /etc/yum.repos.d/Couchbase.repo

and adding the line enabled=0 to the bottom
like so

[couchbase]
name = Couchbase package repository
baseurl = http://packages.couchbase.com/rpm/6.2/x86_64
gpgcheck = 1
gpgkey = http://packages.couchbase.com/rpm/couchbase-rpm.key
enabled=0

Comments are closed.