NoSQL Zone is brought to you in partnership with:

Don Pinto is a Product Marketing Manager with experience in cloud and database technologies. Don is a DZone MVB and is not an employee of DZone and has posted 80 posts at DZone. You can read more from them at their website. View Full User Profile

Easy PHP-Couchbase Setup for Mac with Bitnami

01.29.2013
| 2948 views |
  • submit to reddit
Curator's Note: The content of this article was originally written by Jasdeep Jaitla over at the Couchbase blog.

Setting up PHP can be a bit of a pain on both Windows and Mac OS X. There are a lot of different ways of doing it, but many of them come with a variety of annoyances or confusion. Additionally, on Mac you've got Apache and PHP 5.3 already on your system, but modifying it can be non-intuitive as well.

Using Bitnami has been by far the easiest and most straightforward install I've come across.

Getting Setup on Mac OS X with Bitnami

My friend @AseDeliri on Twitter pointed me to Bitnami a few weeks ago and I am extremely happy he did. It has made setup of PHP a breeze, and keeps everything together in a single place much like Python's virtualenv and RVM's handling of different Ruby versions and gemsets.

First, Pick a Bitnami stack with Apache & PHP

I chose the Bitnami MAPP (Mac Apache PHP Postgres) Stack. Download and install it! (If you choose MAMP instead, the paths might be slightly different, but easy to follow.)

The stack comes with Apache all set up properly, with commented out settings for frameworks to enable, et cetera, and also has all the popular frameworks ready to go: cakePHP, laravel, symphony, zend, code igniter, and smarty. It also includes other dependencies like Varnish, ImageMagick, PEAR, PECL and more! Awesome, right?

Recommended PATH Update to ~/.bash_profile

In the /Applications/mappstack-5.4.10-0/ folder (your version might be slightly different) there is a use_mapstack file, open it in a text editor and you'll see a PATH statement which puts all the apache, php/bin, and [framework]/bin folders, etc. come first in the PATH, this is important so that other versions on your computer don't interfere.

To make my life easier I copied that whole set of paths to a new variable in ~/.bash_profile, and added the root folder of the stack at the beginning of it:

export PHP_PATHS="/Applications/mappstack-5.4.10-0:/Applications/mappstack-5.4.10-0/frameworks..."

Then made a nice simple path export:

export PATH="$PHP_PATHS:$PATH"

So, I don't really use the "use_mappstack" file as I don't really need it. I think if you have multiple mappstacks then it makes more sense. If you decide to use it, you still might want to comment out: #exec /bin/bash --noprofile --norc to stay in the same shell.

Second, Setup libcouchbase C library (Mac)

If you haven't installed Xcode & Command Line Tools via Xcode, you have two options here. One is the obvious, get Xcode (4.4 GB) from the Mac App Store, then once installed, go to Preferences > Downloads and Install Command Line Tools (175 MB). Or if you don't want Xcode, you can go to Apple Developer Connection. Put in your apple ID and download just the Command Line Tools and install that (about 175 MB).

On Mac, Homebrew is your friend. If you haven't ever set it up, check out Homebrew (scroll down).

If you have a previous version of libcouchbase, simply:

$ brew uninstall libcouchbase

Now install libcouchbase:

$ brew update && brew install libcouchbase

If you get any messages about being unable to link, you can force it to re-link by doing:

$ brew link --overwrite libcouchbase

Third, Setup the PHP-Couchbase SDK

I think building from source is the easiest and just do it with all versions of PHP, but of course the Bitnami MAPP stack is PHP version 5.4.10, and as of this writing, the php sdk needs to be built from source for 5.4.x

1. Download PHP Source PHP 1.1.2 SDK, or latest Source Archive on the PHP SDK page

2. Unzip/tar it, cd into folder, then do the following:

$ phpize
$ ./configure
$ make

You don't need to do a make install, also if you are missing something (like autoconf), phpize will let you know, and you can brew install it.

1. You can copy the couchbase.so file you just built into /Applications/mappstack-5.4.10-0/php/lib/php/extensions, or just leave it where it is, or put it in ~/Documents, or wherever you want (I put it in my shared Dropbox folder so I can share it with my other computers running the same stack.)

2. Whichever you chose (move or keep it where it is), add extension=full/path/to/couchbase.so in the php.ini file located here: /Applications/mappstack-5.4.10-0/php/etc/php.ini

Starting and Restarting Apache

If you added the stuff to your ~/.bash_profile earlier then you can use ctlscript.sh from anywhere to control starting/stopping Apache, etc.

Start/restart Apache:

$ ctlscript.sh start apache

OR 

$ ctlscript.sh restart apache

By starting/restarting on the command line you can see if there were any errors in your php.ini, or with the .so extension.

Now you're Setup!

Fire up Couchbase Server if it's not fired up, and test your connection with this simple php script:

phptest.php

<?php

echo "--------------------------------------\n";
echo "\tCouchbase Connection\n";
echo "--------------------------------------\n";

// Connect to default bucket on localhost
$cb = new Couchbase("127.0.0.1:8091", "", "", "default");

// Another quick way to connect to default bucket on localhost
$cb = new Couchbase();

// Create a key
$cb->set("phptest", 1);

// Retrieve the key and output it
echo("$" . "cb->get(\"phptest\") = " . $cb->get("phptest") . "\n");

// Delete the key
$result = $cb->delete("phptest");

if ($result) {
  echo("Delete succeeded");
} else {
  echo("Delete failed : key does not exist");  
}

echo "--------------------------------------\n";

?>

Save that to a file, and run it. If you have setup the PATHS as I recommended, you can just do:

$ php phptest.php


Published at DZone with permission of Don Pinto, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)