Install php mongodb extension centos 7

Pre-Flight Check

  • These instructions are intended specifically for installing the MongoDB PHP Driver (Extension) on CentOS 7.
  • I’ll be working from a Liquid Web Core Managed CentOS 7 server with HTTPD already installed, configured, and running, and I’ll be logged in as root.

Step 1: Setup Environment, Install PHP Extension & Application Repository (PEAR)

As a matter of best practice we’ll update our packages:

yum -y update

Then let’s install a compiler, the PHP Extension and Application Repository (PEAR), and PHP development tools:

yum -y install gcc php-pear php-devel

Step 2: Installation with the PHP Extension Community Library (PECL)

Now let’s install the MongoDB PHP driver (extension) with PECL:

pecl install mongo

Next you’ll be prompted regarding ‘MongoDB Enterprise Authentication’. If you’re not specifically using SASL (and already have it installed), then hit enter to continue (thus answering ‘no’):

Build with Cyrus SASL (MongoDB Enterprise Authentication) support? [no] :

Step 3:Configure PHP

For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor

vim /etc/php.ini

It is possible to place the following bit of code anywhere in the php.ini file. However, the most common location for extensions is in the Dynamic Extensions section.

Find Dynamic Extensions and insert the following in that section:

extension=mongo.so

Then exit and save the file with the command :wq .

And now we’ll restart Apache:

systemctl restart httpd

Step 4: Verify the Extension is Available

Now verify that the extension is available by using the following command:

php -m | grep -i mongo

Your result should be:

mongo

In order to use MongoDB in our PHP programs, we need to install php mongodb driver. It is a PHP extension that manages the connection to the MongoDB server and enables you to perform all kinds of operations on a NoSQL database through PHP.

In this tutorial we will see how to install and configure the mongodb php driver on Ubuntu, CentOS 7 and Microsoft Windows. As distributions and environments vary, installation instructions will also vary.

  • Install PHP MongoDB Driver on Ubuntu.
  • Install PHP MongoDB Driver on CentOS 7.
  • Install PHP MongoDB Driver on Windows 10/Windows Server 2016.
  • Test MongoDB Connection From PHP Script.

Install MongoDB PHP Driver on Ubuntu

MongoDB PHP Driver for Ubuntu provides by the php-mongodb package which we can install with apt-get command.

sudo apt-get update
sudo apt-get install php-mongodb

Then, restart the Apache Web Server:

sudo systemctl restart apache2.service

To verify the installation, we can run phpinfo() function. The phpinfo page should display mongodb details as shown below.

Install php mongodb extension centos 7

From the command line you can run php -i command to get information on mongodb driver.

php -i | grep mongo

Install PHP MongoDB Driver on CentOS 7

The php mongodb driver on CentOS 7 for PHP 5 available from the epel-repository.

First, enable the epel repository:

sudo yum -y install epel-release
sudo yum repolist

Install the php-mongodb package:

sudo yum -y install php-mongodb

Restart the HTTPD server:

sudo systemctl restart httpd

If you installed PHP 7 using webtatic repository, then the package name should be something like "php71w-pecl-mongodb". You can search available packages with yum search command.

yum search php | grep -i mongo

Selinux will block mongodb driver on CentOS 7. As a solution you can either disable Selinux or enable the 'httpd_can_network_connect_db' boolean.

sudo setsebool -P httpd_can_network_connect_db 1

Following is the sample PHP error caused by Selinux on CentOS 7:

PHP Fatal error: Uncaught MongoDB\\Driver\\Exception\\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on 'localhost:27017']

Install PHP MongoDB Driver on Windows 10/Windows Server 2016

Important: For Windows, You need to download correct mongodb driver for PHP, based on your PHP version, Architecture and whether Thread Safety is enabled. You can get those information from the phpinfo page.

Do the following steps to install and configure MongoDB driver on Windows XAMPP Server.

  1. Download the latest stable version of the PHP MongoDB driver from following URL https://pecl.php.net/package/mongodb.

    Install php mongodb extension centos 7

  2. Extract the archive File.
  3. Copy the php_mongodb.dll file from the extracted folder to the PHP extension directory. this is usually the "C:\xampp\php\ext" folder in XAMPP Server.
  4. Open the php.ini file inside your PHP installation(C:\xampp\php) and add the following line:
    extension=php_mongodb.dll
  5. Save the file and close it. Restart the Apache web server.

Test MongoDB Connection From PHP Script

Let's write a very simple PHP program that creates a connection to the MongoDB server and dump the connection status.

Add following PHP code to your php script and access from the Web browser.

The above PHP example will output something similar to:

object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(25) "mongodb://localhost:27017" ["cluster"]=> array(1) { [0]=> array(11) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(0) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["tags"]=> array(0) { } ["last_is_master"]=> array(0) { } ["round_trip_time"]=> int(-1) } } }

And that is mean, you have a connection to the MongoDB Server in your PHP script.