Install MongoDB on Ubuntu 16.04 | 17.10 | 18.04 with Nginx, PHP-FPM 7.2 Support

For developers using Nginx and PHP with MongoDB database in the backend, MongoDB PHP drivers will be required to enable PHP support… Without the drivers, PHP based applications and website using MongoDB will be able to interact with the database….
This brief tutorial shows students and new users how to install MongoDB with Nginx 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.
The steps below should be a great place to start to get MongoDB working with Nginx and PHP 7.2 support..
When you’re ready to get MongoDB setup on Ubuntu with Nginx and PHP support, continue with the steps below:
Step 1: Install Nginx HTTP Server
To install Nginx HTTP on Ubuntu server, run the commands below…
sudo apt update sudo apt install nginx
After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always start up with the server boots.
sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service
To test Nginx setup, open your browser and browse to the server hostname or IP address and you should see Nginx default test page as shown below.. When you see that, then Nginx is working as expected..

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://keyserver.ubuntu.com:80 --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 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
STEP 3: INSTALL MONGODB
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
Docs: https://docs.mongodb.org/manual
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-FPM 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-fpm php7.2-common php7.2-mongodb php-pear php7.2-dev
After installing PHP 7.2-FPM, run the commands below to open PHP default config file for Nginx…
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 "extension=mongodb.so" >> /etc/php/7.2/fpm/php.ini sudo echo "extension=mongodb.so" >> /etc/php/7.2/cli/php.ini
Step 6: Configure Nginx to enable PHP-FPM Support
If you have a custom site file, then edit it to enable Nginx PHP support… Or run the commands below to open Nginx default site configuration file
sudo nano /etc/nginx/sites-available/default
Then uncomment or remove the # symbol on the highlighted lines below to enable Nginx PHP support.
server { listen 80; listen [::]:80; root /var/www/html; index index.php index.html index.htm; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } }
Restart Nginx and PHP-FPM services
sudo systemctl restart nginx.service sudo systemctl restart php7.2-fpm.service
To verify the MongoDB PHP driver is installed and enable, continue below…
Create a phpinfo.php file in Nginx 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…

That’s it!
You may also like the post below:
root@c-handler:~# apt-get install php7.2-mongodb
Reading package lists… Done
Building dependency tree
Reading state information… Done
Package php7.2-mongodb is a virtual package provided by:
php-mongodb 1.5.3-1+ubuntu18.04.1+deb.sury.org+10 [Not candidate version]
php-mongodb 1.3.4-1build1 [Not candidate version]
E: Package ‘php7.2-mongodb’ has no installation candidate
This is the error I’m getting when trying to install php7.2-mongodb package. How do I resolve this?
how to php mongodb connection
Thanks a lot….