This post shows students and new users steps to install MongoDB on Ubuntu with Apache and PHP support. 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 Apache and PHP.
MongoDB packages are included in Ubuntu default repositories, however, the versions in Ubuntu repositories aren’t the latest. In order to install the latest, you will have to install MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.
Also, for students and new users learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.
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 Apache2 HTTP server on Ubuntu, 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.
How to install MongoDB on Ubuntu Linux
We have written a great tutorial on how to install and configure MongoDB on Ubuntu Linux. To read that post, click on the link below:
After reading the post above and installing MongoDB on Ubuntu, continue below with the setup.
How to install PHP on Ubuntu Linux
PHP 7.4 may not be 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.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 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
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.
This post showed you how to install MongoDB on Ubuntu Linux with Apache and PHP support. If you find any error above or have something to add, please use the comment form below.