Friday, January 8, 2016

Node.js running on Amazon EC2

Recently Amazon introduced t2.nano instances and I want to try if it is possible to run node.js application on this low cost instances.

Create Amazon EC2 instance

Login to Amazon AWS console and create new EC2 instance. I wanted to check if it is possible to use ultra low cost t2.nano. I also used default option Amazon Linux.

Be sure that in Step 6 Security Group you will allow to connect using HTTP and HTTPS from anywhere and SSH from your IP (or another IPs you use).

TypeProtocolPort RangeSource
HTTPTCP800.0.0.0/0
HTTPSTCP4430.0.0.0/0
If you will forget, you can do in later in NETWORK & SECURITY / Security Groups (Inbound tab).

Optional - Go to INSTANCES /Instances and click on newly created instance.  Set the name - e.g. node.js test.

Next attach public IP to instance - NETWORK & SECURITY / Elastic IPs / Allocate new Address. Then use this IP as public IP for the new instance. Right click on new IP and select Associate Address. In instance field attach newly created instance. 

Go back to INSTANCES /Instances and click on newly created instance and right click on it and select Connect. Follow the instructions and connect to your instance using SSH.


Update yum (sudo yum update) and install node.js.

Instructions:
https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora

TLDR:
Run as root on RHEL, CentOS or Fedora, for Node.js v4 LTS Argon:

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -

yum install -y nodejs 

EPEL info:
https://aws.amazon.com/amazon-linux-ami/faqs/

TLDR:
Q: How do I enable the Extra Packages for Enterprise Linux (EPEL) repository?

Modify /etc/yum.repos.d/epel.repo. Under the section marked [epel] , change enabled=0 to enabled=1.


Verify installation:

Run on instance:
# node -v
v4.2.4

Create Node.js application


TLDR:
$ npm install express-generator -g
$ express testec2 

Initialize
cd testec2 && npm install

Set port 80
vi app.js

At the end add
app.listen(80, function () {
  console.log('Example app listening on port 80!');
})

Start application
npm start

Type public IP to browser and see default nodejs/express application


Run on background

Use forever

TLDR:
[sudo] npm install forever -g
[sudo] npm install forever-monitor
forever start app.js

List of background:
forever list
Stop running app:
forever stopall
or 
forever stop ID


No comments:

Post a Comment