2011-11-05

Development Setup for Neo4j and PHP: Part 1

Update 2014-02-15: Using Neo4jPHP from a downloaded PHAR file has been deprecated. The preferred and supported way to install the library is via Composer. The examples below have been updated to reflect this. It has also been updated for the 2.0 release of Neo4j.

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 Components

There 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 Instance

It 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 dev
Next, 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=7474
If 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.0
Save the config and exit. Now it's time to start the instance.
> ~/neo4j/dev/bin/neo4j start
You should be able to browse to the Neo4j Browser panel: http://localhost:7474

Set Up the Testing Instance

Unpack the test instance:
> cd ~/neo4j
> tar -xvzf ~/Downloads/neo4j-community-2.0.1-unix.tar.gz
> mv neo4j-community-2.0.1 test
Next, 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=7475
If 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.0
We 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-test
Save and exit, then start the instance.
> ~/neo4j/test/bin/neo4j start
You 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.

22 comments:

  1. Great post Josh! I like the step-by-step instructions! Would love something like that in a README or the Neo4j Manual at docs.neo4j.org/chunked/snapshot/

    /peter

    ReplyDelete
  2. Thanking You
    Your blog is very informative
    software development

    ReplyDelete
  3. Thanks for this really helpful tutorial..
    Good work, Keep it UP.

    ReplyDelete
  4. just a note, I think you have a typo following this header regarding dev vs 'test' instance:

    'Set Up the Testing Instance'
    Unpack the development instance

    ReplyDelete
  5. Hi Josh
    When saving a node, I got the error below, any suggestion?

    Fatal error: Uncaught exception 'Everyman\Neo4j\Exception' with message
    'Unable to create node [500]:
    Headers: Array
    (
    )
    Body: Array
    (
    [error] => Failed to connect to ::1: Permission denied [7]
    )
    ' in phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Command.php:116
    Stack trace:
    #0
    phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Command/CreateNode.php(68):
    Everyman\Neo4j\Command->throwException('Unable to creat...', 500,
    Array, Array)
    #1 phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Command.php(69):
    Everyman\Neo4j\Command\CreateNode->handleResult(500, Array, Array)
    #2 phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(583):
    Everyman\Neo4j\Command->execute()
    #3 phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Client.php(490):
    Everyman\Neo4j\Client->runCommand(Object(Everyman\Neo4j\Command\CreateNode))
    #4
    phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Node.php(110):
    Everyman\Neo4j\Client->saveNode(Object(Everyman\Neo4j\Node))
    #5 /var/www/html/1140/application/controllers/test_neo4j.php(28):
    Everyman\Neo4j\Node->save()
    #6 [interna in phar:///opt/neo4jphp.phar/lib/Everyman/Neo4j/Command.php
    on line 116 
    Neil

    ReplyDelete
  6. It caused by the selinux. Turn it off and it works.

    ReplyDelete
  7. if some of your instance isn't working at first launch, simply try to re-start it again

    ReplyDelete
  8. I already address downloading the plugin, as well as changing the ports between the development and testing database. Not sure about https, since I don't use it in development environments. I'll have to check out if it affects anything and make any appropriate changes. Thanks for the suggestions!

    ReplyDelete
  9. suddenly i'm getting this error when deleting the test instance:

    DELETE /db/data/cleandb/secret-key==> 500 Internal Server Error==> "Node[0]"
    any ideas?

    ReplyDelete
  10. i managed to delete all nodes via cypher
    start n=node(*) delete n;

    and then run my script (adding some new nodes) again and the 0's re-appeared. strange

    ReplyDelete
  11. I think current versions of Neo4j are deprecating the "root" node (node 0) and future versions are removing it altogether. I will update the post to show how to use Gremlin to reset the database instead of the plugin (can't use Cypher since not all versions support deleting with Cypher.)

    ReplyDelete
  12. i switched from 1.8-SNAPSHOT to 1.8 stable and it seems that only 1 instance can run via webadmin. although i can acces both of them via console and neo4j-shell, i'm unable to do so via webadmin.
    the webadmin works for just the first started instance. for the second it tries to access it but never returns any data

    ReplyDelete
  13. the same problem still persists in v1.9

    ReplyDelete
  14. Hi friends, i want say important thing to u all.if u are planning to study software
    languages or sotware training u must go to besant technologies where they
    providing high quaity of teaching with experienced MNC working professionls.I
    studied php course in Besant technologies.now i got job in MNC company.i want to
    thank besant technologies. friends if want further details ,check below link.
    php training in chennai

    thank you

    ReplyDelete
  15. Hi friends, i want say important thing to u all.if u are planning to study software
    languages or sotware training u must go to besant technologies where they
    providing high quaity of teaching with experienced MNC working professionls.I
    studied php course in Besant technologies.now i got job in MNC company.i want to
    thank besant technologies. friends if want further details ,check below link.

    php training in chennai thank you

    ReplyDelete
  16. Thanks for sharing these niche piece of coding to our
    knowledge. Here, I had a solution for my inconclusive problems & it’s
    really helps me a lot keep updates…
    http://phptraininginchennai.in/

    ReplyDelete
  17. It was really a wonderful article and I was really impressed by reading this blog. Thanks for sharing this informative blog. I did HTML5 Training in Chennai at FITA academy, Its really useful for me. Suppose if anyone interested to learn real time PHP Training Chennai reach FITA.

    ReplyDelete