Created
June 20, 2011 08:51
-
-
Save jsermeno/1035318 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install NPM | |
cd ~ | |
git clone git://github.com/isaacs/npm.git | |
cd npm | |
sudo -s | |
PATH=/usr/local/bin:$PATH | |
make install | |
exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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