Skip to content
Micah Stubbs edited this page Nov 1, 2018 · 13 revisions

These are scripts for deploying your own server. We currently host blockbuilder on a Google Cloud Platform n1-standard-2 (2 vCPUs, 7.5 GB memory) VM instance running Ubuntu 16.04 LTS

This guide will list the steps required to setup a new blockbuilder server.

Configure Git User

sudo su root
git config --global user.email "buildingblocks@example.com"
git config --global user.name "Building Blocks"

(this prevents an error where git is complains about not knowing who you are when trying to save a thumbnail image)

Install node & npm with nvm

Install node version v8.12.0

Follow this guide from Digital Ocean, starting at the How To Install Using NVM section

once you install node & npm, you should see:

node -v
# v8.12.0
npm -v
# 6.4.1

Install JavaScript dependencies & build

npm install
npm run buildProd

Copy over secrets.json

Install Redis

blockbuilder uses Redis for ${some kind of caching} TODO document what exactly we use Redis for

install Redis on Ubuntu 16.04

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
sudo make install

you can also follow the instructions here https://redis.io/topics/quickstart

Start Redis

redis-server

Install MongoDB

# Import the public key used by the package management system
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

# Create a list file for MongoDB
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

# Reload local package database
sudo apt-get update

# Install the latest version of MongoDB
sudo apt-get install -y mongodb-org

you can also follow the instructions here https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Start MongoD

To start the MongoDB database service

sudo service mongod start

# to verify that MongoDB has started successfully
# inspect the logs
sudo vim /var/log/mongodb/mongod.log

# and look for the line 
# [initandlisten] waiting for connections on port 27017

Test server

cd blockbuilder
node server.js

Make sure that that node server.js works without errors before proceeding. It's easier to read the webserver logs and debug errors here, before we wrap it in a linux system service.

Create blockbuilder system service to wrap nodejs + express server

We create a linux service called blockbuilder.service with systemd. Wrapping our nodejs + express web server in a linux service makes sure that the blockbuilder nodejs + express web server stays running, even after a host-server reboot.

to install the service, copy the service defintion from the blockbuilder repo to this systemd directory:

sudo cp /home/ubuntu/blockbuilder/deploy/blockbuilder.service /etc/systemd/system/blockbuilder.service

We can now manage the blockbuilder service with these commands:

sudo systemctl start blockbuilder
sudo systemctl stop blockbuilder
sudo systemctl status blockbuilder

to start our new blockbuilder service on boot:

sudo systemctl enable blockbuilder

Networking

Note that the server runs on port 8889, so you will need to redirect port 80 traffic to port 8889 due to Ubuntu's security restrictions.

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8889
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443

Networking with NGINX

alternately, you can setup nginx

generally, follow:

crontab -e

more reading about crontabs https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

# enable the nginx config for your blockbuilder instance's domain
# where blockbuilder.org is your domain
sudo ln -s /etc/nginx/sites-available/blockbuilder.org /etc/nginx/sites-enabled/

Update

SSH into server and run make update.

This will run git pull to update the code base. It will also run npm install and npm run build to ensure dependencies are up to date after a pull and to build the build artifacts.

Other dependencies

If your server doesn't have it installed, you will need to get expect

sudo apt-get install expect
Deprecated instructions

Amazon AMI

You can install everything yourself, or use a pre-packaged AMI running Ubuntu 14.04 with everything up and running. The AMI id is ami-d52e87be. The user is ubuntu and the code is deployed in /home/ubuntu/Code/building-blocks.

Roll Your Own Server

If you wish to roll your own server, the following steps serve as a guide for getting everything up and running.

IP Tables

Note that the server runs on port 8889, so you will need to redirect port 80 traffic to port 8889. You can do this on ubuntu by using iptables-persistent and adding the following to end of /etc/iptables/rules.v4:

# Route ports to app
*nat
:PREROUTING ACCEPT [1:48]
:INPUT ACCEPT [13:816]
:OUTPUT ACCEPT [18:1447]
:POSTROUTING ACCEPT [18:1447]
-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8889
-A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8443
COMMIT
#Done

Upstart / Monit

The following assumes that everything is setup in /home/ubuntu/Code/building-blocks

upstart

Upstart provides a way to start / stop services.

building-blocks.conf

This is the upstart script which allows us to run service building-blocks start to startup the service. Copy to /etc/init/building-blocks.conf.

monit

Monit will watch processes and restart them if they stop. It is optional.

monitrc

Configuration file for monit. Copy to /etc/monit/monitrc

monit-building-blocks

Configuration file for watching building-blocks with monit. Copy to /etc/monit/conf.d/building-blocks