Home Archive RSS Login

I learned something new today

Install MongoDB, Xdebug and PHP 5.3.3 on Ubuntu 9.10

Here is a nice video that shows you how to install PHP 5.3.*, Xdebug, MongoDB and getting started with Lithium, the most RAD framework for PHP 5.3+.

I will note down some of the steps here anyway on how I did on Ubuntu. I started from Ubuntu Server installation and hence already had Apache on the system. Hence, some steps might be different.
sudo apt-get install apache2-prefork-dev

Now the web server must be running at http://localhost. The document root that is setup by default on Ubuntu is /var/www. 

1. PHP 5.3.3

Now head over to the php website and grab the PHP 5.3.* source. At the time of writing this post, it was 5.3.3. Here is the command line I used:
cd ~
wget http://www.php.net/get/php-5.3.3.tar.gz/from/us.php.net/mirror
tar -xzvf php-5.3.3.tar.gz
cd php-5.3.3

You can create a config.clean script in this directory and add the lines shown.
nano config.clean
and paste the following lines:
#! /bin/sh
# ..-. .- .. .-..  ..-. .- ... -
Now you can run the following:

sudo apt-get install libxml2-dev libbz2-dev libcurl4-gnutls-dev libjpeg62-dpg libpng12-dev libxslt1-dev

sudo sh ./config.clean

make -i install

Now PHP 5.3.3 installation is complete. 

2. Xdebug Installation

# install dependancies
sudo apt-get install php5-dev

#install xdebug
sudo pecl install xdebug

# copy and start editing php.ini to add xdebug module
sudo cp /etc/php5/apache2/php.ini /etc/php5
sudo nano /etc/php5.ini 
# Add the following to the end:

3. MongoDB Installation

sudo mkdir -p /data/db

# Download Mongo
curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.4.4.tgz > mongo.tgz
tar xzf mongo.tgz

# start mongod server

# start mongo client 

Now you can try out mongodb by referring to the documentation on their website at http://mongodb.org
Author: Prit -  Date: 27 Jul 2010 18:36
Tags: mongodb,php -  Visits: 8482 -  No Comments

MongoDB - Nginx module for serving files from MongoDB's GridFS

Nginx is a fast web server that is especially popular for its low footprint even under heavy loads. I have tested this on few server setups and found this to be a true statement. 

As mentioned on an earlier post, you can store files in MongoDB using GridFS. This module adds the functionality in Nginx to serve files directly from MongoDB's GridFS as if it were being served from a regular directory. You can get all the information and the files from the following git location:

This is the configuration I tried:
location /gridfs/
    gridfs my_db

When a user visits my site and goes to http://mysite/gridfs/image1.jpg, nginx takes this file from my mongodb database my_db and serves it. 

Author: Prit -  Date: 27 Jul 2010 11:22
Tags: mongodb -  Visits: 57077 -  No Comments

MongoDB - GridFS

Intro from the MongoDB website:

GridFS is a storage specification for large objects in MongoDB. It works by splitting large object into small chunks, usually 256k in size. Each chunk is stored as a separate document in a chunks collection. Metadata about the file, including the filename, content type, and any optional information needed by the developer, is stored as a document in a files collection.

So for any given file stored using GridFS, there will exist one document in files collection and one or more documents in the chunks collection.

GridFS links from the MongoDB website:


PHP MongoGridGS Class


I tested this out in PHP and following are some sample codes that may help.

1. PHP code to store uploaded file into the MongoDB GridFS.

    if (isset($_POST["PHPSESSID"])) {

    $conn = new Mongo("");         // Connect
    $db = $conn->uniform_server;                // Select DB
    $db->authenticate("user","password");        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS
    $name = $_FILES['Filedata']['name'];        // Get Uploaded file name
    $type = $_FILES['Filedata']['type'];        // Try to get file extension
    $id = $grid->storeUpload('Filedata',$name);    // Store uploaded file to GridFS
    /* Mime types did not work properly on my setup, hence the following logic */
    $ext = substr($name,-3);
    switch($ext) {
    case "jpg": $type = "image/jpeg"; break;
    case "gif": $type = "image/gif"; break;
    case "png": $type = "image/png"; break;
    case "txt": $type = "text/plain"; break;
    case "pdf": $type = "application/pdf"; break;
    case "zip": $type = "application/x-zip"; break;
    /* Add additional metadata related to the file if required */
    $files = $db->fs->files;
    $files->update(array("filename" => $name), array('$set' => array("contentType" => $type, "aliases" => null, "metadata" => null)));

    $conn->close();                                // Close connection

2. PHP code to list all the files in the GridFS (in a particular database)

    $conn = new Mongo("");         // Connect
    $db = $conn->uniform_server;                // Select DB
    $db->authenticate("user","password");        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS
    $cursor = $grid->find();

    foreach ($cursor as $obj) {                   // iterate through the results
        echo 'Filename: '.$obj->getFilename().' Size: '.$obj->getSize().'
    $conn->close();                                // Disconnect from Server

3. PHP code to download files stored in GridFS

    $conn = new Mongo("");         // Connect
    $db = $conn->uniform_server;                // Select DB
    $db->authenticate("user","password");        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS
    $ask = $_REQUEST['file'];                   // Get filename requested
    $file = $grid->findOne(array('filename' => $ask));
    $files = $db->fs->files;
    $file1 = $files->findOne(array('filename' => $ask));
    $id = $file->file['_id'];
    if ( (substr($ask,-3) == 'zip') || (substr($ask,-3) == 'pdf') ) {
       /* Any file types you want to be downloaded can be listed in this */
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename='.$ask);
       header('Content-Transfer-Encoding: binary');
       $cursor = $db->fs->chunks->find(array("files_id" => $id))->sort(array("n" => 1));
       foreach($cursor as $chunk) {
          echo $chunk['data']->bin;
    else {
       header('Content-Type: '.$file1["contentType"]);
       echo $file->getBytes();

    $conn->close();                                // Disconnect from Server

4. PHP code to delete a file from GridFS

    $conn = new Mongo("");         // Connect
    $db = $conn->uniform_server;                // Select DB
    $db->authenticate("user","password");        // Authenticate to MongoDB
    $grid = $db->getGridFS();                    // Initialize GridFS
    $filename = $_REQUEST["file"];                // Get requested filename
    $file = $grid->findOne($filename);             // Find file in GridFS
    $id = $file->file['_id'];                    // Get the files ID
    $grid->delete($id);                            // Delete the file

    $conn->close();                                // Disconnect from Server

Author: Prit -  Date: 22 Jul 2010 12:13
Tags: mongodb,programming -  Visits: 55131 -  No Comments

MongoDB - Replica Pair

Intro from the MongoDB website:

Mongo supports a concept of replica pairs. These databases automatically coordinate which is the master and which is the slave at a given point in time.

At startup, the databases will negotiate which is master and which is slave. Upon an outage of one database server, the other will automatically take over and become master from that point on. In the event of another failure in the future, master status would transfer back to the other server. The databases manage this themselves internally.

You can read about the replica pair from the MongoDB website at the below link:

Following is an example of how to run a replica pair. A replica pair is different from the master-slave configuration. In a master slave config, the slave is read-only and acts like a backup database. In a replica pair, if any of the databases go down, then the other takes over as master.
./mongod --pairwith localhost:27018 --arbiter localhost:27019 --dbpath ~/data1
./mongod --port 27018 --pairwith localhost:27017 --arbiter localhost:27019 --dbpath ~/data2
./mongod --port 27019 #arbiter

The arbiter is a mongodb server that runs on a third machine that helps negitiate between the paired servers to determine which one should be master.

Following is the PHP example code to connect when using a replica pair:
$m = new Mongo("mongodb://localhost:27017,localhost:27018");

I tested this by stopping one server at a time and the other took over as master and the application continued to work without any failures or unavailability.
Author: Prit -  Date: 22 Jul 2010 12:07
Tags: mongodb,programming -  Visits: 5104 -  No Comments

MongoDB - Master Slave Setup

You can read more about the master-slave configuration at the MongoDB website:

Following commands to start a master slave setup in Ubuntu and monitor outputs if the logs are the same.
Start the master:
sudo /usr/bin/mongod --master --rest --config /etc/mongodb.conf

Start the slave:
sudo /usr/bin/mongod --rest --slave --source --dbpath /data/slavedb/ --fastsync --autoresync --config /etc/mongodb2.conf

Monitor logs from command line:
tail -f /var/log/mongodb/mongodb.log

Also, when I had authentication issues, the following webpage helped resolve:

In auth mode, for replication to work, you need to add the following kind of similar users to the "local" db of both the master and the slave.

Author: Prit -  Date: 22 Jul 2010 12:06
Tags: mongodb,programming -  Visits: 6160 -  No Comments

Pages: [1] [2]