Archive for the ‘Shared hosting’ Category

How to Setup MongoDB PHP Extension on Shared Hosting

Wednesday, July 7th, 2010 by Phillip Napieralski

Want to use MongoDB but don’t want to switch to a more expensive hosting plan? This is a great alternative that I got working on Dreamhost in less than an hour!

Setup your external MongoDB

I use MongoHQ to host my database (your first 16MB database is free). Make sure to take note of your connection string for later.

Custom PHP.ini

This part is somewhat tricky. Luckily, if you are using dreamhost, it is well documented on their wiki. Thus, I won’t go into detail of that here. Follow the directions and proceed to the next step (or leave a comment asking for help if you have issues).

After setting up your custom ini file for your domain, add the following lines to the very end of your php.ini:

extension_dir = "/home/YOUR_USERNAME/bin"
extension = mongo.so

NOTE: This step loads the MongoDB extension but, we don’t have it yet! The next steps will fix that.

Compile MongoDB: Part 1

Using SSH, cd into your home folder (/home/YOUR_USERNAME) and type the following:

mkdir bin
cd bin/
wget http://download.github.com/mongodb-mongo-php-driver-1.0.6-0-gd261d7a.tar.gz
tar zxvf mongodb-mongo-php-driver-1.0.6-0-gd261d7a.tar.gz
cd mongodb-mongo-php-driver-gd261d7a/
phpize

If you receive the error Cannot find autoconf after running the command phpize, follow the next section. Otherwise, skip it!

Compile/install autoconf

wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.66.tar.gz 
tar -zvxf autoconf-2.66.tar.gz
cd autoconf-2.66/
./configure "--prefix=$HOME" 
make 
make install

The configure line will tell make install to put the library (.so) file into our home/bin directory (eg, /home/YOUR_USERNAME/bin). Quickly check that you have a few autoconf folders in your bin directory and continue on.

IMPORTANT: You must now tell linux to look for libraries in your home/bin directory. To do this, you must change the PATH as follows:

EXPORT PATH=$HOME/bin:$PATH

Compile MongoDB: Part 2

Remember that phpize function? Yea, try it again:

cd $HOME/bin/mongodb-mongo-php-driver-gd261d7a/
phpize

This time, you should not receive an autoconf error. Now, we actually compile the extension.

./configure "--prefix=$HOME"
make

And, for some reason, the make install commands ignores our prefix… so we can just manually copy the needed (.so) file to our bin folder:

cp modules/mongo.so $HOME/bin/

Now double check that Mongo.so exists in your home/bin directory (eg, /home/YOUR_USERNAME/bin/mongo.so)

Test it

Now, here’s a simple script to test if you can insert something into your external Mongo database.

<?php
   $m = new Mongo("mongodb://YOUR_USERNAME:YOUR_PASSWORD@YOUR_SERVER:27046/YOUR_DATABASE");
   $m->connect();
   $db = $m->YOUR_DATABASE
   $collection = $db->YOUR_COLLECTION;

   $collection->insert(array('name' => 'super test 5000'));

There! You should have just inserted a new record into your database collection. Test by changing the insert line to the following:

$cursor = $collection->find();

echo $cursor->count() . ' documents found. <br/>';
foreach ($cursor as $obj) {
   var_dump($obj);
   echo '<br/>';
}

Now when you run that last script, you should see at least the one record you JUST added!

Conclusion

This is the approach I am currently using for one of my smaller pet projects. I’m going to be doing a lot of testing on my local machine using XAMPP, then going live with it using Dreamhost. It is also unfortunate that I’m forced to use an external DB. But, considering the first 16MB of database from MongoHQ is free, I’m totally satisfied with this for now.