Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setup mongoDB-extension for php7

I'm trying to install the mongoDB-extension for php7. I started with a brand new installation of Ubuntu-Server 16.04.1. Pre-installed (while OS-installation) packages were LAMP-extension, OpenSSL and MySQL-server. This was installed on a local VirtualBox. It shall to be a development system which is configured equal for every developer.

First I installed MongoDB itself according to these instructions: install mongoDB on Ubuntu

After that, according to this second instruction: installing php7-mongoDB-client-driver,

I installed

  • php-pear
  • phpize

It can be recapped in

$sudo apt-get install php-pear phpize

because these two packages were flagged as missing when using

sudo pecl install mongodb

the first time.

Then I was suggested to install openssl-extension. About that I found an instruction which told me to execute these 3 commands:

  • $sudo apt-get install libssl-dev
  • $sudo apt-get install libcurl4-openssl-dev
  • $sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev

(the first package was already installed on my system)

after that

$php -m

should show "openssl" as a single point on the list.

Then I could finally execute

$sudo pecl install mongodb

This installation displayed four instructions at the end:

<Installing '/usr/lib/php/20151012/mongodb.so'>
<install ok: channel://pecl.php.net/mongodb-1.1.8>
<configuration option "php_ini" is not set to php.ini location>
<You should add "extension=mongodb.so" to php.ini>

So I executed:

$php -i | grep extension_dir

which should show up the same path as displayed in the first line.

$pear config-set php_ini /etc/php5/apache2/php.ini
$pecl config-set php_ini /etc/php5/apache2/php.ini

to follow the instructions of the third line.

Added extension=mongodb.so to phpini at /etc/php/7.0/apache2/ to conform to line four.

And finally, after restarted apache2,

$php -i

should display the same version of mongodb similar in the second line.

After all of these steps I found one last package to install which is

$sudo apt-get install php-mongodb

But also this this didn't help.

To check if the extension is working I used either:

$m = new MongoClient();  OR

$m = new Mongo\Driver\Manager();

and then echo $m. I read that Mongo\Driver\Manager is the new version but even with this code it doesn't work. It always tells me that the class "could not be found". Irrespective of the class I tried.

I hope this (hopefully) complete instruction can help someone with a similar problem about mongodb and php.

Additionally I hope that these information can help you to give me a hint what else I can do to fix that problem. Unfortunately I searched a long time but didn't found something to include this extension into this php-version. There where only part but I cant build them together.

like image 995
Danaq Avatar asked Aug 15 '16 21:08

Danaq


People also ask

What is MongoDB extension?

MongoDB driver ¶ This extension is developed atop the » libmongoc and » libbson libraries. It provides a minimal API for core driver functionality: commands, queries, writes, connection management, and BSON serialization.

Can I connect PHP with MongoDB?

You can add the driver to your application to work with MongoDB in PHP. The MongoDB PHP Driver consists of the two following components: The extension , which provides a low-level API and mainly serves to integrate libmongoc and libbson with PHP.

What is a MongoDB driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.


2 Answers

When using PHP 7+ install new Mongo PHP Adapter

 composer config "platform.ext-mongo" "1.6.16" && composer require alcaeus/mongo-php-adapter

Then install mongodb with PECL

pecl install mongodb

And if you installed correctly the mongodb

$ sudo nano /etc/php/7.0/apache2/php.ini

Add the following line in the file:

extension = mongodb.so;

or with this command

echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
like image 128
Strahinja90bg Avatar answered Oct 05 '22 00:10

Strahinja90bg


For docker, I just added below line to the Dockerfile:

RUN pecl install mongodb && docker-php-ext-enable mongodb

or

pecl install mongodb-1.5.0

docker-php-ext-enable mongodb
like image 28
Amin Shojaei Avatar answered Oct 05 '22 01:10

Amin Shojaei