Skip to content

Instantly share code, notes, and snippets.

@larascasse
Forked from jsermeno/config-haproxy.sh
Created February 9, 2012 11:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larascasse/1779442 to your computer and use it in GitHub Desktop.
Save larascasse/1779442 to your computer and use it in GitHub Desktop.
Node.js server and Web Sockets on Amazon EC2 with Express.js and Socket.IO - http://catchvar.com/nodejs-server-and-web-sockets-on-amazon-ec2-w
# 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 HAProxy
cd ~
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
# Install MongoDB
cd ~
curl http://downloads.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz > mongo.tgz
tar xzf mongo.tgz
sudo mkdir -p /data/db
sudo chown `id -u` /data/db
# 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
cd ~
git clone git://github.com/isaacs/npm.git
cd npm
sudo -s
PATH=/usr/local/bin:$PATH
make install
exit
# Install Upstart
sudo apt-get install -y upstart
cat > /etc/init/nodeServer.conf << EOF
#!upstart
description "node.js server"
author "jsermeno"
start on startup
stop on shutdown
script
export HOME="/root"
NODE_ENV=production node /root/www/app.js 2>&1 >> /var/log/node.log
end script
EOF
chmod +x /etc/init/nodeServer.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 nodeServer"
stop program = "/sbin/stop nodeServer"
if failed port 3000 protocol HTTP
request /
with timeout 10 seconds
then restart
EOF
# On Local
git remote add ec2 ssh://<user>@<instance ip>.compute-1.amazonaws.com/root/repo
# You may need to do this if you get asked for a password
# ssh-add <path to your amazon key>
git push ec2 master
# Run everything
screen
# Start MongoDB
~/mongodb-linux*/bin/mongod
# ctrl+a then ctrl+d
cd ~/www
# Install NPM dependencies
npm install express stylus mongodb mongoose jade socket.io connect-mongodb
start nodeServer
monit -d 60 -c /etc/monit/monitrc
# Run HAProxy
haproxy -f /etc/haproxy/haproxy.cfg
# Create git hook
cat > hooks/post-receive << EOF
#!/bin/sh
GIT_WORK_TREE=/root/www
export GIT_WORK_TREE
git checkout -f
EOF
chmod +x hooks/post-receive
# Setup git remote master
mkdir ~/www
cd ~/www
mkdir ~/repo
cd ~/repo
git init --bare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment