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
- create an account and login to alibaba’s ecs console
- sign up here: https://www.alibabacloud.com/?spm=5176.2020520001.aliyun_topbar.1.cf8812d2eMeAOC
- ECS console here: https://ecs.console.aliyun.com
- create an ECS instance from Alibaba cloud
- create a new ssh key and bind it to your ECS instance ref
- 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)
- you should be able to see
Welcome to Alibaba Cloud Elastic Compute Service !
step 1 : install node.js
sudo apt-get updatemakes sure you get to download gitsudo apt-get install gitdownloads gitcurl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.shdownloads a .sh file (to setup node)sudo bash nodesource_setup.shinstalls the stuff needed to install nodesudo 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
sudo apt-get install nginxinstalls nginx to serve nodenpm i -g pm2installs PM2 Process Manager (monitors for crashes, autoscales node)
step 3 : YOUR CODE
cd /homegit clone https://github.com/<YOU MY DEAR READER>/<YOUR GITHUB REPO>.gitcd <YOUR GITHUB REPO>enter the folder you just git clonednpm installoryarn installinstalls 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
nano /etc/nginx/sites-available/defaultto open the nano editor- 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;
}
}
- this tells nginx to take requests that hit your
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.
sudo systemctl restart nginxrestarts nginx (my nginx failed here)
step 5 : FINALLY START YOUR NODEJS APP
cd /home/<YOUR NODEJS APP>pm2 start index.js --name "my-node-app"starts your node app, like gunicorn for your flask/nginx deployments