- Establish a connection to a MongoDB instance from a PHP script using MongoDB\Driver\Manager.
- Supports both local and cloud-based MongoDB server
1. Install Prerequisites
Before installing the PHP MongoDB driver on Ubuntu, ensure that you have the necessary tools and dependencies:
- PHP PHP 7.4 or above
- PHP-dev and other necessary development libraries.
Run the following commands to install the required dependencies:
apt update
apt install php php-cli php-dev php-pear libssl-dev pkg-config
This installs PHP, the PHP development tools, and the required libraries for compiling extensions.
2. Download MongoDB PHP Driver (1.7.4)
You can download the MongoDB PHP driver 1.7.4 directly from the PECL website or use wget to download it.
wget https://pecl.php.net/get/mongodb-1.7.4.tgz
This will download the .tgz archive file of the MongoDB driver.
3. Extract the Driver
Once the file is downloaded, you need to extract it to a directory:
tar -xvzf mongodb-1.7.4.tgz
cd mongodb-1.7.4
4. Prepare for Installation
Before installing the driver, you need to prepare the PHP extension using phpize. This command will set up the environment for compiling the driver:
phpize
This command prepares the necessary files for compilation.
5. Configure the MongoDB Driver
Next, run the ./configure command to check your system and set up the MongoDB extension for your PHP version:
./configure
If everything goes well, the ./configure step will complete without errors. If you encounter any issues, make sure all dependencies were installed properly.
6. Compile and Install the Driver
Now, compile the extension using the make command, then install it with make install:
make
sudo make install
This step compiles and installs the MongoDB driver extension for PHP.
7. Enable the MongoDB Extension
After installation, you need to enable the MongoDB extension in PHP. You do this by editing your php.ini file.
1. Find the location of your php.ini file. You can do this by running the following command:
php --ini
It will show the path to the loaded php.ini file (for example: /etc/php/7.4/cli/php.ini).
2. Open the php.ini file for editing:
vim /etc/php/7.4/cli/php.ini
3. Add the following line at the end of the php.ini file to load the MongoDB extension:
extension=mongodb.so
8. Restart PHP or Web Server
After updating php.ini, you need to restart PHP (or your web server if you’re running PHP-FPM or Apache).
If you’re using PHP-FPM:
sudo systemctl restart php8.2-fpm # Replace 8.2 with your PHP version
If you’re using Apache with mod_php:
sudo systemctl restart apache2
9. Verify the Installation
To ensure the MongoDB extension is correctly installed, you can run:
php -m | grep mongodb
If the installation was successful, this will output mongodb.
If you’re facing issues or want a seamless, optimized setup, our expert team is here to assist. With our comprehensive Server Management Services, we handle everything from package installation to database configuration and performance tuning—ensuring your environment runs smoothly and securely. Whether you’re installing the PHP MongoDB Driver on Ubuntu for the first time or need advanced troubleshooting, our certified engineers are available 24/7 to support your server needs.