How add mongodb to xampp?

XAMPP is an open source, easy to use and easy to install stack that contains Apache webserver, MySQL database, PHP compiler and Perl.

MongoDB is one of the most widely NoSQL database in market today. We often end up in a situation where we might find it useful to set up mongodb also along with PHP in the XAMPP stack.

Since mongoDB is not an integral part of this stack, we have to set it up manually as the XAMPP installer is not going to take care of it for you.

Follow the steps below to configure MongoDB for the XAMPP stack.

1. Install and Configure XAMPP

First, you should install the XAMPP stack. Download and install the XAMPP stack from Apache friends project.

Also, keep in mind that you can also install XAMPP on Linux as we discussed earlier.

After the install, start your Apache server from XAMPP controls and create a simple PHP file to get the detailed info about the PHP running with your stack. Just copy paste the below lines to a test.php file in the htdocs folder and execute it to see the output.

<?php
 echo phpinfo();
?>

As highlighted in the screenshot below, you will find the PHP version, Architecture, Compiler in use and can see whether thread safety is enabled or not.

How add mongodb to xampp?

2. Download PHP Mongo Driver

From this PHP Mongo Driver download page, download the appropriate file that matches the PHP version, Architecture, Compiler in use and Thread Safety from the XAMPP that is installed on your system.

3. Copy PHP Mongo DDL to EXT Directory

After you unzip the php monngo driver zip file, copy and paste the “.dll” file to the folder “C:\xampp\php\ext”( Assuming that xampp is installed on C drive).

This ext folder all the “.dll” files of all the extensions that are installed. XAMPP loads the driver files for the extensions from this folder.

After copying the file over here, rename the “.dll” file to “php_mongo.dll” for simplicity.

4. Add Extension to php.ini

Next, open the “php.ini” file from the path “C:\xampp\php” (again, assuming that xampp is installed on C drive), and edit this file to add the name of the “.dll” file as an extension.

Add the following line to the php.ini file.

extension=php_mongo.dll

Later, if you like, you can also disable this extension by adding a semicolon before the line so that it becomes as shown below:

;extension=php_mongo.dll

5. Modify the PATH Variable

Go to control panel, and open the system settings to add the “Environment Variable”.

Add the path of the xampp php installation ( C:\xampp\php ) to the path variable, if it is not present already. This ensures that the newly added “.dll” file is loaded when xampp is started.

How add mongodb to xampp?

6. Restart Apache and Verify

Finally, restart the Apache server from the XAMPP control panel.

If everything is configured properly, xampp should not throw any error messages while starting apache. You can also check the loaded extension by going through the first step and looking into the php information.

You will be able to see the loaded extension information on the page as shown below.

How add mongodb to xampp?

As we have seen in my earlier post that how to install and run mongodb on windows 10. But we didn’t see how mongodb will connect to php. I usually use xampp for my local development which have php 7.2 version. So we will be configuring mongodb on xampp to make it works.

If you don’t have xampp on your machine, please have a look on how to install xampp on windows 10.

By default, PHP in XAMPP has no mongodb support. So we have to download mongodb extension from PECL (PHP extension repository) and configure it in xampp.

So lets started.

Also Read:

  • How to Install Composer on Windows 10
  • How to install Codeigniter in Xampp
  • How to install Slim Framework 3 in windows using XAMPP
  • How to install Laravel 5.6 on windows using XAMPP
  1. Check your current PHP version and also check that it is Thread Safe or not. My PHP version is 7.2 with thread safety enabled.
  2. Goto https://pecl.php.net/package/mongodb/1.5.3/windows and download driver according to your windows and php version. I am going to download 7.2 Thread Safe (TS) x86 because I have windows 10 64bit.
  3. After downloading zip file. Unzip that zip file and copy php_mongodb.dll to your xampp\php\ext directory.I have installed xampp in D: drive so my path will be D:\xampp\php\ext.
  4. Now open php.ini by clicking Config and then click on PHP(php.ini) in XAMPP control panel.
  5. Add extension=php_mongodb.dll in php.ini and save it.
  6. Restart XAMPP.
  7. Now again check PHP info. Now there will be a separate section for mongodb. If you didn’t find mongodb section in phpinfo then make sure you downloaded current mongodb driver zip.
  8. Now open cmd and start mongodb server my typing mongod command.
  9. Now create a mongo-test.php file in htdocs to check php and mongodb communication.
  10. Now run mongo-test.php file from browser. If all goes well, you should get object.

Note: Default address of mongodb server is 127.0.0.1 and default port is 27017.

Test code: (mongo-test.php)

<?php

$manager=newMongoDB\Driver\Manager("mongodb://localhost:27017/");

var_dump($manager);

?>

Step by Step Images:

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

How add mongodb to xampp?

Can we use MongoDB in XAMPP?

ini-file and restart your Apache server through the XAMPP control panel. After that check http://localhost/xampp/ again and search for “mongodb”. You should see something like: Now you're done and MongoDB is available within your XAMPP environment.

Can I connect PHP with MongoDB?

You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.

How manually install MongoDB?

Follow the MongoDB Community Edition installation wizard..
Select Install MongoD as a Service MongoDB as a service..
Select either: Run the service as Network Service user (Default) ... .
Service Name. Specify the service name. ... .
Data Directory. Specify the data directory, which corresponds to the --dbpath . ... .
Log Directory..

How enable MongoDB in PHP INI?

Install manually After executing the previous commands, you have to make a change in your php. ini file. Open the file in your favorite text editor and extension_dir variable is pointing to the location of mongo.so. Also, add 'extension=mongo.so' in a file, save and restart your web server.