How to install MongoDB with Nginx and PHP on Ubuntu Linux

|

,

|

The post guides students and developers through the process of installing MongoDB on Ubuntu Linux with Nginx and PHP support. It includes steps for installing Nginx, MongoDB, PHP, and MongoDB PHP drivers to facilitate interaction between PHP-based applications and MongoDB. The post also provides instructions for configuring Nginx to enable PHP support and verifying the…

This post shows students and new users steps to install MongoDB on Ubuntu Linux with Nginx and PHP support.

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

Installing MongoDB with Nginx and PHP support on Ubuntu Linux can provide developers with a robust and scalable platform to build web applications and websites. MongoDB is a free open-source, NoSQL, high-performance, and schema-free document-oriented database that can be an excellent choice for storing and retrieving data.

Nginx is a popular web server for serving static and dynamic content, and PHP is a popular server-side scripting language for building dynamic web applications. When used together, MongoDB, Nginx, and PHP can provide an efficient and powerful platform for building web applications.

Ubuntu Linux is an accessible and user-friendly operating system that is easy to use, making it a popular choice for beginners and experienced developers.

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

How to install Nginx on Ubuntu Linux

To install Nginx HTTP on the 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 the 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 the Nginx setup, open your browser and browse to the server hostname or IP address. You should see the Nginx 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 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 isn’t available on Ubuntu’s default repositories. So, 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.2 and related modules.

sudo apt install php7.4-fpm php7.4-common php7.4-mongodb php-pear php7.4-dev

After installing PHP 7.4-FPM, run the commands below to open the PHP default config file for Nginx…

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/fpm/php.ini
sudo echo "extension=mongodb.so" >> /etc/php/7.4/cli/php.ini

Configure Nginx to enable PHP support

If you have a custom site file, edit it to enable Nginx PHP support. Or run the commands below to open Nginx’s 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.

Also, add the highlighted index.php to the index line.

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.4-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 enabled, continue below.

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

http://localhost/phpinfo.php

You should see the PHP default test page…

That’s it!

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


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



3 responses to “How to install MongoDB with Nginx and PHP on Ubuntu Linux”

  1. Deepak Avatar
    Deepak

    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?

  2. sagar Avatar
    sagar

    how to php mongodb connection

  3. Ankit Avatar
    Ankit

    Thanks a lot….

Leave a Reply to AnkitCancel reply

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

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading