Deploying Node.js Apps for Production on Alibaba Cloud

Introduction

  • Elastic Compute Service: A virtual computing environment that includes the CPU, memory, OS, bandwidth, disks, and other basic computing components. We’ll use this service to launch a Linux instance, to which we’ll deploy our Node.js application.
  • ApsaraDB for MongoDB: A secure, reliable, and elastically scalable cloud database service. We’ll use this service to launch a MongoDB database for our Node.js application.

Prerequisites

  • The Node.js application we’ll be deploying. You can find it on this Github repository
  • An Ubuntu 16.04 64 bit ECS instance. You can create one using this tutorial.
  • An ApsaraDB for MongoDB instance. You can create one using this tutorial.
  • Successfully connect to your ECS instance. To connect using SSH, you can follow this tutorial.

Step 1 — Installing All Server Dependencies

Installing Git

sudo apt-get install git

Installing Node.js

curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install -y nodejs

Installing Nginx

sudo apt-get install nginx

Installing PM2 Process Manager

npm i -g pm2

Step 2 — Getting the Application Code

Cloning the Application Code

cd /home
git clone https://github.com/bahdcasts/node-js-blog.git

Installing Project Dependencies

cd /home/node-js-blog
npm install

Setting up Environment Variables

cd /home/node-js-blog
nano .env
PORT=3000
DB_URI=
CLOUDINARY_API_KEY=xxxx
CLOUDINARY_API_SECRET=xxxx
CLOUDINARY_NAME=xxx
EXPRESS_SESSION_KEY=d29z4944SxETKPNWqKAkCNUjrZ4A

Step 3 — Connecting to the MongoDB Instance

Whitelisting Our Server IP on ApsaraDB for MongoDB

Setting up DB_URI Connection String

Setting the DB_URI Environment Variable

PORT=3000
DB_URI="mongodb://root:passWORD1234@dds-gs50fb23291731d41.mongodb.singapore.rds.aliyuncs.com:3717,dds-gs50fb23291731d42.mongodb.singapore.rds.aliyuncs.com:3717/admin?replicaSet=mgset-300215248"
CLOUDINARY_API_KEY=132255634713478
CLOUDINARY_API_SECRET=aeBNzdZFcju9ZEAiL7KsEMbBlFI
CLOUDINARY_NAME=bahdcoder
EXPRESS_SESSION_KEY=d29z4944SxETKPNWqKAkCNUjrZ4P

Step 4 — Setting up the Nginx Configuration

# Empty the current configuration file
echo > /etc/nginx/sites-available/default
# Open the file using nano
nano /etc/nginx/sites-available/default
server {
listen 80;
server_name 47.88.230.255; 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;
}
}
sudo systemctl restart nginx

Step 5 — Starting the Application

cd /home/node-js-blogpm2 start index.js --name "blog"

Tracking Application Logs

pm2 logs blog

Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Testing Solidity with Truffle and Async/Await

What Is Flux Architecture In React Native?

How to use Next.js Image Optimization with Cloudflare

Gru’s Plan Meme: Using Cloudflare & Transform Rules to serve webp images with a fallback

Best Chrome Extensions for Developers

What is AJAX?

MakeCode Microbit 遊戲設計:貓追老鼠遊戲(Microbit Game: Cat Chasing Mouse Game)

How to use useParams in a React SPAs

Setting up React App with Webpack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Deploying Node.JS MongoDB application

GraphQL Schema

Redis key expiry

MongoDB Iceberg: Introduction to MongoDB Query.