Skip to content

Instantly share code, notes, and snippets.

View changx03's full-sized avatar
🏠
Working from home

Luke Chang changx03

🏠
Working from home
  • The University of Auckland
  • Auckland
  • 05:07 (UTC +12:00)
View GitHub Profile
@changx03
changx03 / flightplan-deploy.md
Created May 17, 2019 13:18 — forked from learncodeacademy/flightplan-deploy.md
Deploy Node.js Apps with Flightplan

##Setup your server (this would ideally be done with automated provisioning)

  • add a deploy user with password-less ssh see this gist
  • install forever npm install -g forever

##Install flightplan

  • npm install -g flightplan
  • in your project folder npm install flightplan --save-dev
  • create a flightplan.js file
@changx03
changx03 / node-deploy-as-upstart-service.md
Created May 17, 2019 13:19 — forked from learncodeacademy/node-deploy-as-upstart-service.md
Deploy Node.js app on Ubuntu as Upstart Service - instead of using Forever

Deploying a node app with Forever is great...until your server restarts unexpectedly. Then your app stops running and you have to re-deploy.

To get around this, we're going to run our node app as an Upstart service. Upstart services are great, because, once started, the system auto-restarts them if they fail, or if the server restarts.

###Step 1: Create a service for your node app

  • ssh in as root ssh root@youripaddress
  • Create a node-app.conf file in /etc/init
    IMPORTANT: whatever filename you pick is what you will use to start|stop|restart your service i.e. service node-app start
@changx03
changx03 / gist:ad4d34b2b64b85d27ff6068c39337199
Created May 17, 2019 13:37 — forked from learncodeacademy/gist:ebba574fc3f438c851ae
Nginx Node Frontend / Load Balancer / Static Assets Caching
upstream project {
server 22.22.22.2:3000;
server 22.22.22.3:3000;
server 22.22.22.5:3000;
}
server {
listen 80;
location / {
@changx03
changx03 / cluster.md
Created May 17, 2019 13:37 — forked from learncodeacademy/cluster.md
Node Cluster - Enhance your node app by using all the cores of your processor.

Here's all you have to do to add clustering to your node.js application.

  • save this code as cluster.js, and run cluster.js instead of server.js (or /bin/www, or whatever it's called for your project)
  • the only line you'll need to change is the last line - it needs to point to the location of your server.js file
var cluster = require('cluster');

if (cluster.isMaster) {
  // Count the machine's CPUs
 var cpuCount = require('os').cpus().length;
@changx03
changx03 / gist:5f281ffa1f598e42e7cb1605b0bd7807
Created May 17, 2019 13:41 — forked from learncodeacademy/gist:5f84705f2229f14d758d
Getting Started with Vagrant, SSH & Linux Server Administration
@changx03
changx03 / deployUser.md
Created May 17, 2019 16:56 — forked from learncodeacademy/deployUser.md
Adding a deploy user in Linux

(wherever it says url.com, use your server's domain or IP)

Login to new server as root, then add a deploy user

sudo useradd --create-home -s /bin/bash deploy
sudo adduser deploy sudo
sudo passwd deploy

And Update the new password

@changx03
changx03 / provision_lb1.sh
Last active May 18, 2019 18:11
Vagrant provision load balance script
#!/bin/bash
echo 'Starting Provision on:' $1
sudo apt-get update
sudo apt-get install -y nginx
sudo systemctl stop nginx.service
sudo rm -rf /etc/nginx/sites-enabled/default
sudo touch /etc/nginx/sites-enabled/default
echo "upstream testapp {
server 10.0.0.11;
@changx03
changx03 / Vagrntfile
Last active May 18, 2019 18:13
For running nginx load balancer
# -*- mode: ruby -*-
# vi: set ft=ruby :
# `vagrant ssh lb1` to shh on one machine
# `vagrant up web2` to start one machine
# `free -m` check memory size
# `ifconfig` check ip address
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |v|
v.memory = 1024
@changx03
changx03 / provision_web.sh
Last active May 19, 2019 05:53
Vagrant provision web bash
#!/bin/bash
echo "Starting Provision:" $1
sudo apt-get update
sudo apt-get install -y nginx
sudo nginx -t
sudo touch /var/www/html/index.html
echo "<h1>Hello " $1 "</h1>" >> /var/www/html/index.html
echo "Provision " $1 " completed"
@changx03
changx03 / linux_user.md
Last active May 19, 2019 16:25
Creating a new user on Linux

Creating a new user on Linux

I prefer to use cmder on Windows. It provides most comment linux commends out of the box.

Create a node.js project

# install express-generator
npm install -g express-generator