Over a million developers have joined DZone.

MongoDB and the New PHP on Heroku

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.

Current challenge with Heroku’s PHP buildpack

While Heroku’s buildpack and the Composer package help you get started quickly, they have some limitations in regards to MongoDB. PHP deployments come packaged with built-in extensions such as cURL and MySQL, along with a few other extensions that can be enabled in the composer.json file. Currently however, MongoDB cannot be enabled through Composer.

We recently worked with one of our users having trouble getting the MongoDB extension installed. Because of Heroku’s ephemeral filesystem, conventional methods such as changing the php.ini file and using ‘pecl install mongo’ did not work. While Heroku does allow for custom INI settings via per-directory .user.ini files, that does not allow you to load extensions.

Solution

Thanks to David at Heroku Support, we have a solution! To ensure that your PHP deployments install the MongoDB extension every time, you can change your Heroku configuration to use the develop branch of the Heroku buildpack. Using the develop branch allows you to install the MongoDB extension via Composer. Run the command

 
heroku config:set BUILDPACK_URL= https://github.com/heroku/heroku-buildpack-php#develop

…then enable the extension in your composer.json by adding:

{
  “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. The Heroku team expects the develop branch to be pushed to master soon, and you can track the changes on either the Heroku changelog or on their twitter handle @HerokuChangelog.


Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Chris Chang, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}