This tutorial will show you how to configure MongoDB PHP 7 for XAMPP on Windows. XAMPP is an open source, easy to install Apache distribution containing PHP, Perl and MySQL. It’s very easy to install after downloading the Windows exe inataller file.
MongoDB is one of the widely used NoSQL(Not only SQL) database in market today. MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Often we come across a situation where we end up using MongoDB with PHP in XAMPP stack.
Since MongoDB does not ship with XAMPP stack so you have to setup manually.
Apache HTTP Server 2.4, PHP 7.4.3, MongoDB 4.4.0
If you are using XAMPP then you don’t need Apache Server and PHP separately
Please go through the following steps in order to setup MongoDB with PHP7
Step 1. Download and install XAMPP on Windows
Step 2. Check the PHP version 7.4.3 has been installed. Create a phpinfo.php file under /xampp/htdocs directory or if you do not use XAMPP then you can simply create the file under Apache server’s htdocs folder and write below code into it.
<?php echo phpinfo(); ?>
Step 3. Start XAMPP control panel and start the Apache module. Run the above file, you will find the php version as shown below image.
If you are not using XAMPP then just start Apache HTTP Server by executing the httpd.exe file from bin folder in the command line tool.
Step 4. Configure MongoDB Server. Please read tutorial how to configure MongoDB server in Windows Operating System.
Step 8. Download the MongoDB PHP driver in order to establish communication between these two technologies. Choose the file 7.4 Thread Safe (TS) x64.
Step 9. Now extract the archive and copy the php_mongodb.dll file into /xampp/php/ext directory. Now open the /xampp/php/php.ini file and add the below line in order to make it work.
If you are not using XAMPP then you can open your ext directory from the PHP installation directory. Similarly you can edit the file php.ini from PHP installation directory.
Step 10. Add php to System Variables. Open computer’s Advance system settings and click on Environment Variables and add /xampp/php/ to Path under User’s variables (ex, ;/xampp/php/). Click Ok.
If you are not using XAMPP then you need to add PHP installation root directory to the system variable.
Step 11. Now check php version from cmd prompt whether you have successfully added php to System Variables or not.
Step 12. Now restart the Apache server in XAMPP control panel to reflect the changes you have made to php.ini.
If you are not using XAMPP then just restart the Apache HTTP Server.
Step 13. Now run the phpinfo.php file and check for mongodb. You should see something similar to below image.
Step 14. Now test the MongoDB connection from PHP 7 using the below code in php-mongo.php file.
<?php // Configuration $dbhost = 'localhost'; $dbport = '27017'; $conn = new MongoDB\Driver\Manager("mongodb://$dbhost:$dbport"); print_r($conn); ?>
Step 15. Run the above php file, you will see below output in the browser.
Congratulations! You have successfully connected to the MongoDB using PHP 7.
You may also like to read MongoDB PHP 7 CRUD Example.
Thanks for reading.