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 =

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/
tar zxvf mongodb-mongo-php-driver-1.0.6-0-gd261d7a.tar.gz
cd mongodb-mongo-php-driver-gd261d7a/

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

Compile/install autoconf

tar -zvxf autoconf-2.66.tar.gz
cd autoconf-2.66/
./configure "--prefix=$HOME" 
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:


Compile MongoDB: Part 2

Remember that phpize function? Yea, try it again:

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

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

./configure "--prefix=$HOME"

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/ $HOME/bin/

Now double check that exists in your home/bin directory (eg, /home/YOUR_USERNAME/bin/

Test it

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

   $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) {
   echo '<br/>';

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


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.

Tags: , ,

12 Responses to “How to Setup MongoDB PHP Extension on Shared Hosting”

  1. Jorge Says:

    Hi Phillip, I do a similar procedure:

    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?


  2. Phillip Napieralski Says:

    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, and connect to it via PHP. It’s not ideal, but even the free MongoHQ database works for small projects.

    Hope that helps.

  3. Jorge Says:

    Yes it’s true

    I am thinking start a mongo database server on demand of a php script

    thanks for the answer

  4. Leander Says:

    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 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?

  5. Phillip Napieralski Says:

    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:

    Options +ExecCGI
    AddHandler php5-cgi .php
    Action php-cgi /cgi-bin/php-wrapper.fcgi
    Action php5-cgi /cgi-bin/php-wrapper.fcgi

    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!

  6. Fabricio Matías Quagliariello Says:

    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.

  7. Phillip Napieralski Says:


    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.

  8. How To Use MongoDB and PHP 5.3 on DreamHost | Daniel McOrmond Says:

    [...] Napieralski wrote a helpful post about How to Setup MongoDB PHP Extension on Shared Hosting earlier this [...]

  9. charlie Says:

    super helpful, thanks!!

  10. sebastien Says:

    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

  11. Phillip Napieralski Says:


    Good question, though I’m not entirely sure how to do it for PHPFog. The best information I could find is this post:

    Are the mongo php extensions already available? If so, you can skip right to the PHP source code in my post [... $m = new Mongo(...) ]

  12. Peter Herz Says:

    My ideal (decentralizing) setup is to use DH shared hosting ($8/mo unlimited storage/bandwidth) units as commodity XML-only document API endpoints, then on a centralized dedicated server or vm you use sshfs to mount each DH endpoint then create a worker process on master (dedicated) to scan these endpoints securely as if they’re in the local filesystem (no http/websockets required as sshfs is a fuse kernel module :)). There’s a NodeJS module designed to do this called ‘watchr’ or just roll your own with shell script, php, perl, etc. This central worker will then take all updating (streaming optionally) endpoints (with new XML) from clients and intelligently insert / update the mongo collection which centralizes data so clients can get to it later using this article’s method of mongodb php driver on shared hosting. This is how I’m designing my own distributed database system / client-trusted app. Also I’m planning to use E4X/XProc ( more (instead of the classical XML toolchain) and embedding my partial dbms/controllers in the client (within encrypted SWF), thereby semi-trusting the XML downstream and removing load off PHP to be more like NodeJS (which is best at I/0 not CPU and thereby making nodes each c10k candidates — probably not ;)). This way I could have dozens of these DH shared accounts orbiting the dedicated node or superclient (clients with dedicated storage/bandwidth availability & node consent) with their copious bandwidth and storage (and great many-honed backbone) offerings while funneling client and edge apps/data into an XML trunk. Too much information perhaps, I know. -Peter