#!/bin/bash # # Node Kick Start # - Builds a node.js server # - Installs all dependencies apt-get install build-essential # Update and install NodeJS sudo apt-get update sudo apt-get install -y g++ curl libssl-dev apache2-utils sudo apt-get install -y git-core sudo git clone --depth 1 https://github.com/joyent/node.git cd node git checkout v0.4.7 ./configure --prefix=/usr make sudo make install ################################################## # Install NPM git clone git://github.com/isaacs/npm.git cd npm PATH=/usr/local/bin:$PATH make install ################################################## # Setup git remote master if [ "$1" != "" ]; then git clone $1 ~/www else mkdir ~/www cd ~/www npm install express stylus mongodb mongoose jade socket.io connect-mongodb fi mkdir ~/repo cd ~/repo git init --bare ################################################## # Create git hook cat > ~/repo/hooks/post-receive << EOF #!/bin/sh GIT_WORK_TREE=/root/www export GIT_WORK_TREE git checkout -f EOF chmod +x ~/repo/hooks/post-receive ################################################## # Install HAProxy wget http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev6.tar.gz tar xzf haproxy-1.5-dev6.tar.gz cd haproxy* make install ################################################## # HAProxy config mkdir /etc/haproxy cat > /etc/haproxy/haproxy.cfg << EOF global maxconn 4096 defaults mode http frontend all 0.0.0.0:80 timeout client 86400000 default_backend www_nodejs acl is_websocket hdr(upgrade) -i websocket acl is_websocket hdr_beg(host) -i ws use_backend www_nodejs if is_websocket backend www_nodejs option forwardfor timeout server 86400000 timeout connect 4000 server nodejs 127.0.0.1:3000 weight 1 maxconn 10000 check EOF # Test haproxy config haproxy -c -f /etc/haproxy/haproxy.cfg ################################################## # Install MongoDB curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz > mongo.tgz tar xzf mongo.tgz cp -R mongodb-linux-i686-1.8.1/bin/* /usr/bin/ sudo mkdir -p /data/db sudo chown `id -u` /data/db ################################################## # Install Node.js Upstart sudo apt-get install -y upstart cat > /etc/init/node.conf << EOF #!upstart description "node.js server" author "joe" start on startup stop on shutdown script export HOME="/root" exec /usr/bin/node $HOME/www/app.js 2>&1 >> /var/log/node.log end script EOF chmod +x /etc/init/node.conf #Install HAProxy Upstart cat > /etc/init/haproxy.conf << EOF #!upstart description "haproxy server" author "joe" start on startup stop on shutdown script export HOME="/root" exec /usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg end script EOF chmod +x /etc/init/haproxy.conf #Install Monit Upstart cat > /etc/init/monit.conf << EOF #!upstart description "monit server" author "joe" start on startup stop on shutdown script export HOME="/root" /usr/sbin/monit -d 30 -c /etc/monit/monitrc end script EOF chmod +x /etc/init/monit.conf #Install Mongo Upstart cat > /etc/init/mongo.conf << EOF #!upstart description "Mongo DB server" author "joe" start on startup stop on shutdown script export HOME="/root" exec /usr/bin/mongod end script EOF chmod +x /etc/init/mongo.conf ################################################## # Install Monit sudo apt-get install -y monit cat > /etc/monit/monitrc << EOF #!monit set logfile /var/log/monit.log check host nodejs with address 127.0.0.1 start program = "/sbin/start node" stop program = "/sbin/stop node" if failed port 3000 protocol HTTP request / with timeout 10 seconds then restart EOF