I would really love to see more of my fellow PHP developers talking about, playing with, and building awesome applications on top of graph databases. They really are a powerful storage solution that fits well into a wide variety of domains.
In this two part series, I'll detail how to set up a development environment for building a project with a graph database (specifically Neo4j). Part 1 will show how to set up the development and unit testing databases. In Part 2, we'll create a basic application that talks to the database, including unit tests.
All the steps below were performed on Ubuntu 10.10 Maverick, but should be easy to translate to any other OS.
Grab the ComponentsThere are a few libraries and software tools needed for the project. First off, we'll need to install our programming environment, specifically PHP, PHPUnit and Java. How to do this is dependent on your operating system. My setup is PHP 5.3, PHPUnit 3.6 and Sun Java 6.
Next, we need to get Neo4j. Download the latest tarball from http://neo4j.org/download. I usually go with the latest milestone release (2.0 at this time.)
> cd ~/Downloads > wget http://dist.neo4j.org/neo4j-community-2.0.1-unix.tar.gz
Set Up the Development InstanceIt is possible to put test and development data in the same database instance, but I prefer a two database solution, even when using a SQL database. I do this because 1) I don't have to worry about accidentally blowing away any development data I care about, and 2) I don't have to worry about queries and traversals in my unit tests accidentally pulling in development data, and vice versa.
Unlike SQL or many NOSQL database servers, Neo4j cannot have more than one database in a single instance. The only way to get two databases is to run two separate instances on two separate ports or hosts. The instructions below describe how to set up our development and unit testing databases on the same host but listening on different ports.
First, create a point to hold both neo4j instances, and unpack the development instance:
> mkdir -p ~/neo4j > cd ~/neo4j > tar -xvzf ~/Downloads/neo4j-community-2.0.1-unix.tar.gz > mv neo4j-community-2.0.1 devNext, configure the server by editing the file "~/neo4j/dev/conf/neo4j-server.properties". Make sure that the server is on port 7474
org.neo4j.server.webserver.port=7474If you will be accessing the database instance from a host other than localhost, uncomment the following line:
org.neo4j.server.webserver.address=0.0.0.0Save the config and exit. Now it's time to start the instance.
> ~/neo4j/dev/bin/neo4j startYou should be able to browse to the Neo4j Browser panel: http://localhost:7474
Set Up the Testing InstanceUnpack the test instance:
> cd ~/neo4j > tar -xvzf ~/Downloads/neo4j-community-2.0.1-unix.tar.gz > mv neo4j-community-2.0.1 testNext, configure the server by editing the file "~/neo4j/test/conf/neo4j-server.properties". Make sure that the server is on port 7475 (note that this is a different port from the development instance!)
org.neo4j.server.webserver.port=7475If you will be accessing the database instance from a host other than localhost, uncomment the following line:
org.neo4j.server.webserver.address=0.0.0.0We need one more change to differentiate the testing from the development instance. Edit the file "~/neo4j/test/conf/neo4j-wrapper.properties" and change the instance name line:
wrapper.name=neo4j-testSave and exit, then start the instance.
> ~/neo4j/test/bin/neo4j startYou should be able to browse to the Neo4j Browser panel: http://localhost:7475
Congratulations! Everything is set up and ready for us to build our application. In Part 2 of this series, I'll talk about setting up unit tests and building a basic application using a graph database as a backend.