This post is all about my first hello world to Node.js runs on Amazon EC2. I will note down the steps on how to get through Amazon EC2, start an instance, and how to download and build Node.js on Amazon EC2, and runs a Hello World Example!
This post includes nearly complete steps from how to setup Amazon EC2 and setup Node.js, it is recommended for all level of readers who don’t know Node.js and Amazon EC2, but at least you need to know how to use Linux and have basic programming skills.
First of all, you need a credit card to apply Amazon EC2, assume you are using Windows, you need a Putty to access Amazon EC2 Linux Server instance, and Puttygen, to generate your private key files.
Next, navigate to https://console.aws.amazon.com, login with your Amazon EC2 account, sign in to AWS Management Console, and navigate to EC2 Tab.
Steps:
1. To launch an Amazon EC2 instance.
Click on “Launch Instance” on the top left just under My Instances panel.
Then select “Community AMIs”, type “nodejs” on the keyword box, an AMI with ID ami-a8ca37c1 is shown, and click “Select”.
Then for “Instance type”, choose “Micro” for free tier (not need to pay for 1 year), or you can use Small instance (1 hour $0.087), I chose Small instance.
Click “Continue” at the bottom to navigate to next steps. For “Advanced Instance Options”, we not need to choose, just click “Continue” again. And then you will come to a screen with key – value pairs. Normally we not need to bother also, but for convenience you can type key (name) value (nodejs) to help you organize, browse your created instances, like a Tag.
Click “Continue” you will come to screen to create keypair. OK, choose “Create a new Key Pair” if there is no existing key pairs created before. As shown below, you can enter a easy-remembered name for your key pair, for example, I write “fyhaokey2”, and then click “Create & Download your Key Pair”, you should get a “.pem” file. Save it at some convenient location.
Now, open your Puttygen, click on Load, remember to choose (All File Types), and then select your “.pem” file. And then, click “Save private key”, you will get your “.ppk” file. “.ppk” file will be used with Putty later which used in authentication when you log in to Amazon EC2.
Back to your AWS browser, click on “Continue”, you will come to the screen to configure a firewall. You can select “default” OR, to ensure port 22 and port 80 is enabled, and then click “Continue” further to finish the process create the instance.
Once back to the main screen, you will see your instance’s status is in pending mode, wait a minute, it will become running mode.
Next step, we need to apply for “Elastic IPs”, click on “Elastic IPs” at the bottom left of the screen.
Let’s allocate a new address, and associated with your just created EC2 instance. Once done, back to main screen by click on “Instances” on the left menu bar. Click on your instance, see below, you will got something like this:
Let’s ping your elastic IP see whether it is on or not. By the time you reading this post, my elastic IP (50.19.125.189) had been released, so that this IP address is disabled, you may try your own.
2. To use Putty to access our EC2 instance.
At host name, write your elastic IP address, port remained 22.
Then, click on left side under “Category”, click “SSH” and expand and click “Auth”. Then, at right side, for “Private key file for authentication”, you browse your “.ppk” file, and then click “Open” to open the session.
And then, login as “ec2-user”, then “sudo bash” to execute command on behalf of “ec2-user”.
3. To install Nodejs setup.
Then, come to this link: https://gist.github.com/579814, see the first sh command file.
node-and-npm-in-30-seconds.sh (As below)
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc . ~/.bashrc mkdir ~/local mkdir ~/node-latest-install cd ~/node-latest-install curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 ./configure --prefix=~/local make install # ok, fine, this step probably takes more than 30 seconds... curl http://npmjs.org/install.sh | sh
Copy the contents, and execute it on your putty.
Wait for about 10 to 20 minutes, the nodejs will be installed completely.
4. To test our Hello World Nodejs !!!
OK, now we create a file called hello.js, with the content:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World Nodejs run on Amazon EC2 by fyhao\n'); }).listen(80); console.log('Server running on Amazon EC2');
and then,
we execute this, by type:
node hello.js
Then, you should see the Putty console with the text “Server running on Amazon EC2”.
OK, last step, open your favorite browser (for me Google Chrome), navigate to your created Elastic IP address, you should see the plain text you have made in hello.js !!!
WOW, finally, we done the first hello world job.
Have a nice try!
Hi there to every one, the contents existing at this site are actually awesome for people knowledge, well, keep up the
good work fellows.