Skip to content

DSC-FPTU-HCMC/HandsOnLab-DeployNodeJSApplicationToComputeEngine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DSC FPTU HCMC

Hand-on lab

Deploy NodeJS Application to Google Compute Engine

A hello world server-side application powered by NodeJS

Lab: Deploy NodeJS application to Compute Engine

Prerequisites:

  • A google cloud platform account
  • Fundamental knowledge of NodeJS

STEP 1: Create a Virtual Machine

  1. In the Cloud Console, go to the VM Instances page.
  2. Click Create instance.
  3. In the Boot disk section, click Change to begin configuring your boot disk.
  4. On the Public images tab, choose Debian version 9.
  5. Click Select.
  6. In the Firewall section, select Allow HTTP traffic.
  7. Click Create to create the instance.

Allow a short time for the instance to start. After the instance is ready, it is listed on the VM instances page with a green status icon.

STEP 2: Connect to the VM

  1. In the Cloud Console, go to the VM Instances page.
  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

What is SSH?

Secure Shell (SSH) is a software standard to support encrypted data transfer between two computers. It can be used to support secure logins, file transfers or general purpose connects. Servers maintained by ITS require SSH-based connections in most cases.

STEP 3: Install Node & GIT

sudo apt-get -y update
sudo apt-get install -y nodejs npm

node --version
npm --version

What is NodeJS?

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

sudo apt install git-all

git --version

What is GIT?

GIT is a version control system, a software tool that helps record changes to files by keeping a track of modifications done to the code.

STEP 4: Clone source code from Github to the VM

git clone https://github.com/DSC-FPTU-HCMC/node-hello-world.git
cd node-hello-world

STEP 5: Start your application

# install the dependencies
npm install

# start the application
npm start

# Ctrl C to shutdown

STEP 6: Configure nginx

sudo apt-get install -y nginx

# navigate to configuration folder for nginx
cd /etc/nginx/sites-available

# backup your current default file
sudo mv default default.bak

# create new nginx's configuration file
sudo touch default

Edit the file default

server {
  listen 80;
  server_name YOUR_SERVERS_IP_ADDRESS;

  location / {
    proxy_pass "http://127.0.0.1:8080";
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_cache_bypass $http_upgrade;
  }
}
# restart nginx
sudo service nginx restart

# start application
npm start

What is nginx? nginx is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. ... In addition to its HTTP server capabilities, nginx can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

STEP 7: Setup PM2

# install pm2
sudo npm install -g pm2

# start the application
pm2 start npm -- start

What is PM2?

PM2 is a production process manager for Node. js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

STEP 8: See the magic

  1. In the Google Cloud Console, go to the VM Instances page.
  2. Copy the external IP Address of the instance you created.
  3. Open new tab on Google Chrome, and paste the link into navigation bar.

STEP 9: Clean up resource

  1. In the Google Cloud Console, go to the VM Instances page.
  2. Click the name of the instance you created.
  3. At the top of the instance's details page, click Delete.

Thank you!

DSC FPTU HCMC

Releases

No releases published

Packages

No packages published