Install and Configure MongoDB with PHP on Windows 🌱

Adding PHP Support for MongoDB

  1. Open Windows Explorer and navigate to the PHP installation directory
  2. Hold the SHIFT key and right click in the white space > Open PowerShell window here...
  3. Run the following command to output the PHP version information
    .\php.exe -v
  4. Note the architecture (x86 or x64) and the PHP version
  5. Open a web browser and navigate to the MongoDB PHP driver page Download
  6. Click the Windows DLL link
  7. Select either the x86 or x64 to match the PHP installation. If using PHP with Apache or nginx download the Thread Safe version. If using PHP for IIS, download the Non-Thread Safe version
  8. Extract the downloaded .zip file
  9. Copy the .dll file from the extracted folder to the PHP installation /ext directory
  10. Find php.ini inside the PHP installation directory and edit it in a text editor
  11. Search for ;extension=
  12. At the bottom of the list of extensions, add the following line

    extension=mongodb

  13. Restart the web server to apply the change

Installing MongoDB on Windows

  1. Download MongoDB Community Server Download
  2. Extract the downloaded .zip file
  3. Rename the bin folder inside the extracted folder MongoDB
  4. Cut the MongoDB folder to the location where it will be running from
  5. Once in place, navigate inside the MongoDB folder > Hold the SHIFT key and right click in the white space > Open PowerShell window here...
  6. Run the following command to install MongoDB as a service
    mkdir data
    .\mongod.exe --install --serviceName MongoDB --serviceDisplayName MongoDB --logpath ((Get-Item .).FullName+"\mongo.log") --dbpath ((Get-Item .).FullName+"\data")
    net start MongoDB

Testing PHP and MongoDB

  1. Download the mongodb_test.php Download
  2. Save mongodb_test.php on the root of the web server
  3. Open a web browser and navigate to http://DNSorIP/mongodb_test.php
  4. The page will create a new database called phpDemo, a collection named states, write some rows and then read them back and display them in a table

Install MongoDB Compass (Optional)

  1. Open a web browser and navigate to the MongoDB Compass GitHub latest release Link
  2. Download the win32-x64.zip file
  3. Extract the downloaded .zip file
  4. Move the extracted files to a folder where it will run from permanently (ie C:\Program Files\MongoDBCompass\)
  5. Run MongoDBCompass.exe
  6. Once the application loads, click the green Connect button to connect to the locally running MongoDB instance

For more information on MongoDB CRUD operations with PHP check out https://roytuts.com/mongodb-php-7-crud-example/