deploying a node.js app to alicloud

makes you really appreciate gh-pages, now.sh and other deployment services that don't force you to setup your own virtual machine. - author

non-fat notes based on this blogpost

tested on macos 10.14 (Mojave) with alicloud's Hong Kong server. YMMV

step 0 : your ECS container / VM

  1. create an account and login to alibaba's ecs console
  2. create an ECS instance from Alibaba cloud
  3. create a new ssh key and bind it to your ECS instance ref
  4. connect to the ECS instance with an ssh keypair (the 'cloud console' (the page you see when you click 'connect' on your ECS instance) is super slow, don't use it)
    • add your ssh key on your terminal (i use ssh agent) to add that to my list of keysu.
    • ssh root@<YOUR ECS INSTANCE'S PUBLIC IP ADDRESS> ref
    • summing summing not verified, press yes to add that address to your system
  5. you should be able to see Welcome to Alibaba Cloud Elastic Compute Service !

step 1 : install node.js

ref

  1. sudo apt-get update makes sure you get to download git
  2. sudo apt-get install git downloads git
  3. curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh downloads a .sh file (to setup node)
  4. sudo bash nodesource_setup.shinstalls the stuff needed to install node
  5. sudo apt-get install -y nodejs

step 1.1 : (optional) install yarn

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

step 2 : install nginx and some process manager

  1. sudo apt-get install nginx installs nginx to serve node
  2. npm i -g pm2 installs PM2 Process Manager (monitors for crashes, autoscales node)

step 3 : YOUR CODE

  1. cd /home
  2. git clone https://github.com/<YOU MY DEAR READER>/<YOUR GITHUB REPO>.git
  3. cd <YOUR GITHUB REPO> enter the folder you just git cloned
  4. npm install or yarn install installs your npm packages

step 3.1 : other stuff

the author of the reference blogpost has a more complex application in mind, so if you have other stuff like .env files or mongodb to deal with, refer to that.

step 4 : nginx config

  1. nano /etc/nginx/sites-available/default to open the nano editor
  2. paste the following:
server {
  listen 80;

  server_name <YOUR ECS INSTANCE'S PUBLIC IP ADDRESS>;

  location / {
      proxy_pass http://localhost:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
  }
}
  1. this tells nginx to take requests that hit your <ECS IP ADDRESS> and give it to http://localhost:3000 which is your nodejs app.

you can replace the <YOUR ECS INSTANCE PUBLIC IP ADDRESS, 47.XX.XXX.XXX> with your domain name if you have one, but i don't, so i'm using the ip address.

  1. sudo systemctl restart nginx restarts nginx (my nginx failed here)
    • if your systemctl doesn't exist, try following this
    • my nginx still doesn't work with systemctl, so i used service restart nginx instead ref

step 5 : FINALLY START YOUR NODEJS APP

  1. cd /home/<YOUR NODEJS APP>
  2. pm2 start index.js --name "my-node-app" starts your node app, like gunicorn for your flask/nginx deployments