Configure MongoDB with PHP on windows for XAMPP in 5 simple steps
Today we will configure MongoDB with PHP. It is very easy to do so. For that let’s install latest XAMPP, here I am using XAMPP -1.8 with VC9 on windows XP.
Install XAMPP and create a test file on htdocs with following code:
Save it as phpinfo.php
Now run the phpinfo.php file in browser and check for “mongo” by pressing ctrl + F. You will definitely not get anything as MongoDB extension is not there on XAMPP right now. Also Observe your PHP version Compiler and architecture on the info page. As mentioned on screenshot.
Lets download the php driver for MongoDB from following links:
I will prefer first one that is from Github:
https://github.com/mongodb/mongo-php-driver/downloads and download the package that best suits to your php like compare your php version, Architecture and Compiler as in my case my php version is php 5.4 , Compiler is MSVC9 that is VC9 and architecture is x86. So based upon this configuration I will download package #2 as marked and shown in screenshot below.
Download and extract the package on your system and it will look like below screenshot:
I will select the php_mongo-1.3.1-5.4-vc9.dll as marked in screenshot. I will rename it as php_mongo.dll and put it in PHP folder of XAMPP.
open your php.ini file and add the below line of code in it:
as shown in below screenshot.
Step V: Restart Apache and again refer Step I , run phpinfo.php file in browser and again search for “mongo” this time you will find the
it there as extension MongoDB extension is enabled and thats it….
Hope you enjoyed the article, soon I will post some example of php and MongoDB here…
Updated for higher versions::
If above is not working for you for some reasons may be you are windows is of higher version like windows 7 and above then you may try below:
Here couple of steps are same as above. Also I am using Windows 8 for this, and it is configured for PHP 5.6.3 version.
A. Same as Step I above, we will do phpinfo(); again and this time we will get some other results as per our higher windows architecture and due to other higher versions of softwares. You will see something like as shown in below screenshot:
B. Then goto https://s3.amazonaws.com/drivers.mongodb.org/php/index.html get last package, say for this configuration, as shown below.
C. Extract the zip, and you will find some dll files in it as shown below, so as per our configuration in step “A” we will select the highlighted dll, as it match with our configuration like PHP version 5.6 and visuall c++ version 11, as shown in below image.
D. Rename it to php_mongo.dll and copy it and paste it in your \xampp\php\ext folder , in my case its C:\xampp\php\ext
E. open your php.ini file and add the below line of code in it (if doesn’t exists): extension=php_mongo.dll , as mentioned in Step IV above.
F. Restart Apache, this time you might see some apache error as mentioned in below images:
G. To tackle this go to your \xampp\php folder, which is C:\xampp\php in my case. Copy libsasl.dll (as shown in below image), paste it in \xampp\apache\bin ,which is C:\xampp\apache\bin in my case.
H. Now restart Apache again, and you are there.
F. On Success, repeat Step “A” again and search for “mongo”, this time you will find mongo module enabled as shown below.
Here are some references sites: