How To Configure MongoDB for PHP on Windows and Ubuntu

By bhagwatchouhan
How To Configure MongoDB for PHP on Windows and Ubuntu

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source, and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to configure MongoDB for PHP on Windows 10 and Ubuntu 20.04 LTS.

 

Prerequisites

 

PHP is already installed on the system. You can follow How To Install PHP On Windows, How To Install PHP 7 On Windows, How To Install XAMPP On Windows 10, and How To Install WampServer on Windows, and  How To Install PHP 8 On Ubuntu 20.04 LTS. Also, the PHP path is set on the Windows system as mentioned in the PHP installation tutorials How To Install PHP On Windows and How To Install PHP 7 On Windows.

 

Web Server is already installed on the system. You can follow How To Install XAMPP On Windows 10, How To Install WampServer on Windows, How To Install Apache 2 On Windows, and How To Install Apache 2 On Ubuntu 20.04 LTS to install Apache Web Server.

 

MongoDB is already installed on the system. You can follow How To Install MongoDB on Windows, How To Install MongoDB on Ubuntu Desktop, and How To Install MongoDB on Ubuntu.

 

Configure MongoDB for PHP on Windows

 

Download the compatible .DLL file for MongoDB based on your current PHP version and update the PHP .ini file configured on the system path.

 

In my case, it was PHP 7.4 installed on Windows 10 using WampServer, hence I have downloaded the thread-safe version of php_mongodb.dll from the official website and copied it to the PHP extension directory wamp64\bin\php\php7.4.9\ext. Also, I have configured the .ini file by appending the below-mentioned line at last.

 

# MongoDB Extension
extension=php_mongodb.dll

 

Now restart your Web Server and confirm whether the extension is enabled as shown in Fig1 for WampServer.

 

Configure MongoDB for PHP on Windows and Ubuntu - Wampserver Configuration

Fig 1

 

Configure MongoDB for PHP on Ubuntu

 

Install the PECL extension for MongoDB based on your current PHP version and update the PHP .ini file for CLI and Web Server.

 

# Install PECL if the next command throws an error
sudo apt install php-pear php-dev

# Install MongoDB Extensions
sudo pecl install mongodb

# Output
Build process completed successfully
Installing '/usr/lib/php/20200930/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.10.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini

# Update PHP .ini - Web Server
sudo echo "extension=mongodb.so" >> /etc/php/7.4/apache2/php.ini

# Update PHP .ini - CLI
sudo echo "extension=mongodb.so" >> /etc/php/7.4/cli/php.ini

// Restart Web Server
sudo service apache2 restart

 

Confirm MongoDB for PHP

 

Also, check the output of phpinfo function. It should show MongoDB as shown in Fig 2.

 

Configure MongoDB for PHP on Windows and Ubuntu - Verify MongoDB

Fig 2

 

Summary

 

This tutorial provided the steps required to configure MongoDB for PHP on both Windows and Ubuntu systems.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan