If you're developing a PHP application and need to connect to a MongoDB database, you will need the MongoDB PHP extension. This guide walks you through the process of installing the MongoDB extension in XAMPP on a Windows system.
Step-by-Step Guide
1. Determine Your PHP Version
First, you need to know your PHP version to download the compatible MongoDB DLL file.
- Open the XAMPP Control Panel.
- Start Apache.
- Click on the "Admin" button next to Apache.
- A web browser will open. Create a new PHP file (e.g.,
info.php) in thehtdocsdirectory with the following content:<?php phpinfo(); ?> - Open this file in your browser by navigating to
http://localhost/info.php. - Look for the PHP version information on this page. Note down your PHP version (e.g., 8.2.4).
2. Download the MongoDB DLL File
Download the MongoDB DLL file that matches your PHP version from the MongoDB PHP driver GitHub releases page.
- Go to the MongoDB PHP driver GitHub releases page.
- Find the version that corresponds to your PHP version. For example, for PHP 8.2.4, download the
php_mongodb-1.15.0-8.2-ts-x64.zipfile (wheretsstands for Thread Safe andx64for 64-bit). - Extract the downloaded ZIP file.
3. Copy the DLL Files to XAMPP's Extension Directory
After extracting the ZIP file, you will have the php_mongodb.dll and php_mongodb.pdb files. These need to be copied to the XAMPP extension directory.
- Navigate to the extracted files.
- Copy
php_mongodb.dllandphp_mongodb.pdb. - Paste them into the XAMPP PHP extensions directory. Typically, this path is
C:\xampp\php\ext.
4. Modify the PHP Configuration File
To load the MongoDB extension, you need to add it to the PHP configuration file (php.ini).
- Open the
php.inifile located in theC:\xampp\phpdirectory using a text editor (e.g., Notepad++ or VS Code). - Search for the section where other extensions are enabled (look for lines starting with
extension=). - Add the following line to this section:
extension=php_mongodb.dll - Save and close the
php.inifile.
5. Restart Apache
For the changes to take effect, you need to restart the Apache server.
- Open the XAMPP Control Panel.
- Stop the Apache server by clicking the "Stop" button next to Apache.
- Start the Apache server again by clicking the "Start" button.
6. Verify the Installation
To verify that the MongoDB extension is installed correctly:
- Open the
info.phpfile again in your browser by navigating tohttp://localhost/info.php. - Look for the MongoDB section in the output. If the installation was successful, you will see a section titled
mongodbwith information about the MongoDB extension.
Conclusion
By following these steps, you should have successfully installed the MongoDB extension for PHP in your XAMPP environment on Windows. This allows you to connect and interact with MongoDB databases from your PHP applications. Happy coding!