Skip to content

Instantly share code, notes, and snippets.

@abrkn
Created November 26, 2012 16:56
Show Gist options
  • Save abrkn/4149318 to your computer and use it in GitHub Desktop.
Save abrkn/4149318 to your computer and use it in GitHub Desktop.
Configuring EC2 Ubuntu 12 AMI for node.js web server

Update & upgrade

sudo apt-get update -y -q && sudo apt-get upgrade -y -q

Install node.js (with g++ for compiling native extensions and make)

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

Install hook to publish app

On work station:

git remote add prod ssh://ubuntu@YOURAMAZONHOSTNAME/home/ubuntu/snow-web.git

You may need to specify the SSH key manually for this remote. Edit ~/.ssh/config, adding:

Host YOURAMAZONHOSTNAME
  IdentityFile c:\snow.key

On server:

sudo apt-get install git -y
cd ~
mkdir snow-web.git
cd snow-web.git
git init --bare
sudo mkdir /var/www
sudo mkdir /var/www/snow-web

Set security group for website directory (from superuser)

sudo usermod -a -G www-data ubuntu
sudo chgrp -R www-data /var/www
sudo chmod -R g+w /var/www
sudo find /var/www -type d -exec chmod 2775 {} \;
sudo find /var/www -type f -exec chmod ug+rw {} \;

Configure post-receive hook: (from toroid.org)

vim hooks/post-receive

with template

#!/bin/sh
echo stopping upstart process...
sudo stop snow-web
echo checking out work tree...
git --work-tree=/var/www/snow-web --git-dir=/home/ubuntu/snow-web.git checkout -f
cd /var/www/snow-web
npm install
echo starting upstart process...
sudo start snow-web
echo post-receive hook successful
chmod +x hooks/post-receive

Configure upstart (from howtonode.org)

sudo vim /etc/init/snow-web.conf

with template

#!upstart
env name="snow-web"
env start
description "$name"
start on startup
stop on shutdown

script
    export HOME="/home/ubuntu"
    echo $$ > /var/run/$name.pid
    cd /var/www/$name ; sudo -u ubuntu NODE_ENV=prod PORT=8000 DEBUG=.* node bin/web.js >> /var/log/$name.sys.log 2>&1
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$name.sys.log
end script

pre-stop script
    rm /var/run/$name.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$name.sys.log
end script

Install monit (from howtonode.org)

sudo apt-get install monit -y

Configure monit

sudo vim /etc/monit/monitrc

with template:

#!monit
start program = "/sbin/start snow-web"
stop program  = "/sbin/stop snow-web"
if failed port 8000 protocol HTTP
    request /
    with timeout 10 seconds
    then restart

Start monit

sudo monit -d 60 -c /etc/monit/monitrc

On workstation (if local branch is called prod):

git push prod +prod:refs/heads/master

and from now on you publish with:

git push prod prod:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment