Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project MongoDB
Purpose
The purpose of this tutorial is to show how to install the MongoDB driver (for PHP) onto an AWS (Amazon Web Services) Ubuntu 12.04 EC2 Instance running Apache2 / PHP 5x. Unfortunately it is not as simple as it likely could be, but still not too confusing.

Install Pear (if not already installed)
Pecl requires that "pear" be installed in order to install the MongoDB Driver. Let's find out if pear is installed:
$ pear
If you get no information, whilst you have php installed, pear is not. However, you should get a suggestion as to how to install pear such as:
$ sudo apt-get install php-pear
By default you should get the latest version installed. You can determine the version by:
$ pear version

Install Make (if not already installed)
Another application that will need to be installed is Make, for the MongoDB Driver. To determine if it is installed, type:
$ make
If it says it is not installed then you will need to install it via something like:
$ sudo apt-get install make

[OPTIONAL] Integrating Pear with PHP
If you are not using "Pear" in your PHP pages, you may not need to be concerned with further integration for it. Here are some useful snippets:
If you need to find out where Pear was installed you can do so by:
$ pear config-get php_dir
It should respond with something like "/usr/share/php".
To test, create a new simple PHP page such as:
<?php
require_once("System.php");
var_dump(class_exists("System", false));
?>

If you do not get "bool(true)" it looks like you will need to do some further work as indicated at http://pear.php.net/manual/en/installation.checking.php.

[OPTIONAL] Install SASL
In a lot of internet documentation pertaining specifically to installing the MongoDB driver, there is no mention of the Cyrus SASL, yet it is the first prompt you get when you start an installation for MongoDB. You can install the MongoDB driver without also installing Cyrus SASL, but if you don't have it and think you may need it then:
$ sudo apt-get install libsasl2-dev

Install the MongoDB driver [link]
At the prompt, enter the following command:
$ sudo pecl install mongo
You will get a prompt "Build with Cyrus SASL (MongoDB Enterprise Authentication) support? [no]". If you do not have SASL instealled (sasl.h) then enter no to continue.
A heck of a lot of text should be dumped and you should eventually see "Libraries have been installed in" which will point to something similar to "/tmp/pear/temp/pear-build-abcd/mongo-1.5.8/modules" followed by "Build process completed successfully" with installation pointing to something like "/usr/lib/php5/20120626/mongo.so" and mention of needing to add "extension=mongo.so" to the php.ini file.
Restart the Apache server:
$ sudo service apache2 graceful -- if you prefer finesse
[OR]
$ sudo service apache2 restart -- if you cannot wait around

Time For Some Testing
To test, create a new simple PHP page such as the following and load in your web browser:
<?php
echo phpinfo();
?>

Look for "Configuration File (php.ini) Path" in case you need to remember the location. Then look for mongo on the php page output...if it is not there, php cannot see it (even if the mongo.so is in the previously alluded to "/usr/lib/php5/20120626/" location).

How to Get PHP to Recognize MongoDB
Let's assume you are not sure if you should modify the php.ini file (many standard installations use the php.ini file, but with Ubuntu separate ini files are usually used). So I'll show some examples of how to resolve this "fork".
If mongo was NOT found from the phpinfo() dump, you need to determine if modules are loaded by the php.ini file OR through the use of external ini files based on your implementation. There are a variety of ways to find out, but one way would be to go to:
/etc/php5/conf.d
If you see .ini files there (such as mcrypt.ini, mysql.ini, pdo.ini, etc) chances are you are using external ini files instead the php.ini file for loading. Another way of telling is looking at the dump of phpinfo() for the section "Scan this dir for additional .ini files" and "Additional .ini files parsed" which would give an indication of external ini loading. In this case, you will want to create a new file "mongo.ini" with the following content:
; configuration for php mongoDB module
extension=mongo.so

On the other hand, if you know the php.ini file is used instead of external ini files for loading, you'll need to edit the php.ini file. In that file you will need to add the extension for Mongo AFTER the line for Json (if any, to avoid load conflicts) such as:
extension=json.so
extension=mongo.so


Final Test!
After modification (either adding an additional ini or modifying the php.ini based on your implementation), you'll need to restart the Apache server:
$ sudo service apache2 graceful -- if you prefer finesse
[OR]
$ sudo service apache2 restart -- if you cannot wait around
Once that is completed you'll need to, one last time, view the dump of phpinfo(). This time, you should see something similar to the following for MongoDB:
MongoDB installed and recognized by PHP

About Joe