Easiest way to install Mongodb PHP extension in Ubuntu 13.10 (saucy)?
Asked Answered
B

4

20

I'm using Ubuntu 13.10 (saucy salamander) and I was hoping that sudo apt-get install php5-mongo would be enough to get the mongodb database driver installed.

No such luck though. I'm using php5-fpm, so firstly I found I also needed to do php5enmod mongo but even with that, I get a error:

include(MongoClient.php): failed to open stream: No such file or directory

Instead, to install I have to do the following (which uses a lot more disk space):

 sudo apt-get install php5-dev make php-pear
 sudo pecl install mongo
 sudo echo "extension=mongo.so" | tee /etc/php5/mods-available/mongo.ini

My question is why isn't sudo apt-get install php5-mongo enough? Is it a problem with the Ubuntu repo? How can I look into the ubuntu repositories and find what version it uses or why MongoClient.php isn't included. I'm using the "ubuntu:saucy" docker image as my base and it includes universe by default I think.

Byte answered 17/3, 2014 at 15:58 Comment(3)
You don't have to include a PHP script to use MongoDB. Open a phpinfo() and verify your module got loaded. Keep in mind that different PHP environments use different php.inis. (/etc/php5/fpm/php.ini)Involucel
apt-get isn't enough because every PHP setup is differentPuff
also php5-mongo is probably a really old version of the driver, you should be using pecl install mongoPuff
B
35

In Ubuntu 14.04, sudo apt-get install php5-mongo results in a fully working mongo PHP extension. So I guess there is just a packaging problem in Ubuntu 13.10 which causes the problem.

Solution: Use Ubuntu 14.04 LTS instead of Ubuntu 13.10.

But, if you have to use Ubuntu 13.10, don't use the php5-mongo Ubuntu package, instead install the mongo extension via pecl:

sudo apt-get install php5-dev make php-pear
sudo pecl install mongo
sudo echo "extension=mongo.so" | sudo tee /etc/php5/mods-available/mongo.ini
Byte answered 1/5, 2014 at 11:57 Comment(2)
This line sudo echo "extension=mongo.so" | tee /etc/php5/mods-available/mongo.ini fails for me. It says permission denied when executing tee However I changed it to this sudo echo "extension=mongo.so" | sudo tee /etc/php5/mods-available/mongo.iniGoogly
If you want this module to be loaded in both apache and cli you'd the need to symlink it properly. ln -s /etc/php5/mods-available/mongo.ini /etc/php5/cli/mongo.ini and another symlink for apacheSelfloading
T
34

The easiest to install the mongoDB driver for php5 in ubuntu is using the command :

sudo apt-get install php5-mongo

Attention, the driver is correctly installed but not loaded yet, so should absolutly restart the server, if using apache should do :

sudo service apache2 restart
Tigress answered 10/12, 2014 at 10:50 Comment(1)
Thanks but while your instructions would work on todays Ubuntu, they won't work on Ubuntu 13.10 (saucy).Byte
S
3

There is also a good way to install the mongo DB for PHP. I have ubuntu 14.04LTS.

open synaptic package manager and search "mongo"

select these packages to install.

  • php-horde-mongo
  • php-mongo

synaptic package manager

click apply to install the packages.

Now restart you apache2 using this command.

sudo service apache2 restart

Test the mongo

create the PHP file with name mongo-test.php and paste this code there.

<?php
   // connect to mongodb
   $m = new MongoClient();

   echo "Connection to database successfully";
   // select a database
   $db = $m->mydb;

   echo "Database mydb selected";
?>

test this URL http://localhost/mongo-test.php

Saturninasaturnine answered 10/12, 2015 at 19:31 Comment(2)
On Ubuntu 15.04, I used Software Center to find php-horde-mongo and php5-mongo. After installation, I restarted the Apache. And it worked perfectly. Thanks for providing the test code as well :)Barrus
welcome. Synaptic and software center used the same repository for packages. Synaptic is more advanced tool while Software center is simple and easy.Saturninasaturnine
W
2

This seems to be a packaging problem in Ubuntu.

Did you install PHP5-fpm via apt-get too? It looks like there is a disconnect between how you installed PHP5 and which php.ini file the php5-mongo package is trying to update.

As for the include(MongoClient.php): failed to open stream error; it is because of your autoloader. When you do new MongoClient when the extension is not enabled it hits your autoloaded which tries to include a file called <classname>.php

Westbrook answered 8/4, 2014 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.