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
Post a Comment