2012. július 30., hétfő

Installing MongoDB driver for PHP Ubuntu Server 12.04

This article is about connecting PHP with MongoDB.

Before you start, make sure you have apache2 and PHP5+ installed in your server:
$ sudo apt-get install apache2 php5 libapache2-mod-php5


You will also need to install some dependencies for the driver. Do it this way:
$ sudo apt-get install php-pear php5-dev


Okay, let's find this driver!
$ pecl search mongo


And install it:
$ sudo pecl install mongo


If you wouldn't have the make package installed, install and reinstall mongo:
$ sudo apt-get install make
$ sudo pecl install mongo


Now, let's hack that php.ini too, located at /etc/php5/apache2/ path. Do that by adding mongo.so extension somewhere after the extensions part:
$ sudo vim /etc/php5/apache2/php.ini

extension = mongo.so


Since I have run my database server on a separate MongoDB server, I have also changed to path to the database for the extension. I have added the following lines after MySQL block:
[Mongo]
; default host 127.0.0.1
; custom host: the IP address of the database server
mongo.default_host = "xxx.xxx.xxx.xxx"
Further infos at in the PHP - MongoDB documentation.

Installing MongoDB on Linux Ubuntu Server 12.04

For those who like to know how to install MongoDB on a clean Linux Ubuntu 12.04 install.
Note: this article is for any Ubuntu versions above 9.10, using upstart.


At first, you need to tell Ubuntu that the repository where you are going to download MongoDB is a secure one. Do that by typing this line in the terminal:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
I you are interrested about the 'apt-key' part, visit SecureApt.


Now you need to tell where to look for MongoDB. You will need to store this information in a file named 10gen.list, in the /etc/apt/sources.list.d/ directpory, so feel free to type in the terminal (I use vim):
$ sudo vim /etc/apt/sources.list.d/10gen.list
And the contents of the file will be:
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen


Now, let's update!
$ sudo apt-get update
And finally, here comes our target: MongoDB!
updated 'mongo' to 'mongo-10gen'
$ sudo apt-get install mongodb-10gen

Done.
Based on my experience, if you type in "mongo" now, it will not work. I rebooted the server and it was working like a charm. Try rebooting and then:
$ mongo
show dbs;
//local
//test