Install PHP 5, Apache 2 and MongoDB on Ubuntu 10.10 and test it out

Post to Twitter

With Ubuntu 10.10 recently launched (at the time of this article) many developers are upgrading to the latest and greatest Ubuntu to run their LAMP servers. Today I’m going create a LAMP server but without the traditional MySQL server and instead replacing it with MongoDB.


I created a clean install of Ubuntu 10.10 on VMWare Workstation. The VMWare free player will work as well. Once I had Ubuntu 10.10 installed and ran the patch update I proceeded to install Apache 2 from the terminal:

$ sudo apt-get install apache2

Next step, install PHP 5. Please note I’m installing some additional packages I’ve found useful – you don’t need to install all of these if you choose just be careful you don’t remove a dependent package, ex: php5-dev is needed for MongoDB. Pick and choose what you need.

$ sudo apt-get install php5 php5-dev libapache2-mod-php5 php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ps php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-xsl php5-common

Let’s create a new PHP file to ensure everything is working:

$ sudo touch /var/www/info.php
$ sudo pico /var/www/info.php

Add this code to the file:

<?php
	phpinfo();
?>

Restart Apache so all the prior changes get applied:

$ sudo /etc/init.d/apache2 restart

Now using a web browser point it to: http://localhost/info.php

You should see something like the following:

Updated: April 6, 2011 – changed the article to match recent changes to the MongoDB package install documentation located here. Refer to that page first as changes might have occurred since this article was updated.

Time to install MongoDB:

$ sudo pico /etc/apt/sources.list

Edit that file by adding the following line at the end:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Now we can install MongoDB and then begin to modify the PHP.ini file to add the MongoDB support:

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
$ sudo apt-get update
$ sudo apt-get install mongodb-stable
$ sudo pecl install mongo
$ sudo pico /etc/php5/apache2/php.ini

Add this line to the PHP.ini file (the path should be: /etc/php5/apache2/php.ini):

extension=mongo.so

Save the file.

Restart Apache:

$ sudo /etc/init.d/apache2 restart

If you re-run the info.php file you should see the MongoDB driver has been added:

Create a test file to ensure PHP and MongoDB are talking to each other:

$ sudo touch /var/www/mongotest.php
$ sudo gedit /var/www/mongotest.php

Here is the PHP code for the file (I modified the code from here):

<?php
	error_reporting(E_ALL);
	ini_set('display_errors', '1');

	header("Content-type: text/plain");

	$connection = new Mongo();
	$db = $connection->mydb;

	$collection = $db->mycollection;
	$myobj = array("first_name" => "John", "last_name" => "Doe", "age" => 30);
	$myobj2 = array("first_name" => "Jane", "last_name" => "Doe", "age" => 27);
	$collection->insert($myobj);
	$collection->insert($myobj2);

	$cursor = $collection->find();
	foreach($cursor as $result)
	{
		echo "Name: " . $result["last_name"] . ", " . $result["first_name"] . "\n";
		echo "Age: " . $result["age"] . "\n\n";
	}

	$db->drop();
	$connection->close();
?>

Go back to your browser and hit the new file: http://localhost/mongotest.php

Post to Twitter

This entry was posted in MongoDB, Open Source, PHP, Ubuntu. Bookmark the permalink.

9 Responses to Install PHP 5, Apache 2 and MongoDB on Ubuntu 10.10 and test it out

  1. Thanks a Lot, This helped me a LOT !!!!!

    you are awesome !!!

  2. Pingback: Utterly Apache… | Utterly IT566

  3. Devan says:

    Please help:
    i got this error while installing mongodb.
    it says : Unable to locate package mongodb-stable

  4. Chad Lung says:

    @Devan,

    It looks like they have made changes since this article was written. In particular you need to add the following line to your sources.list:
    deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

    After an apt-get update I was able to install mongodb-stable on a clean Ubuntu install. This might change though since their current documentation states the package name is “mongodb-10gen”. That didn’t work for me though “mongodb-stable” worked (for now).
    http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages

    Chad

  5. Pingback: Como instalar MongoDB no linux com PHP | NOSQL BRASIL - nosqlbr

  6. ken says:

    great article..thanks

  7. Fabiano says:

    Muito bom!
    Very good!

  8. Pingback: Tutorial de instalação do MongoDB – links | suissacorp.com.br

  9. Sarp says:

    Great tutorial.