← ServerPilot Docs

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, 7.1, 7.2, and 7.3.

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

Uninstalling the MongoDB Extension

To uninstall this extension, as root run the commands:

sudo rm /etc/phpX.Y-sp/conf.d/mongodb.ini
sudo peclX.Y-sp uninstall mongodb

Next, restart PHP-FPM with the command:

sudo service phpX.Y-fpm-sp restart

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: October 8, 2018

Launch your first site in 5 minutes