5/5 - (7 votes)

Last Updated on August 22, 2022 by Vikash Ekka

How to install mongodb extension for PHP - vetechno
How to install mongodb extension for PHP – vetechno
In this tutorial guide, we will learn how to install MongoDB driver / extensions for PHP on Ubuntu and CentOS.
Assuming you have installed PHP with all required extension. If you have not installed so far then check this link.

 
How to Uninstall PHP, Apache and MySQL on Ubuntu 20.04 LTS 

What is PHP MongoDB driver ?

MongoDB driver is a PHP extension that manages the connection to the MongoDB server and enables you to perform all kinds of operations on a NoSQL database through PHP.

Same steps will also work on Ubuntu 20.04, 18.04 and CentOS 7, 8.

How to install mongodb extension for PHP on Ubuntu 22.04 LTS.

NOTE:- In my system I have installed php7.4 version

Step1.  Update the apt repository

sudo apt update
Step 2. Install MongoDB PHP driver on Linux
sudo pecl install mongodb
Installing the driver via PECL command will use bundled versions of » libbson and » libmongoc and attempt to automatically configure them.
Step 3. Adding the following line in two locations to your php.ini file: 
extension=mongodb.so
sudo echo "extension=mongodb.so" >> /etc/php/7.4/cli/php.ini
sudo echo "extension=mongodb.so" >> /etc/php/7.4/fpm/php.ini
Step 4. Now Restart the Web Server (Nginx or Apache2)
sudo service nginx restart

Verify MongoDB Extension on Ubuntu 22.04

To verify the installation, we can run phpinfo() function. 
Create a phpinfo.php file in /var/www/html/ root directory by running the commands below;

sudo echo "<?php phpinfo( ); ?>" >> /var/www/html/phpinfo.php
Access phpinfo.php file from web interface to verify mongodb extension


Enter the server IP address or domain name along /phpinfo.php in the browser URL address to execute
php via web interface.

 

 
How to install mongodb extension for PHP - vetechno
mongodb extension for PHP

 

 
Verify from the command line you can run php -m command to get information on MongoDB driver.
php -i | grep mongo
php -m

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of https://www.vetechno.in. I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

3 thoughts on “How to install mongodb extension for PHP – vetechno”

Leave a Reply

Your email address will not be published. Required fields are marked *