NuoDB: How to Turn LAMP into a Powerful, Scale-out Software Bundle

A popular software stack for Web applications is LAMP.  LAMP is the combination of Linux, Apache, MySQL, and PHP.  It is easy to use NuoDB instead of MySQL in that software stack (LANP) :).  I will show you how to do it.  In this article, I will create a very simple web application that uses NuoDB to display the contents of the NuoDB Hockey sample database.

In this blog article, I am using clean install of 64-bit Ubuntu desktop 12.04.http://www.ubuntu.com/download/desktop  (note: the commands and paths below may be different on other Linux distributions).

 

Installing Apache2 and PHP

Run the following commands to install Apache2 and PHP:

$ sudo apt-get install apache2

$ sudo apt-get install libapache2-mod-php5

 

Install and Configure NuoDB

Download NuoDB: http://www.nuodb.com/download

Install NuoDB and run the quickstart: http://www.nuodb.com/nuodb-online-documentation/tasks/t_InstallConfigLix_inst.html

To use NuoDB with PHP, you will need to install and configure the NuoDB PHP PDO Driver.  The source code is available on GitHub,  but you probably won’t need to build it yourself because your NuoDB installation already contains pre-built drivers for PHP 5.3 NTS and PHP 5.4 NTS.

 

 

PHP Configuration

The Ubuntu distro uses PHP 5.3.10 NTS.  You can see that with the following command:

$ php -i | grep "PHP Version" PHP Version => 5.3.10-1ubuntu3.6  

Next, we can find the location of the PHP extension_dir:

$ php -i | grep extension_dir extension_dir => /usr/lib/php5/20090626 => /usr/lib/php5/20090626

 

Install and Configure NuoDB PHP PDO Driver

Having obtained the PHP  version and extension_dir, we can copy the correct NuoDB PHP PDO Driver to that extension_dir with the following command:

$ sudo cp /opt/nuodb/drivers/php_pdo/php-5.3.NTS/pdo_nuodb.so /usr/lib/php5/20090626

 

Next, we need to modify the PHP Configuration to dynamically load the NuoDB PHP PDO Driver.  This is done by adding “extension=pdo_nuodb.so” to your PHP initialization files.  On Ubuntu, PHP is configured to scan specific directories for PHP .ini files.  The command line version of PHP scans the /etc/php/cli/conf.d directory.  When PHP is executed from Apache2, PHP is configured to scan/etc/php5/apache2/conf.d directory.  To add NuoDB to those PHP configurations, we can use that extension statement in a file named nuodb.ini and copy that file to those config directories:

$ echo "extension=pdo_nuodb.so" > /tmp/nuodb.ini

$ sudo cp /tmp/nuodb.ini /etc/php5/cli/conf.d/

$ sudo cp /tmp/nuodb.ini /etc/php5/apache2/conf.d/

$ sudo /etc/init.d/apache2 restart

 

 

Verify Proper Loading of NuoDB PHP PDO Driver

We can verify the proper configuration and loading of the NuoDB PHP PDO Driver by calling the phpinfo() function from a .php file that we can copy to the default Apache2 website.  Example:

$ echo "<? phpinfo(); ?>" > /tmp/phpinfo.php

$ sudo cp /tmp/phpinfo.php /var/www/

Then use the web browser with the URL: http://localhost/phpinfo.php

 

phpinfo

 

NuoDB Web APP

Below is a example of the PHP code that can display the contents of the NuoDB Hockey sample database:

----------

<?php
$dsn = "nuodb:database=test@localhost;schema=Hockey";
$username = "dba";
$password = "goalie"; # MAY BE 'dba' on some installations
$db = NULL; 
try { 
    $db = new PDO($dsn, $username, $password); 
    $sql = "SELECT * FROM teams WHERE conferenceid = 'EC' AND year = 2011 ORDER BY rank"; 
    $stmt = $db->prepare($sql); 
    $stmt->execute(); 
    $result = $stmt->fetchAll(); 
    echo '<h1>NuoDB Example - EAST COAST Conference</h1>'; 
    echo '<table border="1">';
    // Columns
        echo '<tr>'; 
        echo "<td> YEAR </td>"; 
        echo "<td> TEAMID </td>"; 
        echo "<td> NAME </td>"; 
        echo "<td> DIVISIONID </td>";
        echo "<td> RANK </td>";
        echo "<td> PLAYOFF </td>";
        echo "<td> GAMES </td>";
        echo "<td> WINS </td>";
        echo "<td> LOSSES </td>";
        echo "<td> TIES </td>";
        echo "<td> OVERTIMELOSSES </td>";
        echo '</tr>'; 
    // Populate Rows
    foreach ($result as $row) { 
        echo '<tr>'; 
        echo "<td>" . $row["YEAR"] . "</td>"; 
        echo "<td>" . $row["TEAMID"] . "</td>"; 
        echo "<td>" . $row["NAME"] . "</td>"; 
        echo "<td>" . $row["DIVISIONID"] . "</td>";
        echo "<td>" . $row["RANK"] . "</td>";
        echo "<td>" . $row["PLAYOFF"] . "</td>";
        echo "<td>" . $row["GAMES"] . "</td>";
        echo "<td>" . $row["WINS"] . "</td>";
        echo "<td>" . $row["LOSSES"] . "</td>";
        echo "<td>" . $row["TIES"] . "</td>";
        echo "<td>" . $row["OVERTIMELOSSES"] . "</td>";
        echo '</tr>'; 
    } 
    echo '</table>'; 
} catch (PDOException $e) { 
    echo $e->getMessage(); 

$db = NULL;
?>

----------

Copy the above code into the file /var/www/showHockey.php

 

Running the Web APP

To run the web application, use the web browser on the URL:http://localhost/showHockey.php

If you do not recieve a similar page, then either your driver is not loaded properly or you did not run quickstart. ( For quickstart simply run ./opt/nuodb/run-quickstart)

 

NuoDB Example - EAST COAST Conference

Summary

This article enumerated the simple steps for using NuoDB with Linux, Apache2, and PHP.  There are numerous LAMP and PHP PDO examples on the internet where you can learn more about writing these types of Web Applications.   If you have an existing LAMP application that uses PHP PDO, you should be able to make that application run with NuoDB with little to no changes.  In an upcoming post I’ll show you how to use the Zend framework with NuoDB.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.