Skip to main content

How to Deploy Node JS App on AWS (Amazon Web Service) - Step by Step - can be deploy any App

 


hello coders , Am Vasu Birla and today we will learn how to deploy Nodejs or any App on AWS (Amazon Web Service );

1.  Log in to AWS account

2.  Select EC2 service in Services section

3. Create / Launch Instance  (select your Instance Type - Ubuntu or choose your own)

4. After Launching Instance open it and copy public IP address

5. Open Putty App on your local machine

6. in Host name give host name - (it can be given by two ways )
    -  ec2.user@0.0.0.0  (use your instance public ip instead of 0.0.0.0)
                 or
    -   ec2-0-0-0-0.ap-southeast-1.compute.amazonaws.com

7. also in putty app go to connection tab -> SSH -> Auth -> Credensials -> Browse and open your key pari (.ppk or .pem file)

8. if you succeced if previous step terminal will be open -   run command  'ls'  it will show nothing
    because we didn't put our project folder here .

9. now on your local machine install filezilla or winSCP ....

10. Open Your WinSiP -  create new site ->  select SFTP -> give hostname (only public ip address )
    -> port(22) -> open advance -> browse for your key pair file only .ppk file
11. if failed use it without username  only use login file (.ppk)
    if you have .ppm file then convert it using puttyGen software . load and save private file .
12.  default username for ubuntu OS is  ubuntu or ec2-user  
13. now   on winsip there are two portion -  left side is your local machine and right side is Amazaon machine

14. copy paste your project to Amazon Machine from your local machine you can also do drag and drop

15. Now on the terminal of putty - run   ls command - and you will see your project folder their  

    # ls   (dont use # only ls is command)

16. now we have to install node enviroment on AWS instance for this run following commands

    # sudo apt update

     # sudo apt install npm

    # curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh

    # nano /tmp/nodesource_setup.sh

    # sudo bash /tmp/nodesource_setup.sh

    # sudo apt install nodejs

    # node -v  

    Output
    v16.19.0

17.  make this  accessibale to public network  - select your instance -> Network & Security
    -> Security Group -> Create Security Group - Give name for example 'kilvish'

    select inbound rules - Type - All traffic - > Source -> Anywhere or ip 0.0.0.0/0
    Add Rule

18 . now go to Your Instances -> check your  instance -> Action -> Security -> Change Security -> add that group 'kilvish' and then Save it .

19. now on terminal - run your app by your own script - i always give script in package.json -  like  npm run dev app.js or npm run start

    npm run start -  now your app is live
    open Chrome Browser in address bar access   YourinstanceIP:Port   support your instance ip is 20.21.22.23 and port is 3000 then  use it on
    chrome like   20.21.22.23:3000  it will open your node js application with all templates and designs

20. But here is the problem  your Node app is live until you close your terminal ,

    for make your node app live for ever - run following commands

    # npm install pm2 -g  
        if it give error  use sudo infront of it for administrator access
    # sudo npm install pm2 -g


    # pm2 start app.js
    # pm2 stop app

if you want to change something in code , do it and restart it -
run following command

    # pm2 restart app

    if you want to see list of app process list by pm 2 run following command
    # pm2 ls
if you want to delete pm2 app run following command
    # pm2 delete app

Now your APP is live without running  terminal command



23 open package.jsson  and put it in script

   "pm2-start": "pm2 start ./bin/www"


24.  alternative of pm2 is  forever pacakge to use this -  but do not use it if you already done pm2 start

    # sudo npm i -g forever

   #  forever start app.js   or  #  forever start src/app.js

to stop
   # forever stop app.js




   //----------------Finish ------------------------------



Note - To connect with Database

1. For MongoDB -
use mongodb cloud link instead of localhost link -
to get cloud link - register on mongodb cloud - and create free cluster and get link -



2. For MySQL -
To connect with MySQL database using nodejs - create Database Intance on AWS
using RDS - and then connect it on terminal using following command -
~ $ mysql -h kilvishDB.xxxxxxxxxx.ap-southeast-2.rds.amazonaws.com -u root -p
(RDS database instance must have public access - dont connect it to EC2 instance )


Enter password: 
Server version: 5.6.40 Source distribution

MySQL [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| something          |      <<<< Here is my "default" database
| sys                |
+--------------------+
6 rows in set (0.04 sec)

If your Database is not there then create new  database using following command - 
#  create database kilvishDB; 
to check again run   SHOW DATABASES ;  command - 

+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| something          |      <<<< Here is my "default" database
| sys  
! kilvishDB          !
             |
+--------------------+
now in your config.js file 





If you were connecting to a database, you could additionally add
 a “database” field under port, with the value set to the name of the database.
  • Host is the Endpoint
  • User is the Master Username
  • Password is the Master Password
  • Port is the port you specified, such as 3306


download mysql workbench - connect your database - import your sql file

I prefer using MySQL workbench. It's much more easier & user friendly than the command line way.

It provides a simple GUI.

MySQL workbench or SQL Yog.

These are the steps that I did.

1) Install MySQL Workbench.

2) In AWS console, there must be a security group for your RDS instance. Add an inbound rule to that group for allowing connections from your machine. It's simple. Add your IP-address.

3) Open MySQL workbench, Add a new connection.

4) Give the connection a name you prefer.

5) Choose connection method- Standard TCP/IP

6) Enter your RDS endpoint in the field of Hostname.

7) Port:3306

8) Username: master username (the one which which you created during the creation of your RDS instance)

9)Password: master password

10) Click Test Connection to check your connection.

11) If connection is successful, click OK.

12) Open the connection.

13) you will see your database 'realcardiodb' there.

14) Now you can export your mysqldump file to this database. Go to-> Server. Click Data Import.

15) You can check whether the data has been migrated by simply opening a
blank SQL file & typing in basic SQL commands like use database,
select * from table;

//-----------finish databse connectivity --------------




Comments

Popular posts from this blog

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  ...

How to deploy NodeJS app on server with Apache2 and acess it with Server IP addresss - Node JS deploy project Live.

     hello friens , This is Vasu Birla , in previous post we have seen the deployement of NodeJS app on AWS instance server instance but AWS server is expensive than other servers . SO today we will use simple Ubuntu server for making Live out Project using Apache2 . At the end you will be abe to access your NodeJS app using server IP address from anywhere  .. 1.  Login to your server using SSH terminal . (in AWS part i already explained how to do this )     -> Open your putty in hostname put your server IP -      login with ssh username ->root and password     (if you dont have root username and pass ask your      provider or reset it from cpanel or hosting panel ) 2. after login - on terminal you can Put your project anywhere.     There are two ways to put your project folder on server location     (i) - > using Github - (very popular and easy to track your everyday code changes t...

GitHub Repo Collaboration Work on single project

 =============================================== To collaborate effectively with your friend on the same project, you should use Git branches to manage different lines of development. Here's a step-by-step procedure you can follow to streamline collaboration: 1. Create Separate Branches for Each Developer Create a New Branch for Your Friend: On your local repository, create a new branch for your friend. For example if your friend name is kilvish , if you want to create a branch named kilvish , you would run: make sure you would be on main branch already   command->  git checkout -b kilvish git push origin kilvish 2. Set Up Your Friend’s Environment( On your Friend's System )  at kilvish side  run ->  Clone the Repository (if not already done): If your friend hasn’t cloned the repository yet, they should do so: command ->  git clone https://github.com/Vasu-Birla/your-repo.git   // your your main clone line  Fetch All Bran...