How to Deploy Node.js App in EC2 instance?

How to Deploy a Node.js Application or Website in EC2 instance?

In this post, I will show you how to deploy a node.js app (website) in AWS EC2 instance.

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

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

Launch an EC2 Instance

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

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

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

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

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.

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'.

Step 4: Add Storage

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

Step 5: Add Tags

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

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)

Step 7: Review Instance Launch

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

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.

Launch Status

Click on the 'View Instances' button.

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

Hurray! Your EC2 instance was launched successfully.

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 PuTTYgen for Windows

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

Download Filezilla for Windows

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

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.

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

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

  4. Now, click on the 'Save private key' button.

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

  6. Type the ppk file name of your choice and click on the 'Save' button.

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

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....

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

  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).
  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.
  5. Now paste the copied value to the Filezilla Host input box and click on the 'connect' button.

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

  7. Hurray! You are connected to your EC2 instance via Filezilla.

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

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

  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'

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

  5. Now click on the 'Data' menu under 'Connection' section in the left side menu.

  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.

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

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

  9. Expand 'SSH' menu under 'Connection' section on the left side menu.

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

  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.
  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
  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.

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.

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

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

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

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'.

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.

List Directories in you EC2 Instance

List all directories by using the command 'ls'.

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.

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

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

  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.

  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'.

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

      Yes, the files are available.

  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.

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

  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.
  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.

Related Tutorial

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.

Meet the Author