Skip to content

Instantly share code, notes, and snippets.

@solos
Forked from remy/jsbin-live-clone.md
Last active December 11, 2015 08:58
Show Gist options
  • Save solos/4576771 to your computer and use it in GitHub Desktop.
Save solos/4576771 to your computer and use it in GitHub Desktop.

Based on installing Ubuntu 10.04 (lucid) and cloning the live environment (calling local environment dev.jsbin.com)

Add core packages:

sudo apt-get install libpcre3-dev build-essential libssl-dev
sudo apt-get install git-core
sudo su -
mkdir pkgs && cd pkgs
wget http://nginx.org/download/nginx-1.0.0.tar.gz
./configure --user=www-data --group=www-data --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-pcre 
make
make install

adduser --system --no-create-home --disabled-login --disabled-password --group nginx

echo "jsbin" > /etc/hostname
hostname -F /etc/hostname
apt-get install mysql-server
logout # from root

Install Node 0.8.1:

cd pkgs 
wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz 
tar xf node-v0.8.16.tar.gz && cd node-v0.8.16
./configure --prefix=/usr/local/node
make 
make install

npm install -g forever
npm install -g grunt

Edit /usr/local/nginx/conf/nginx.conf to:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types    text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 10;

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   /www/jsbin/public;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Restart nginx:

/etc/init.d/nginx restart

Add JS Bin & JS Bin database

mkdir /WWW
cd /WWW
chmod 777 .
git clone git://github.com/remy/jsbin.git
git checkout development
npm install
mysql -uroot -e 'create database jsbin'
mysql -uroot jsbin < build/jsbin.sql
mysql -uroot jsbin < build/v3.sql

Add config.local.json and set to:

{
  "env": "production",
  "url": {
    "host": "dev.jsbin.com",
    "prefix": "/",
    "static": "dev-static.jsbin.com"
  },
  "store": {
    "adapter": "mysql",
    "mysql": {
      "host": "localhost",
      "user": "root",
      "password": "[CHANGE TO YOUR PASSWORD]",
      "database": "jsbin"
    }
  },
  "session": {
    "secret": "[CHANGE TO SOME SECRET]"
  }
}

Note that we're using jsbin.dev and static.jsbin.dev to replicate online, so you'll need to add these records to your local /etc/host file too.

Add run.js to /WWW/jsbin/

var jsbin = require('./lib/app');

jsbin.on('before', function () {
  jsbin.use(function (req, res, next) {
    var v3betaurl = jsbin.set('url prefix') + '/3/',
        host = req.header('host', '');

    if (req.url.indexOf(v3betaurl) === 0) {
      res.redirect(301, req.url.substring(v3betaurl.length - 1));
    } else if (host.indexOf('www') === 0 || host.indexOf('3.') === 0) {
      res.redirect(301, jsbin.set('url full') + req.url);
    } else {
      next();
    }
  });
});

jsbin.connect();

Build latest JS Bin JavaScript source:

cd /WWW/jsbin/public
grunt

Run JS Bin:

cd /WWW/jsbin/
PORT=8000 forever start --append -l forever.log run.js

To restart all services:

/etc/init.d/nginx start /etc/init.d/varnish start /etc/init.d/mysql start cd /WWW/jsbin/ PORT=8000 node run.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment