How to Install the PHP MongoDB Extension
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
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: February 26, 2018