How to install MongoDB with Apache and PHP on Ubuntu Linux

|

,

|

The post provides a guide on installing MongoDB on Ubuntu Linux with Apache and PHP support, primarily for students and new users. It narrates the step-by-step installation process of Apache2 HTTP server, MongoDB, and PHP 7.4 on Ubuntu. It also covers how to install MongoDB PHP drivers and their execution, with necessary commands listed for…

This post shows students and new users how to install MongoDB on Ubuntu with Apache and PHP support.

MongoDB, a free open source, NoSQL, High-performance, and document-oriented database, can be a robust website and application.

Installing MongoDB with Apache and PHP on Ubuntu Linux provides a powerful combination for developing web applications. MongoDB is a document-oriented NoSQL database with high performance and scalability, making it an excellent choice for web applications requiring flexible data structures. Apache, on the other hand, is a widely used HTTP server that provides a robust platform for hosting web applications.

PHP is a popular server-side scripting language for building dynamic web pages and applications. By combining these technologies, developers can create scalable, high-performance web applications that handle large amounts of data.

Additionally, Ubuntu Linux is an accessible operating system that developers widely use due to its ease of use and extensive community support.

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

How to install Apache on Ubuntu Linux

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

sudo apt update
sudo apt install apache2

After installing Apache2, the commands below can stop, start, and enable the 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 the Apache2 setup, open your browser and browse to the server hostname or IP address. You should see the Apache2 default test page, as shown below.

http://localhost

How to install MongoDB on Ubuntu Linux

We have written a great tutorial on installing and configuring MongoDB on Ubuntu Linux. To read that post, click on the link below:

How to install and configure MongoDB on Ubuntu Linux

After reading the post above and installing MongoDB on Ubuntu, continue with the setup below.

How to install PHP on Ubuntu Linux

PHP 7.4 may not be available on Ubuntu default repositories. Therefore, to install it, you must get it from third-party repositories.

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

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

Then update and upgrade to PHP 7.4

sudo apt update

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

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

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

How to 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 "extension=mongodb.so" >> /etc/php/7.4/apache2/php.ini

Restart Apache2

sudo systemctl restart apache2.service

To verify the MongoDB PHP driver is installed and allowed, continue below.

Create a phpinfo.php file in the 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

http://localhost/phpinfo.php

You should see the PHP default test page.

That’s it!

This post showed you how to install MongoDB on Ubuntu Linux with Apache and PHP support. Please use the comment form below if you find any errors above or have something to add.

Like this:



8 responses to “How to install MongoDB with Apache and PHP on Ubuntu Linux”

  1. Amin Avatar
    Amin

    thank you,very well.

  2. amirsuhail Avatar
    amirsuhail

    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.

  3. Prithvi Avatar
    Prithvi

    This is the best link that I could follow

  4. CHINTAN Avatar
    CHINTAN

    I’m getting an error: sudo : pecl : command not found

  5. Pratish Avatar
    Pratish

    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 :-
    <?php
    $connection = new MongoClient();
    echo "COnnected to MONGODB”;
    $db=$m->mydb;
    echo “database selected”;
    ?>
    can u please help me…thanks in advance

  6. DJ Avatar
    DJ

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

  7. anand Avatar
    anand

    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

  8. Annycoder Avatar
    Annycoder

    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
    bye
    But while I am going to connect with PHP7.2 using below code:
    mydb;
    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?

    Thanks.

Leave a Reply to Amin Cancel 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.