Friday, November 29, 2013

Zero to Play with Linux Neo4j, Play2 and nginx

This is a guide for taking your (debian like) linux system from a fresh install to staging server.

sudo -s
apt-get update
apt-get dist-upgrade

# install Sun java 7
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
apt-get update
apt-get install oracle-java7-installer

# install scala
apt-get install scala

# build tools
apt-get install build-essential

# install recent nodejs
sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:richarvey/nodejs #or sudo apt-get-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs nodejs-dev npm

# install nginx
apt-get install nginx


# get play
wget http://downloads.typesafe.com/play/2.2.1/play-2.2.1.zip

# unzip this somewhere
vim .bashrc

# add play to path
export PATH=$PATH:/path/to/play

# install neo4j

# start root shell
sudo -s
# Import our signing key
wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add - 
# Create an Apt sources.list file
echo 'deb http://debian.neo4j.org/repo testing/' > /etc/apt/sources.list.d/neo4j.list
# Find out about the files in our repository
apt-get update
# Install Neo4j, community edition
apt-get install neo4j
# start neo4j server, available at http://localhost:7474 of the target machine
/var/lib/neo4j/bin/neo4j start


# install git
apt-get install git

# clone projects
...
...

# make sure things are good
cd projects/dir/
play compile

# test running the server
play run

# compile staging package
play clean compile stage


# setup your init scripts to keep it alive
vim /etc/init.d/play.myapp

###################################################
## Init Script
###################################################
#!/bin/bash

APPLICATION_PATH=/home/user/projects/myplayapp

start() {
    echo -n "Starting"
    sudo start-stop-daemon --start --background --pidfile ${APPLICATION_PATH}/RUNNING_PID -d ${APPLICATION_PATH} --exec target/start -- -Dhttp.port=9000
    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}
stop() {
    echo -n "Stopping"
    sudo start-stop-daemon --stop --pidfile ${APPLICATION_PATH}/RUNNING_PID

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        echo " - Success"
    else
        echo " - Failure"
    fi
    echo
}

  case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: play-server {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL



## END INIT



#make it executable 
chmod +x /etc/init.g/play.myapp

# test it
/etc/init.d/play.myapp start

# now make it start on boot
update-rc.d play.myapp defaults


# finally lets get some proxy action
# edit nginx.conf adding servers for each of your projects...
vim /etc/nginx/nginx.conf


######### SAMPLE SECTION TO ADD in http{ ...
# add 1 sever per domain. Also note that you can have wildcards *
 server {
        listen       80;
        server_name  *.mysite.com;
               
        location / {
           proxy_pass http://localhost:9000/;
           proxy_set_header Host $http_host;
        }
# 404 ect...
}

# now restart nginx
/etc/init.d/nginx restart


Lets assume you had a number of sites on one server. You could have a number of play applications running on various ports. Then in nginx.conf you would define a sever rule for each site. Also MAKE sure you pass through the Host header or you will run into issues. (ex: secure_socaiul sending redirect urls as "localhost:9000"

No comments:

Post a Comment