NoSQL Zone is brought to you in partnership with:

Seth is the CTO at NuoDB. His main areas of focus are on the administration, security and resource management models, automation and the tools that drive these pieces. Seth is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

NuoDB: How to Turn Lamp Into a Powerful, Scale-Out Software Bundle

05.11.2013
| 1955 views |
  • submit to reddit

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";
$db = NULL;
try {
   $db = new PDO($dsn, $username, $password);
   $sql = "select * from Hockey";
   $stmt = $db->prepare($sql);
   $stmt->execute();
   $result = $stmt->fetchAll();
   echo '<h1>NuoDB Hockey Example</h1>';
   echo '<table border="1">';
   foreach ($result as $row) {
      echo '<tr>';
      echo "<td>" . $row["NUMBER"] . "</td>";
      echo "<td>" . $row["NAME"] . "</td>";
      echo "<td>" . $row["POSITION"] . "</td>";
      echo "<td>" . $row["TEAM"] . "</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

NuoDB_LANP_Example

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.

Published at DZone with permission of Seth Proctor, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)