How to install MongoDB PHP extension
How to install MongoDB PHP extension
In our last tutorials I’ve showed you how to install MongoDB server directly on a linux Debian or Centos. The process is simple and you just need about 10 minutes or so for the average beginner to start working from shell with MongoDB. But what comes next is using a programming language like PHP to connect directly from a website to our noSQL database server. In this tutorial I’m gonna go through the steps you need to run to install MongoDB PHP extension on debian / Ubuntu operating system.
Preparing the needed packages
To install the MongoDB PHP extension automatically without compiling you need to install certain packages that are required. You can do this running the commands:
1 |
apt-get install php5-dev php5-cli php-pear |
Install MongoDB PHP Extension
1 |
pecl mongo |
Finally you need to add the extension to your php.ini, I normally add a new file the conf.d folder, but you can add it to the php.ini directly if you wish.
1 |
echo "extension=mongo.so" >> /etc/php5/conf.d/mongo.ini |
Manually compile MongoDB PHP Extension
There are certain cases where pecl can not be used to install the php extension, it happen with certain extension with me and I was forced to manually compile it. This is not really for beginners as a wrong step can install things badly can cause issues, however for those that feel comfortably with this, then can follow the below steps.
First you need to go to Github and download the MongoDB driver / extension:
https://github.com/mongodb/mongo-php-driver
1 2 |
unzip mongo-php-driver-master.zip cd mongo-php-driver-master |
For the next command you need to make sure you have the php5-dev installed, you can run the bellow command for this:
1 |
apt-get install php5-dev |
Then you can run continue the installation:
1 2 3 4 |
phpize ./configure make make install |
And finally again add the extension to php.ini:
1 |
echo "extension=mongo.so" >> /etc/php5/conf.d/mongo.ini |
When you view your phpinfo() you should be able to find the MongoDB extension loaded and running as expected:
That would be all for now, see you again in our next tutorial where I’ll show you how you can use MongoDB with PHP.