Monday, April 1, 2013

Building Couchbase PHP driver on Ubuntu

I've been testing our PHP connector on Ubuntu lately, so I thought I should tell all of you how I'm doing this. I know a lot of people prefer to run Linux on their desktop, but I don't have any boxes running Linux at home. What I do have is a server running SmartOS, which makes it really easy to spin up new virtual machines with "any" operating system I'd like to test.

The first thing I do on my SmartOS server is to create a manifest file for my new vm. This manifest file is a JSON file describing the vm, and yesterday I was going to build on Ubuntu 12 so I created a file named ubuntu12.json looking like this:

{
   "brand": "kvm",
   "vcpus": 1,
   "autoboot": false,
   "alias": "ubuntu12",
   "ram": 1024,
   "resolvers": ["10.0.0.1"],
   "disks": [
      {
         "boot": true,
         "model": "virtio",
         "size": 10240
      }
   ],
   "nics": [
      {
         "nic_tag": "admin",
         "model": "virtio",
         "ip": "10.0.0.251",
         "netmask": "255.255.255.0",
         "gateway": "10.0.0.1"
      }
   ]
}

with that in place I created the virtual machine with the following command:

[root@smartos ~] # vmadm create -f ubuntu12.json
Successfully created 3d09ad09-e124-4b68-90fd-53c76f05dbc0

The next thing I did was to copy the Ubunto 12.10 installation iso file to /zones/3d09ad09-e124-4b68-90fd-53c76f05dbc0/root/cdrom.iso, before I executed the following command:

[root@smartos ~] # vmadm boot 3d09ad09-e124-4b68-90fd-53c76f05dbc0 order=cd,once=d cdrom=/cdrom.iso,ide

This boots the vm and provides a "console" to the machine over vnc. To figure out where to connect the vncviewer to complete the installation I executed:

root@smartos ~]# vmadm info 3d09ad09-e124-4b68-90fd-53c76f05dbc0 vnc
{
  "vnc": {
    "host": "10.0.0.22",
    "port": 39944,
    "display": 34044
  }
}

With my Ubuntu 12.10 installed the first thing I did was to install some extra packages:

trond@ubuntu12:~$ sudo wget -O/etc/apt/sources.list.d/couchbase.list http://packages.couchbase.com/ubuntu/couchbase-ubuntu1204.list
trond@ubuntu12:~$ wget -O- http://packages.couchbase.com/ubuntu/couchbase.key | sudo apt-key add -
trond@ubuntu12:~$ sudo apt-get update
trond@ubuntu12:~$ sudo apt-get install openssh-server php5-dev php5-cli libcouchbase2 libcouchbase2-libevent libcouchbase2-bin libcouchbase-dev
trond@ubuntu12:~$ wget -Orepo https://git-repo.googlecode.com/files/repo-1.19
trond@ubuntu12:~$ chmod a+x repo
trond@ubuntu12:~$ sudo cp repo /usr/bin

I can now build the php extension by running:

trond@ubuntu12:~$ mkdir compile
trond@ubuntu12:~$ cd compile
trond@ubuntu12:~/compile$ repo init -u git://github.com/trondn/manifests -m php.xml
trond@ubuntu12:~/compile$ repo sync
trond@ubuntu12:~/compile$ cd php
trond@ubuntu12:~/compile/php$ phpize
trond@ubuntu12:~/compile/php$ ./configure
trond@ubuntu12:~/compile/php$ make
trond@ubuntu12:~/compile/php$ cd tests
trond@ubuntu12:~/compile/php/tests$ cp couchbase.local.inc.dist couchbase.local.inc
trond@ubuntu12:~/compile/php/tests$ vi couchbase.local.inc

Now you should specify the hostname of your Couchbase cluster in COUCHBASE_CONFIG_HOST, and clear (or specify correct values) for COUCHBASE_CONFIG_USER and COUCHBASE_CONFIG_PASSWD).

trond@ubuntu12:~/compile/php/tests$ cd ..
trond@ubuntu12:~/compile/php$ make test

Depending on your cluster configuration you might encounter some test errors (the tests are unfortunately not written fully bullet proof). If you have a one-node cluster with a replica count set to 1, the following tests will fail (because they don't check for "inconsistent" configuration):

GetReplica - GetReplica [tests/phpt/GetReplica/GetReplica.phpt]
GetReplica - GetReplicaMulti [tests/phpt/GetReplica/GetReplicaMulti.phpt]

You can now install the driver by:

trond@ubuntu12:~/compile/php$ sudo cp modules/couchbase.so /usr/lib/php5/20100525
trond@ubuntu12:~/compile/php$ sudo cp example/couchbase.ini /etc/php5/cli/conf.d

And verify that it is properly installed by running:

trond@ubuntu12:~/compile/php$ php -i | grep couchbase
/etc/php5/cli/conf.d/couchbase.ini
couchbase
couchbase support => enabled
couchbase.compression_factor => 1.3 => 1.3
couchbase.compression_threshold => 2000 => 2000
couchbase.compressor => none => none
couchbase.durability_default_poll_interval => 100000 => 100000
couchbase.durability_default_timeout => 40000000 => 40000000
couchbase.instance.persistent => On => On
couchbase.restflush => On => On
couchbase.serializer => php => php
couchbase.view_timeout => 75 => 75

So let's go ahead and create a small test using our driver. Create the file test.php with the following content:

<?php
  try {
     $cb = new Couchbase("mycluster");
     print "Store foo: " . $cb->set("foo", "bar") . "\n";
     print "Get foo: " . $cb->get("foo") . "\n";
  } catch (CouchbaseException $e) {
     var_dump($e);
  }
?>

And test it with the following command:

trond@ubuntu12:~/compile/php$ php test.php
Store foo: 14566767818228433408
Get foo: bar

Happy hacking!!!

2 comments:

  1. hello Trond,
    i couldnt go durther in PHP extension install for couchbase.
    after repo init -u git://github.com/trondn/manifests -m php.xm
    there is no such php directory...

    ReplyDelete