How to install and support node.js app from git on Ubuntu server

Problem

Full stack of commands for install node.js and app from git on the Ubuntu server (Ubuntu 12.04 on Amazon EC2)

Problem courtesy of: Andriy Kuba

Solution

#INSTALL

#install oracle java

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

#insatll git

sudo apt-get install git

#install node, npm & forever

sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm
sudo npm install forever -g

#download server repository

:~$ mkdir webserver
:~$ cd webserver$
:~/webserver$ git init
:~/webserver$ git remote add origin https://webserverteam@bitbucket.org/webserverteam/server.git
:~/webserver$ git config --global user.name "webserverteam"
:~/webserver$ git config --global user.email "webserverteam@gmail.com"
:~/webserver$ git config --global push.default "matching"
:~/webserver$ git config --global branch.autosetuprebase always
:~/webserver$ git config --global color.status auto
:~/webserver$ git config --global color.branch auto
:~/webserver$ git config branch.master.remote origin
:~/webserver$ git config branch.master.merge refs/heads/master
:~/webserver$ git pull

#install dependencies

:~/webserver$ npm intsall

#edit settings you need (if you need)

:~/webserver$ nano app.js

#setup iptables

#trivial user have no access to 80 port, so we run server on 8080 and do redirect 80->8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

#run web server app:

:~/webserver$ forever start app.js

#SUPPORT

#after every restart

#unfortunatly I could not "save\run after start" iptables with trivial rights
:~$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
:~$ cd webserver$
:~/webserver$ forever start app.js

#trivial update from git

:~$ cd webserver$
#check running forever processes
:~/webserver$ forever list
#kill forever process number 0
:~/webserver$ forever stop 0
:~/webserver$ git pull
:~/webserver$ forever start app.js

#full update from git

:~$ cd webserver$
:~/webserver$ forever list
:~/webserver$ forever stop 0
:~/webserver$ git fetch --all
:~/webserver$ git reset --hard origin/master
:~/webserver$ git pull
#edit settings you need (if you need)
:~/webserver$ nano app.js
:~/webserver$ forever start app.js

Solution courtesy of: Andriy Kuba

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.