Using PHP with MongoDB in the Cloud

You must first provision your application with a MongoDB database from the dashboard. If you are using an external MongoDB database, you are not required to provision a database with your app. To deploy your application follow the instructions below:

Log in and navigate to the Dashboard. Click on deployments and complete the form. You will be required to select the size of your container and the region where your cluster is to be hosted.

Create your application on NodeChef

Add the "ini" file to your application declaring the mongo or mongodb extension depending on if you are using the latest or the legacy driver.

In the root folder of your application create the directory structure ./bp-config/php/php.ini.d/. Place a file with the extension .ini under this directory. We will use this file to enable both php and zend extensions. Your folder structure should look like this: Note, we also created a file options.json under the .bp-config/ folder. We will use this file to specify the PHP version we want if we are using the legacy mongo driver.

index.php .bp-config/ options.json php/ php.ini.d/ some_directives.ini

Declare mongo as an extension or mongodb as an extension. You can also declare zend extensions here using for example: ""

Applications using the legacy Mongo driver

It is important you understand, PHP has two drivers which is the legacy mongo driver and the latest driver which is the extension mongodb

If your application receives the error "Class 'MongoClient' not found" (see issue) or in your code you are connecting to MongoDB using new MongoClient(""), that means you are using the old driver and must use PHP version 5.6 on NodeChef for this to work. To make this work use the below in your ini file:

After this you must instruct NodeChef you want to use PHP 5.6 as the extension is only available in PHP 5.6. In your options.json file see the directory structure we created above, format the JSON file with the below:


Applications using the latest Mongo Driver

For those using the lastest driver and most likely initiating their PHP client as "new MongoDB\Client("mongodb://localhost:27017");", you are using the new driver and must specify the mongodb extension

You can use composer here as well, if you run composer require mongodb/mongodb on your terminal, make sure you have the require 'vendor/autoload.php'; in your code. See the official PHP MongoDB docs for more details

You do not have to specify the PHP version when using the latest Mongo driver as we always build with the latest stable version.

Deploying your code

After you have setup your project, you can then zip and upload it from the dashboard, push to your git repository to deploy or use our cli.

If you face any issues, do not hesistate to open an issue in our forums, our team will help you right away