Tuesday, December 11, 2012

IIS, PHP and Windows

In this short blog post I’m going to show you how to use PHP to access your Couchbase cluster on your Windows machine. I have a limited set of hardware at home, so I used a laptop with Microsoft Windows 7 home premium edition to test this example. Unfortunately for you that laptop came with a Norwegian version of Windows, so I can't tell you the exact name for the various menu items.

The first thing we need to do is to install Microsoft Internet Information Services (I'm going to refer to this as IIS) on your computer. You do so by selecting the "enable/disable windows features", and check the box for "Internet Information Services". Expand that selection and locate and expand "Webservices", and then locate and expand something like "application development" (sorry for not having an english copy of Windows ;)). In this category check the CGI checkbox. At the level right beneath the Internet Information Services you should have an entry containing something like "tools for web management", and in this sub category you should check "IIS-management console".

So lets go ahead and verify that the IIS installation works, and that it doesn’t support PHP already (that would render this blogpost pretty useless right ;)) Go ahead and put the following into c:\inetpub\webroot\test.php:


If you try to connect to http://localhost/test.php, you should either a page with an error message. If you get something else your IIS installation may already be configured (and hopefully we'll make it work).

The next thing we need to do is to install PHP and the Couchbase extension. At the time of writing we don’t have a binary package you can install, but it isn’t really hard to build it yourself. If you follow the steps I outlined in http://trondn.blogspot.no/2012/11/building-php-extension-for-couchbase-on.html you should be up’n’running in no time. I followed that instruction when I wrote this example, but I used a slightly different configure command:

configure --disable-all --enable-cli --with-couchbase=shared --enable-json --enable-cgi --with-config-file-scan-dir=C:\php\conf.d

You should now create C:\php\conf.d\couchbase.ini with the following content:


Now that we’ve got IIS and PHP installed its time to look at the magic to glue them together. Open up the IIS Service Management Console (as I’ve already said I've got Norwegian windows so I may have used the wrong translation there) and select “Handler Mappings”, and in the actions pane (upper right corner) select “Add Module Mapping”. Fill in the following details starting from the top: “*.php”, “FastCgiModule”, “C:\php\php-cgi.exe” and whatever you want as the description. Be sure to answer “Yes” on the next dialog that asks if it should create a FastCGI application for this executable (if not it won’t work). You may also want to enter the “default document” setting and add index.php as an alternative. By doing that IIS will search for a file named index.php if the URL specifies a directory.

With this in place it’s time to retry our test page. Tune your browser back to http://localhost/test.php, and this time you should see a nice page with a lot of info. Scroll down and you should locate a section with information about the Couchbase extension.

Yay!!! We made that work, but being able to run PHP from IIS isn’t that fun all by itself.. Let’s see if we can get the Couchbase beersample for php working!

Go ahead and download and install Couchbase server 2.0 from http://www.couchbase.com/download, and choose that you want to install the beer sample database during configuration.
With that in place you can download the PHP example program I wrote earlier today (I’ll describe that in another blog post) from https://github.com/trondn/beersample-php/archive/master.zip. Unzip that program in C:\inetpub\webroot. Before we can try the sample application you have to create two new views in Couchbase. The first one should be in a design document named brewery and named by_name and look like:

    function (doc, meta) {
        if (doc.type == "brewery") {
            emit(meta.id, doc.name);

The next one is located in the beer design document and named by_name and looks like:

function (doc, meta) { if (doc.type == "beer") { emit(doc.name, doc.brewery_id); } }

Make sure you remember to press the publish button after you create them, or things won't work.

You should now be able to direct your browser to http://localhost/beersample-php-master and start browsing breweries :)

Happy hacking!


  1. Do you really need the C:\php\conf.d\couchbase.ini file? Can't you just put the "extension=php_couchbase.dll" line in your php.ini file?

    1. That's correct. Personally I prefer to put it in its own file in case you want to tune any of the other tunables in the module.

  2. Your title is "PHP performance and memory usage". The tests seem to only cover speed of the different solutions. I would very interested to see memory usage comparisons, as I am looking to optimize memory usage in my application.


  3. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. free file shreder