Tuesday, June 18, 2024

Installing MongoDB Extension in XAMPP on Windows

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.

  1. Open the XAMPP Control Panel.
  2. Start Apache.
  3. Click on the "Admin" button next to Apache.
  4. A web browser will open. Create a new PHP file (e.g., info.php) in the htdocs directory with the following content:

    <?php phpinfo(); ?>
  5. Open this file in your browser by navigating to http://localhost/info.php.
  6. 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.

  1. Go to the MongoDB PHP driver GitHub releases page.
  2. 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.zip file (where ts stands for Thread Safe and x64 for 64-bit).
  3. 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.

  1. Navigate to the extracted files.
  2. Copy php_mongodb.dll and php_mongodb.pdb.
  3. 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).

  1. Open the php.ini file located in the C:\xampp\php directory using a text editor (e.g., Notepad++ or VS Code).
  2. Search for the section where other extensions are enabled (look for lines starting with extension=).
  3. Add the following line to this section:
    extension=php_mongodb.dll
  4. Save and close the php.ini file.

5. Restart Apache

For the changes to take effect, you need to restart the Apache server.

  1. Open the XAMPP Control Panel.
  2. Stop the Apache server by clicking the "Stop" button next to Apache.
  3. Start the Apache server again by clicking the "Start" button.

6. Verify the Installation

To verify that the MongoDB extension is installed correctly:

  1. Open the info.php file again in your browser by navigating to http://localhost/info.php.
  2. Look for the MongoDB section in the output. If the installation was successful, you will see a section titled mongodb with 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!