Install MongoDB with Apache2, PHP 7.2 Support on Ubuntu 16.04 | 17.10 | 18.04

This brief tutorial shows students and new users how to install MongoDB with Apache2 and PHP 7.2 support on Ubuntu 16.04 / 17.10 and 18.05 LTS servers… MongoDB, a free open source, NoSQL, High-performance, schema-free document-oriented database can be used to create powerful websites and applications.

If you’re going to be developing an application or a website that requires MongoDB as its backend database server, the steps below should be a great place to start to get MongoDB working with Apache2 and PHP 7.2 support..

When you’re ready to get MongoDB setup on Ubuntu with Apache2 and PHP support, continue with the steps below:

Step 1: Install Apache2 HTTP Server

To install Apache2 HTTP on Ubuntu server, run the commands below…

sudo apt update
sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to always start up with the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

To test Apache2 setup, open your browser and browse to the server hostname or IP address and you should see Apache2 default test page as shown below.. When you see that, then Apache2 is working as expected..

apache2 ubuntu install

Step 2: Add MongoDB Repository

In order to get the latest version of MongoDB, you must add its repository to Ubuntu.. to do that, run the commands below to add the official repository key.

sudo apt-key adv --keyserver hkp:// --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

After adding the repository key to Ubuntu, run the commands below to add MongoDB repository to your system…

echo "deb [ arch=amd64,arm64 ] xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list


Now that the repository and key have been added to Ubuntu, run the commands below to install the package.

sudo apt update
sudo apt install mongodb-org mongodb-org-server

By default, MongoDB listens on port 27017… after installing, the local server should be able to communicate with MongoDB.. to verify whether MongoDB is running and active, run the commands below:

sudo systemctl start mongod
sudo systemctl status mongod

You should see something like the lines below:

richard@ubuntu1604:~$ sudo systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-01-27 08:53:42 CST; 13min ago
 Main PID: 2383 (mongod)
    Tasks: 23
   Memory: 60.7M
      CPU: 2.613s
   CGroup: /system.slice/mongod.service
           └─2383 /usr/bin/mongod --config /etc/mongod.conf

To connect to MongoDB shell, run the commands below:

Step 4: Install PHP 7.2 on Ubuntu

PHP 7.2 isn’t available on Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.2

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.2

sudo apt update

Next, run the commands below to install PHP 7.2 and related modules.

sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-cli php7.2-mongodb php-pear php7.2-dev

After installing PHP 7.2, run the commands below to open PHP default config file for Apache2…

Step 5: Install MongoDB PHP Drivers

MongoDB PHP drivers provide exceptionally thin glue between MongoDB and PHP… run the commands below to install it.

sudo pecl install mongodb

After that, run the commands below to enable the drivers

sudo bash
sudo echo "" >> /etc/php/7.2/apache2/php.ini

Restart Apache2

sudo systemctl restart apache2.service

To verify the MongoDB PHP driver is installed and enable, continue below…

Create a phpinfo.php file in Apache2 root directory by running the commands below

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.. then browse to your server hostname followed by /phpinfo.php


You should see PHP default test page…

MongoDB Ubuntu

That’s it!

You may also like the post below:

8 Replies to “Install MongoDB with Apache2, PHP 7.2 Support on Ubuntu 16.04 | 17.10 | 18.04”

  1. how to connect php7 in mongodb in ec2 ubuntu 16.04?
    means i wanna create a website for that i want to use mongo as db for this how would i connect it in php coding.

  2. In my case info.php “mongodb” section is showing exactly same as above. But when i run a simple php connectivity config file it doesn’t run well.
    My config.php file contain these codes :-
    $connection = new MongoClient();
    echo "COnnected to MONGODB”;
    echo “database selected”;
    can u please help me…thanks in advance

  3. amazing blog for ubuntu users. i have successfully install all things properly.
    thanks for this

  4. Please change php-7.2-cli to php7.2-cli. (Without an hyphen between php & 7.2)

    sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php-7.2-cli php7.2-mongodb php-pear php7.2-dev

  5. I have installed MongoDB check status:
    ● mongodb.service – An object/document-oriented database
    Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2019-08-19 20:25:31 IST; 59s ago
    Docs: man:mongod(1)
    Main PID: 25309 (mongod)
    Tasks: 23 (limit: 4915)
    CGroup: /system.slice/mongodb.service
    └─25309 /usr/bin/mongod –unixSocketPrefix=/run/mongodb –config /etc/mongodb.conf

    Aug 19 20:25:31 user systemd[1]: Started An object/document-oriented database.

    After that I have create DB and insert one record for test result are as below:
    > db.user.findOne();
    “_id” : ObjectId(“5d5a969006e247a47fe2dc88”),
    “name” : “my_name”,
    “age” : 2
    > exit
    But while I am going to connect with PHP7.2 using below code:
    echo “Database mydb selected”;

    Gives me an error :

    PHP Fatal error: Uncaught Error: Class ‘MongoClient’ not found in /var/www/html/mongo/mongo.php:3
    Stack trace:
    #0 {main}
    thrown in /var/www/html/mongo/mongo.php on line 3

    Could you please help out me?


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.