rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

Install MongoDB on Ubuntu with PHP


* PLEASE NOTE *
Consider using Docker instead!

Because the mongodb-10gen package is not included in the default source (maybe one day it will be) you need to get and add the public key to your source. To import the 10gen public GPG Key, simply run the following command:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Now, to make sure the Ubuntu package management tool understands and is able to get it, you need to create a list file and include the debian package URL.

sudo -s
echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" > /etc/apt/sources.list.d/10gen.list

Now, update your repository

sudo apt-get update

and install

sudo apt-get install mongodb-10gen

You can always modify the config file based on your needs. The file is located at /etc/mongodb.conf. If you change the config file, you will have to restart the service using this command:

sudo service mongodb restart

To test the installation, in the shell simply run:

mongo

You can run all the MongoDB commands like:

>show dbs
local	(empty)

>db.version()
2.2.2

>db.stats()
{
	"db" : "test",
	"collections" : 0,
	"objects" : 0,
	"avgObjSize" : 0,
	"dataSize" : 0,
	"storageSize" : 0,
	"numExtents" : 0,
	"indexes" : 0,
	"indexSize" : 0,
	"fileSize" : 0,
	"nsSizeMB" : 0,
	"ok" : 1
}

Install MongoDB with PHP

To install MongoDB with PHP you will need the pear package. If this package is not installed, you will need to run:

apt-get install php-pear

Now, install the MongoDB php module:

sudo pecl install mongo

This will output something like:

Build process completed successfully
Installing '/usr/lib/php5/20090626+lfs/mongo.so'
install ok: channel://pecl.php.net/mongo-1.3.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongo.so" to php.ini

Add it to the apache config:

sudo vi /etc/php5/apache2/php.ini

or

sudo vi /etc/php5/apache2/conf.d/mongo.ini

Then restart apache:

sudo /etc/init.d/apache2 restart

Done!