MongoDB and the new PHP on Heroku

Update 5/7/14 5:15PM: Post has been rewritten to reflect Heroku’s PHP Buildpack changes

Update 5/5/14 10:45PM: Heroku’s default PHP buildpack now allows Composer to install the MongoDB extension. You do not need to use the develop branch. To enable, add “ext-mongo” to the composer.json file as described in the Solution section.

One of our close Platform-as-a-Service (PaaS) partners, Heroku, recently announced its official public beta for new PHP features. This announcement was met with much excitement from the developer community, and the MongoLab team looks forward to working with PHP developers on Heroku who want to power their apps with fully-managed MongoDB databases.

This post will cover how to install the PHP MongoDB extension on Heroku.

Running PHP on Heroku

If you’re just getting started with PHP on Heroku, we highly recommend visiting their Dev Center for a quick tutorial. Heroku leverages a PHP buildpack to help you quickly get up and running with a standard PHP environment and come bundled with the Composer package for easy package management.

Activating the MongoDB extension

To activate MongoDB for PHP, you can use Heroku’s built-in extensions that can be enabled in the composer.json file. Following the instructions for activating optional extensions, all you need to do is add “ext-mongo” to the “require” section of your composer.json. If you have no other dependencies in your application, the resulting file will look like this:

{
  “require”: {
    “ext-mongo”: ”*”
  }
}

Commit and push this change then you’re good to go!

Happy hacking!

We hope this post helps you get up and running on Heroku with PHP and MongoDB. For more information on building PHP apps on Heroku, check out the Dev Center PHP docs!

Subscribe

Subscribe to our e-mail newsletter to receive updates.