How to Setup MongoDB PHP Extension on Shared Hosting
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.

July 21st, 2010 at 2:36 PM
Hi Phillip, I do a similar procedure:
http://jag2kn.blogspot.com/2010/07/mongodb-en-dreamhost-basic.html
but they send me a notification
”
Our automated system has come across the following activity under your account
that is in violation of our Terms of Service. We request that you take any
necessary action to cease the activity as quickly as possible or we will be
forced to take further action. If you were not aware of any such activity,
please feel free to contact support and we can look into the issue further.
User username running “bin/mongod –dbpath data/db/ ” listening on port 27017.
Network daemons are against our Terms of Service.
”
you have received this message?
thanks
July 21st, 2010 at 6:45 PM
Hey Jorge,
It looks like you got that notification since you are actually hosting your Mongo database on dreamhost’s shared hosting. They don’t allow that
What they DO allow, however, is to host your Mongo database externally (check out MongoHQ.com), and connect to it via PHP. It’s not ideal, but even the free MongoHQ database works for small projects.
Hope that helps.
July 30th, 2010 at 6:58 PM
Yes it’s true
I am thinking start a mongo database server on demand of a php script
thanks for the answer
October 24th, 2010 at 6:27 PM
Hmm, I can’t seem to get this to work on my dreamhost shared hosting.
Everything went smoothly just as you had written in this tutorial, but then when I go to test it, it claims it can’t find the Mongo class. Even though I’ve made sure mongo.so is in the $HOME/bin folder and it’s using the correct php.ini file… (phpinfo says the extension_dir is correct)
Any ideas why I have this problem?
October 24th, 2010 at 8:04 PM
Hey Leander,
Is it possible you missed a step in the Dreamhost Wiki when setting up your custom php.ini? Try this:
1. Create a .htaccess file in the root of your domain’s folder.
2. Add the following code somewhere in the file:
3. Try again. Does it find the Mongo class now?
Although, if you were getting the correct extension_dir before, I’m not entirely sure what else could be wrong. Let me know if you get it figured out!
October 26th, 2010 at 6:27 PM
To do all these that you describe in this excellent article, a Private Servert must be acquired in DreamHost.
Otherwise it won’t be possible.
October 27th, 2010 at 12:32 PM
Fabricio,
I managed to get PHP5 with the MongoDB PHP extension running just fine on my Shared Dreamhost Server. While it’s quite tricky to actually get working, it is very doable without having to buy a private server.
December 20th, 2010 at 3:23 PM
[...] Napieralski wrote a helpful post about How to Setup MongoDB PHP Extension on Shared Hosting earlier this [...]
June 2nd, 2011 at 1:11 PM
super helpful, thanks!!
June 14th, 2011 at 11:31 PM
hey there, good article but I have a question. How would I use MongoHq with an app hosted on PHPfog? I m new to Github and I m confused actually. On PHPFog, you use commits through Github to push your files. How would I fit MongoHq in the picture? Thanks in advance. sebastien
June 15th, 2011 at 4:18 PM
Sebastien,
Good question, though I’m not entirely sure how to do it for PHPFog. The best information I could find is this post: http://community.phpfog.com/discussion/90/hi-im-stephen-a-developer-in-beijing-china/p1
Are the mongo php extensions already available? If so, you can skip right to the PHP source code in my post [... $m = new Mongo(...) ]