The Building of Yeww: A WebRTC ProjectInstall node.js on Amazon Web Services

Posted on September 16, 2016 at 4:22 PM by Lynn Walker

Amazon Web Services, or AWS, is an impressive collection of services, many of which are available as part of the free-tier, a one year free trial.

Console

This lesson series won’t cover the process of signing up for AWS. The console, where we begin after sign-up and login, is a menu into each of the AWS services.

EC2

The prize of the free services are the server instances available in EC2, what they refer to as “Virtual Servers in the Cloud”. AWS offers 750 hours a month (a 31 day month has 744 hours) for each Linux and Windows based servers. The instances available in the free-tier have minimal resources but they are adequate for our development purposes.

Instances are available across eight global zones in nearly two dozen configurations.

This is the EC2 Dashboard, a menu into the virtual server and related services, a snapshot of your resource usage, and a status on service availability by zone.

Ubuntu Server

We will be launching an Ubuntu 14.04 instance to host node.js servers as well as apache. The node.js server will act as our chat server running on port 8000. Apache will be our http server on port 80. We will also install mongoDb to store the chat server data.

Today's tasks

We have enough today with Ubuntu, node.js, apache, mongoDb plus a few node modules, nodemon and forever. At that point we will be able to develop our chat service.

Launch a server instance in the cloud

Click on the blue button “Launch Instance” in the center of the EC2 Dashboard.

You will be presented with a choice of AMI’s (Amazon Machine Image) to choose from.

Select the checkbox labeled “Free tier only” at the bottom of the Quick Start menu on the left side of the page. When deselected you will see options that include installed applications for which a fee must be charged.

Choose Instance Type

In Step 1, select the Ubuntu Server 14.04 LTS - ami-e7b8c0d7, a 64 bit OS. You will be presented with the screen for “Step 2: Choose an Instance Type”.

In Step 2, the t2.micro instance type is selected by default. We will keep this selection as this is the only size that is free tier eligible. Click the “Review and Launch” button.

Review Launch Page

There are several pages of configuring instance details that can be paged through by selecting the “Next: ***” buttons on each page. We’ll be accepting default choices so we can go straight to the final page, “Step 7: Review Instance Launch”.

Click the “Launch” button. A modal dialog appears “Select an existing key pair or create a new key pair”.

Create new key pair

In the first drop-down box select the option “Create a new key pair”. Give the key pair a name in the second box and then select the “Download Key Pair” button. You’ll have to check a box to agree to terms and then selecting the “Launch Instances” button will finally launch your instance.

Launch Status

It will take a few minutes to prepare your instance. Click on the “View Instances” button to view the Instances page

View Instances

The Instances page has two main sections, a box at the top that lists all of your active instances and a lower, tabbed box which lists many of the details of a specific instance selected in the top box.

Common Instance Actions

From this instances page you can start, stop, reboot and terminate an instance by selecting the instance and clicking the appropriate “Action” menu item.

You can configure as many active instances as you like, and will only be charged for the hours that an instance is actually running. Terminating an instance is like deleting it, the instance will be returned to the pool of AWS resources available for configuration. I often have multiple servers configured for various purposes, but I stop them when I’m not using them so I don’t incur charges for going over 750 hours a month.

As a developer, I use AWS instances for experimentation with various applications and OS’s. It is not uncommon to run into issues. One downside to AWS is the limited free support. I find it is often easier to just start over with a clean instance, and AWS makes that option very easy. Terminate the problem server, and launch a new instance in minutes.

Configure Security Group

Before we can connect to the instance we need to edit the security group settings, to open the ports that we will need.

AWS creates your account with one security group named “default”. When launching an instance AWS creates a new instance, per instance, named “launch-wizard-1”, “launch-wizard-2”, “launch-wizard-{n}” unless you select an existing security group at step 6 or step 7 of the launch wizard.

In launching an instance we bypassed that step because I wanted to show how to edit security groups any time needed.

Editing security groups includes selecting which security group is associated with a specific server instance, and secondly, the settings of an individual security group.

View Security Groups

From the Instances page select either the Security Group item from the left panel menu or click the named security group in the details box for your selected instance.

Security Group Details

Next, in the security group page, make sure your security group is selected, then select the “Inbound” tab in the lower panel showing details for the selected security group.

Edit Security Group

Now you are looking at the inbound rules for the selected security group. Click the “Edit button. By default, the Linux instances come with port 22 opened for SSH, accompanied by a warning in the final review page of the instance Launch-Wizard that you should secure this port. That is one part of what we will be doing now. On Windows instances, RDP port 3389 is opened instead.

Edit Rules

A modal dialog appears showing the group’s current inbound rules, in this case one, for SSH. Select the “Source” drop-down box, displaying “Anywhere” by default, and choose the option “My IP”.

Your IP address will be updated in the box to the right of the drop-down. Select the “Add Rule” button.

We will add 2 rules, both TCP, one for port 80 (HTTP) and one for port 8000, which we will use for our node.js chat server. Select the “Type” drop-down box in the last row, originally showing “Custom TCP rule” and choose the “HTTP” option. Leave the rest of the settings as they are and click on the “Add Rule” button again.

In the “Port Range” text box of the last row, replace the current value “0” with “8000”.

Save Rules

Leave the rest of the settings as they are and press the “Save” button.

Select Instances Menu

Select the “Instances” menu from the panel at the left side of the page.

Connect

Select the Ubuntu instance if not already selected. Click on the “Connect” button. You will be see a modal dialog that looks like this:

The following instructions will rely on “A standalone SSH client” option. Selecting “A Java SSH client directly from my browser” option, the instructions may vary slightly.

Change PEM file attributes

Open a terminal window in the directory where the previously downloaded .pem file is located and change the file attributes of the .pem file.

chmod 400 nodejs.pem

Connect to server in the cloud

Connect to the Ubuntu instance via ssh:

ssh -i nodejs.pem ubuntu@54.68.234.150 (your IP and pem filename values will/may differ).

Response from Ubuntu server on AWS

If you get a timeout error, it is most likely due to the security group rules blocking you out. Check the inbound rules of the security group associated with this instance and make sure that the rules for SSH are either set to “My IP” (and you haven’t changed location since setting this), or “Anywhere”.

Install Node.js

The rest of today’s session will be spent typing commands into the terminal window as we administer the Ubuntu server remotely by ssh.

Update server

First some server updates. During each of these steps, many files will be downloaded onto your computer. In some cases, builds may be performed to generate a required binary. Many messages will scroll across your screen.

sudo apt-get install -y python-software-properties python g++ make

sudo add-apt-repository ppa:chris-lea/node.js

sudo apt-get update

For those unfamiliar with sudo, it executes the command that follows it with administrator privileges.

Node.js

Now for the good stuff. To install node.js execute this command:

sudo apt-get install nodejs

Verify installation

Now, to verify that it has been installed successfully, execute this command:

node –v

You should see a version number displayed

Also type

npm –v

You should see a version number for the node package manager, too.

If you see version numbers for both node and npm then you have achieved success to this point. Only a few more easy steps to complete today’s tasks.

Install Apache

One command:

sudo apt-get install apache2

Verify installation

We previously viewed the connection properties for our instance, which included the public IP.

Open a browser window and enter this public IP address into the URL bar. You should get something that looks like this:

If you don’t, either apache has not been installed successfully, or port 80 isn’t open to the public in the server’s security group. Or maybe you entered the url incorrectly. Review the security group rule for port 80, and make sure it is set to “Anywhere”. I’m not going to go into the process of troubleshooting the apache install.

Install mongoDb

Don't ask a bunch of questions, just type these commands. (meaning: it's mysterious voodoo stuff I don't understand. It just works.)

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Then we'll execute the familiar update command again.

sudo apt-get update

Now we'll finally install mongoDb.

sudo apt-get install -y mongodb-org

We'll cover basic mongo administration in a later lesson on persisting chat sessions.

Create a project directory for the chat feature

sudo mkdir dev

cd dev

Install a few development dependencies

sudo npm install -g nodemon

sudo npm install -g forever

Adding the g switch "-g" to our install commands installs the libraries globally. They will be accessible in each project folder.

Wrap up

Great job. If you made it through the entire lesson and everything was installed successfully then you are ready to go with our next lesson, some proof of concept tasks with socket.io. We’re going to start working with code!


Previous lesson

Next lesson

Don't miss these coming lessons

Later in the series we will create a second node server to support our video chat feature.

Also coming later in the series we will launch an instance of a Windows Server 2012, or possibly 2008R2, to host web services as REST endpoints, created with the ASP.NET Web API.

Previous lesson

Next lesson