How to Deploy Node.js App in EC2 instance?

Answer

In this post, I will tell you what is PuTTY, FileZilla and Amazon EC2 instance and how to deploy a node.js app (website) in AWS EC2 instance.

What is PuTTY?

PuTTY is open source SSH and telnet client software that allow you to remotely connect to your Amazon EC2 instance to install / update operating system, software, package, and so on.

What is FileZilla?

FileZilla is a open source FTP, FTPS and SFTP software that allows you to transfer your file from your local machine to the Server.

What is Amazon EC2 Instance?

Amazon EC2 instance is the varying combinations of CPU, memory, storage, and networking capacity that allow you to run your websites.

The best part of the Amazon EC2 instance is that it will allow you to scale your resources to the requirements of your target workload.

Creating a Simple Node.js App

Our Node.js App will contain two files:

  • home.html - Client-Side HTML file

    HTML Online Compiler
    <!DOCTYPE html> <html> <head> <title>Welcome Home</title> </head> <body> <h2>Welcome Home</h2> </body> </html>
  • index.js - Server-Side Node.js file

    Node.js Compiler
    var express = require("express"); var app = express(); app.get("/", function(req, res) { res.sendFile(__dirname + "home.html"); }); app.listen(80, function() { console.log("port is listening to 80"); });

Sign-In to Your AWS Account

Lets go to https://aws.amazon.com/console/ and do any one of the following:

  • If you have an AWS Account - Click on the 'Already have an account? Sign in' link and Sign-In
  • If you do not have an AWS Account - Click on the 'Create a Free Account' button and then Sign-In
AWS Sign-In Page

Now you will head towards the "AWS Management Console" Page.

AWS Management Console Page

Launch an EC2 Instance

From the "AWS Management Console page", click on ServicesEC2 (under Compute section).

AWS EC2 Instance Page

Now you will head towards the "EC2 Management Console" page. Before choosing anything, make sure that the deploy region is of your choice.

AWS EC2 instance Home Page

From the "EC2 Management Console" page, click on InstancesLaunch Instance.

AWS EC2 instance launch

Now you will head towards the "Launch instance wizard" Page. Do the following steps to launch an AWS EC2 instance.

Step 1: Choose an Amazon Machine Image (AMI)

Do the following steps:

  1. Click on the check box 'Free tier only'.
  2. Choose 'Ubuntu Server 18.04' (you are requested to choose latest version).
  3. Click on the check box 64-bit(x86)
  4. Click on "Select" button
Choose an AMI Page

Step 2: Choose an Instance Type

Do the following steps:

  1. Choose "t2.micro" (Free tier eligible) instance.
  2. Click on the 'Next: Configure instance Details' button.
Choose an Instance Type Page

Step 3: Configure Instance Details

Leave everything as it is and click on the 'Next: Add Storage' button.

Points to be noted

  • You are free to choose the 'Subnet' of your choice or AWS will choose any available 'subnet' on behalf of you.
  • Make sure the 'Tenancy' is 'Shared - Run a shared hardware instance'.
Configure Instance Details Page

Step 4: Add Storage

Leave everything as it is and click on the 'Next: Add Tags' button.

Add Storage Page

Step 5: Add Tags

Type key-Value name of your choice and click on the 'Next: Configure Security Group'.

Add Tags Page

Step 6: Configure Security Group

Do the following steps:

  1. In Assign a security group, choose 'Create a new security group'.
  2. In Security group name input box, type a meaning full name of your choice.
  3. In Description input box, type a detail information about the Security Group.

Now, add the following rules

Type Source Explanation
HTTP AnyWhere To HTTP request
HTTPS AnyWhere For HTTPs request
SSH AnyWhere To upload files to EC2 instance (using Filezilla).
NFS AnyWhere To share the files remotely (EFS)
Custom TCP Rule AnyWhere One free port for testing (Not necessary)
Configure Security Group Page

Step 7: Review Instance Launch

Review all the details of your ec2 configuration and click on the 'Launch' button.

Review Instance Launch Page

Download New key pair

Now, 'Select an existing key pair or create a new key pair' pop up will appear. Do the following:

  1. Choose the 'Create a new key pair' option.
  2. Type the name of your new key pair.
  3. Click on the 'Download Key Pair' button.
  4. After the download, the 'Launch' button will be enabled. Click on the 'Launch' button.

Points to be Noted:

  • Do not delete your 'key pair' file. Because it is the only way to access the EC2 instance you launched.
  • Do not share your 'key pair' file in any internet platform in any case.
Download Key Pair Page

Launch Status

Click on the 'View Instances' button.

Note: AWS will take at least 5 mins to launch your EC2 instance.

Launch Status Page

Hurray! Your EC2 instance was launched successfully.

Check EC2 Status Page

Connect to Your Instance from Mac or Linux

To connect to your EC2 instance from Mac or Linux using a standalone SSH client. Do the following:

  1. Open your command line shell

  2. Change the directory to the location of the private key file that you created when you launched the instance.

  3. Use the chmod command to make sure your private key file isn't publicly viewable.

    For example, if the name of your private key file is myKey.pem, use the following command:

    chmod 400 myKey.pem

  4. Use the following SSH command to connect to the instance:

    ssh -i /path/myKey.pem ubuntu@public_dns_name

Connect to Your Instance from Windows

To connect to your EC2 instance from Windows using a standalone SSH client, you need 3 tools:

  1. PuTTY - To start, stop, delete, or restart your application remotely.

  2. PuTTYgen - To convert your myKey.pem file to myKey.ppk file. Because PuTTY and Filezilla accepts .ppk file only.

  3. Filezilla - To upload all your files from your PC to EC2 instance.

Download PuTTY for Windows

Download PuTTY for your windows (Mine is 32-bit).

Download PuTTY Page

Download PuTTYgen for Windows

Download PuTTYgen for your windows (Mine is 32-bit).

Download PuTTYgen Page

Download Filezilla for Windows

Download Filezilla for your windows (Mine is 32-bit).

Download Filezilla Page

All required software to connect to Amazon EC2 instance from your windows is downloaded and installed.

Convert .pem file to .ppk file

To Convert your myKey.pem (that you created when you launched the instance) to myKey.ppk. Do the following:

  1. Open PuTTYgen and click on the 'load' button.

    PuTTYgen Software Screenshot
  2. Change the file type to 'All Files(".")' and locate your myKey.pem file.

    PuTTYgen locate file
  3. Click on your myKey.pem file. Now, the PuTTYgen Notice dialog box appears, click on the 'OK' button.

    PuTTYgen Generate ppk file
  4. Now, click on the 'Save private key' button.

    PuTTYgen Save Private Key
  5. Now, the PuTTYgen Warning window appears, click on the 'Yes' button to save your key file without a passphrase to protect it.

    PuTTYgen Warning Pop up Window
  6. Type the ppk file name of your choice and click on the 'Save' button.

    PuTTYgen Type .ppk file name
  7. Hurry! you have converted your myKey.pem file to myKey.ppk file successfully.

    PuTTYgen final output

Setting up Filezilla

Now we will set up Filezilla to upload our local files to the EC2 instance. To do so, please follow these steps:

  1. Open up filezilla and click on the menu FileSite Manager....

    Filezilla Open
  2. The 'Site Manager' Pop up window will appear. Click on the New site and type the name of your choice.

    Filezilla Sitemanager Open
  3. From the same window, do the following configuration:

    • Protocol ⟶ 'SFTP - SSH File Transfer Protocol'.
    • Logon Type ⟶ 'Key file'
    • User ⟶ 'ubuntu'
    • Key file ⟶ Locate your myKey.ppk file by clicking on the 'Browser...' button.
    • Host ⟶ Your EC2 instance 'Public DNS (IPv4)' name (follow next step).
    Filezilla Configuration
  4. Open up your EC2 instance Page from 'AWS Management Console' and do the following:

    1. Click on the Instances on the left side menu.
    2. Click on the Instance that you want to connect from Filezilla.
    3. Click on the Description tab in the bottom menu.
    4. Copy your Public DNS (IPv4) name.
    EC2 Public DNS Name
  5. Now paste the copied value to the Filezilla Host input box and click on the 'connect' button.

    Paste DNS Name and click Connect
  6. Now, the 'Unknown host key' window will appear. Tick the checkbox and click on the 'OK' button.

    Filezilla unknown host key window
  7. Hurray! You are connected to your EC2 instance via Filezilla.

    Filezilla Successfully Connected

Setting up PuTTY

Now we will set up PuTTY to access your EC2 instance. To do so, please follow these steps:

  1. Open up PuTTY

    PuTTY window
  2. Find your Host Name in your 'AWS Management Console' by clicking on the 'connect' button in your Instances page.

    Find EC2 Host Name
  3. The 'Connect to your instance' pop up appears. Copy the Host Name starting from '@'.

    My Host name is '@ec2-3-81-164-1.compute-1.amazonaws.com'

    Copy EC2 host name
  4. Now paste the copied host name to the PuTTY.

    Paste EC2 instance Host name
  5. Now click on the 'Data' menu under 'Connection' section in the left side menu.

    PuTTY Open Data section
  6. To fill the Auto-login username, find your user name from your 'AWS Management Console' by clicking on the 'connect' button in your Instances page.

    Ec2 Connect Button
  7. Copy your EC2 instance 'username'. In most cases your username will be 'ubuntu'.

    Copy EC2 User Name
  8. Paste your username in the 'Auto-login username' input box of PuTTY Configuration.

    Paste EC2 User Name
  9. Expand 'SSH' menu under 'Connection' section on the left side menu.

    Expand PuTTY SSH
  10. Click on the 'Auth' menu under 'SSH' section and locate your myKey.ppk by clicking on the 'Browse...' button.

    PuTTY Auth Menu
  11. Move to 'Session' section in the top of the left side menu and do the following:

    1. Type the session name of your choice.
    2. Click on the 'Save' button.
    Save PuTTY Session
  12. PuTTY Configuration is over. To Connect to your EC2 instance via PuTTY. Do the following:

    1. Open PuTTY
    2. Click on your Saved Sessions Name
    3. Click on the 'Open' button
    open saved PuTTY session
  13. Hurray! PuTTY is connected to your EC2 instance.

    Note: If your PuTTY is not connecting automatically. Please type your username ('ubuntu') manually or delete the saved session and reconfigure your PuTTY will fix it for you.

    PuTTY connected to your EC2 instance

Setting up Node.js Environment

In this section, we will setup Node.js environment for your EC2 instance. To do so, please follow these steps:

  1. Connect to your EC2 instance via PuTTY (saved session).

  2. Type the command 'curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -' and press the 'Enter' key to install NodeSource Node.js 10.x repo.

    Node setup command 1
  3. Type the command 'sudo apt-get install -y nodejs' and press the 'Enter' key to install Node.js.

    Node setup command 2
  4. Type the command 'sudo npm i express' and press the 'Enter' key to install express.js.

    Node setup command 3
  5. Type the command 'sudo npm i -g pm2' and press the 'Enter' key to install pm2.

    Node setup command 4

Node.js Environment is setup successfully.

Create Directory in your EC2 Instance

Create a directory ('new-website') by using the command 'sudo mkdir directory-name'.

EC2 Create Directory

Give Permission

Give permission to ('new-website') folder by using the command 'sudo chmod 777 /home/ubuntu/new-website', this will allow filezilla to upload your local files to that remote folder.

Give Permission

List Directories in you EC2 Instance

List all directories by using the command 'ls'.

EC2 Create Directory

Upload Files to EC2 Instance

To upload all your files to EC2 instance. Do the following:

  1. Open Filezilla and connect to your EC2 instance.

    Open filezilla and connect
  2. Once successfully connected. Open the 'new-website' folder in 'Remote site' section.

    open new-website folder
  3. Select all your required files from the 'Local site' section , right-click it and upload the files.

    Upload files to EC2 instance
  4. Now, all the uploaded files will appear on your 'Remote site' which indicates that the files are in your EC2 instance.

    Close the filezilla.

    files uploaded to EC2 instance
  5. For verification, lets check from PuTTY. To do so, please follow these steps:

    1. Open PuTTY and navigate to the 'new-website' folder using the command 'cd new-website'.

      Open PuTTY to verify
    2. Type 'ls' command to list all the files in the 'new-website' folder.

      Yes, the files are available.

      List all files in new-website folder
  6. Lets start our Node.js app using pm2.

    Type 'sudo pm2 start test.js' command to start your test.js application.

    Note: Make sure you are the in 'new-website' folder.

    Start Node.js app

    pm2 will display the status ('online') of the app your are running.

    pm2 status
  7. Open your 'AWS Management Console' and do to following:

    1. Navigate to the Instances.
    2. Click on the instance you are running the app
    3. Copy the Public DNS (IPv4) value.
    Copy Public DNS (IPv4) Value
  8. The copied Public DNS (IPv4) value is the URL. Paste the URL in your favorite web browser and press 'Enter' key.

    Hurray! your Node.js website/app is live forever.

    Live URL of your app

Related Tutorial

Learn Node.js

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author