Setup Node and Express on AWS EC2

PJ Wang
CS Note
Published in
5 min readNov 3, 2017

[Part I ] Setup AWS EC2

Step 1: sign in/sign up (Amazon web service)

Step 2: Click services after sign in.

Step 3: Click “Running Instances”.

Step 4: Click “Launch Instance”.

Step 5: Choose an Amazon Machine Image (AMI), and click “Select”.

Step 6: Choose an Instance Type, and click “Next: Configure Instance Details”.

Step 7: Click “Next Add Storage” directly.

Step 8: Click “Next Add Tags” directly.

Step 9: Click “Configure Security Group” directly.

Step 10: Click “Add Rule" and insert the data, and click “Review and Launch”

Step 11: Click “Launch”

Step 12: Create a new key pair, and download the file. Finally, click ”Launch Instances”

Step 13: Wait for 5 minutes

[Part II] Connect to AWS

Windows:

Step 1: Download PuTTYgen and PuTTY

Step 2: Rebuild .pem to .ppk by PuTTYgen

Step 3: Open PuTTY and set parameters

Step 4: Success

MacOS / Linux :

$ ssh -i "key.pem" ubuntu@ec2-18-220-186-193.us-east-2.compute.amazonaws.com

if show the error, use sudo

$ ssh -i "key.pem" ubuntu@ec2-18-220-186-193.us-east-2.compute.amazonaws.com

Key commandyes

Successful

[Part III] Install Node and Express

Step 1: Update the latest package

ubuntu@ip-172-31-17-13:~$ sudo apt-get updateubuntu@ip-172-31-17-13:~$ sudo apt-get install libssl-dev g++ make

Step 2: Get the file link(Website)

Step 3: Download Node package and extract the file

ubuntu@ip-172-31-17-13:~$ wget https://nodejs.org/dist/v8.9.0/node-v8.9.0.tar.gz
ubuntu@ip-172-31-17-13:~$ tar -xvf node-v8.9.0.tar.gz

Step 4: Install Node

ubuntu@ip-172-31-17-13:~$ cd node-v8.9.0
ubuntu@ip-172-31-17-13:~/node-v8.9.0$ ./configure && make && sudo make install

if show the error: “./configure 4: exec: python not found”

ubuntu@ip-172-31-17-13:~$ sudo apt-get python
ubuntu@ip-172-31-17-13:~$ sudo apt-get python3

Step 5: Test NodeJS

ubuntu@ip-172-31-17-13:~$ mkdir test1
ubuntu@ip-172-31-17-13:~$ cd test1
ubuntu@ip-172-31-17-13:~/test1$ vim test1.js

Type the following code on test1.js

var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);

Execute test1.js

ubuntu@ip-172-31-17-13:~/test1$ node test1.js
Listening on port 9000

(option) If show the error

(option) Correct it by following instructions

ubuntu@ip-172-31-17-13:~/test1$ sudo node test1.js

Open your browser

Website will break down if you close the CMD, so you do the following steps.

ubuntu@ip-172-31-17-13:~/test1$ nohup node test1.js
[1] 6916
ubuntu@ip-172-31-17-13:~/test1$ nohup: ignoring input and appending output to 'nohup.out'
^C
ubuntu@ip-172-31-17-13:~/test1$ ls
test1.js nohup.out
ubuntu@ip-172-31-17-13:~/test1$ rm nohup.out

If you want to break down your website.

ubuntu@ip-172-31-17-13:~/test1$ ps -ef
ubuntu@ip-172-31-17-13:~/test1$ kill 6916

Step 6: Install Express

ubuntu@ip-172-31-17-13:~$ sudo npm init

Insert the following questions

package name: 
version: (1.0.0)
description:
entry point:
test command:
git repository: https://github.com/USERNAME/PROJECT.git
keywords:
author:
license: (ISC)

After that

ubuntu@ip-172-31-17-13:~$ sudo npm install Express

Step 7: Test Express

ubuntu@ip-172-31-17-13:~$ mkdir test2
ubuntu@ip-172-31-17-13:~$ cd test2
ubuntu@ip-172-31-17-13:~/test2$ vim test2.js

Type the following code

var express = require('express');
var app = express();
app.get('/',function(res,req){
res.send('Hello world(test2)!\n');
})
var port = 80;
app.listen(port);
console.log('Listening on port', port);

Execute test2.js

ubuntu@ip-172-31-17-13:~/test2$ sudo node test2.js
Listening on port 80

And open your browser

Reference

[1] https://www.youtube.com/watch?v=WxhFq64FQzA

[2] https://www.cyberciti.biz/faq/bash-python-command-not-found/

[3] https://stackoverflow.com/questions/16827858/npm-warn-package-json-no-repository-field

[4] http://weaintplastic.github.io/web-development-field-guide/Development/Frontend_Development/Setting_up_your_project/Setup_Dependency_Managers/Node_Package_Manager/Initialize_NPM_on_a_new_project.html

--

--

PJ Wang
CS Note

台大資工所碩畢 / 設計思考教練 / 系統思考顧問 / 資料科學家 / 新創 / 科技 + 商業 + 使用者