Articles

How to Install the PHP MongoDB Extension

In the examples shown, replace "X.Y" with your app's PHP version (for example, "7.0"). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.
This PHP extension provides client access to the MongoDB server. It is not MongoDB itself. MongoDB is a NoSQL database server. When you install the mongo PHP extension, it will allow PHP to communicate with a MongoDB server.

The MongoDB PHP extension supports PHP 5.4, 5.5, 5.6, 7.0, and 7.1.

The MongoDB PHP extension does not yet support PHP 7.2.

Installing the MongoDB Extension

To install this extension, run the following commands as your server's root user:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install libssl-dev
sudo peclX.Y-sp install mongodb

Once installed, create a configuration file for the extension and restart PHP by running the following commands as root:

sudo bash -c "echo extension=mongodb.so > /etc/phpX.Y-sp/conf.d/mongodb.ini"
sudo service phpX.Y-fpm-sp restart

Verify the Installation

You can verify the MongoDB extension is installed by running this command:

phpX.Y-sp -i | grep -i mongodb

You should see output such as the following:

/etc/phpX.Y-sp/conf.d/mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.8
mongodb stability => stable
mongodb.debug => no value => no value

Using the MongoDB PHP Library

Once you've installed the mongodb PHP extension, the easiest way to use the extension is through the MongoDB PHP library.

See the PHP documentation for a tutorial on using the MongoDB PHP library.

Removing the Deprecated mongo PHP Extension

If you previously installed the older mongo extension (the mongo extension is different than the mongodb extension), you can remove it with the following commands run as root:

sudo rm /etc/phpX.Y-sp/conf.d/mongo.ini
sudo service phpX.Y-fpm-sp restart

Last updated: August 31, 2017