
Hi all…In this tutorial lets get into configuring MongoDB with PHP using Apache Server that allows you to build awesome websites using MongoDB server as database engine.
Step 1: Installation of Apache Server in Ubuntu:
To get started we first need Apache Server installed in our machine. Open a terminal and enter the following command.
sudo apt install apache2
Step 2: Starting Apache Server and Verifying installation
sudo systemctl start apache2.service
Now open a browser and type as http://localhost
Now you will see the Apache Homepage as given below.

Step 3: Adding Repositories for PHP 7.2
Since Ubuntu does not have PHP in the default repos, we add it to get PHP from third party repositiories. So run the following commands in a terminal to add PHP repo.
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update
Step 4: Installing PHP and Dev Libraries:
Our next task is to install PHP and other development llibraries needed.Run the following command in a terminal.
sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-cli php7.2-mongodb php-pear php7.2-dev
Step 5: Instaling PHP-MongoDB Driver using PECL
So we have configured PHP driver with Apache and our next task is to install driver that connects PHP with MongoDB Server.Run the given command in a terminal.
sudo pecl install mongodb
Step 6: Restart Apache Server to make changes into effect:
Now restart your Apache Server to apply integrate MongoDB Driver with PHP.
Note: You may get some warnings and you can ignore it.!!
sudo systemctl restart apache2.service
Step 7: Checking the Integration:
Open up a terminal and type the following command which will list the modules installed with PHP. Here you can find mongodb listed.
php -m

Step 8: Adding PHP files to perform CRUD Operations
Now you can add your PHP files in the following repository to get executed using apache server.Change your working directory:
cd /var/www/html/
Now you can create files using nano or gedit or vim .
nano mongodb.php
Here you can add your PHP code and save it and run it in a browser by entering the url as follows
http://localhost/mongodb.php
To get a tutorial on how to perform CRUD operations please visit my previous post at
https://mohantk.home.blog/2019/11/02/performing-crud-operations-in-mongodb-using-php/
Please feel free to get in touch if you face any errors or struggle somewhere in the configuration. Do comment if you find something easy as compared to this tutorial.