By Daniel Du
Node.Js is very popular in web programming, you can use single language - JavaScript - to create the back end logic and the front end web page. Installing NodeJs environment on Mac or Windows is relatively easy, while if I need to put my Node.Js sample into production, I will have to set it up on Linux. Of cause I can choose Windows to host Node.Js application, but I prefer to choose the free Linux, which is a good practice for me as well.
I am using AWS EC2 as my host. Firstly, I need to launch an EC2 instance on AWS. I will use Amazon Linux AMI here.
I’d like to start from t2.micro, which is free tier eligible. Once I find it not powerful enough for my application, I can scale up or scale out easily.
for the 3rd step, I use the default settings:
Step4, add a Elastic Block Storage, I just use the default setting, and I can add another EBS storage latter if necessary.
Step 5, add some tags so that I can recognise this server easier, it is useful especially when you have many instances running.
Step6, Configure the security group, which is similar like firewall on AWS side, which is a whitelist, only allowed port can pass though. My server is supposed to be web server, so I add HTTP/80, and to connect to the server and do some configuration work, I also enabled SSH/22. I can remove this once the configuration is done.
Once I review the instance settings and they are fine for me, now I am ready to launch it. But before that, I need to create a key pair so that I can connect to this serve latter. Be sure to download the .pem file and save it to somewhere secure.
Now, it is ready to go. After a few minutes, my Linux server is up and running.
Now I will connect to it and install the node.Js environment into it. To connect to the EC2 Linux instance, I need a SSH client. For Mac, the built-in terminal is good enough for me, for Windows, you may find Putty useful. Actually if I got the EC2 console, select the instance and click the “connect” button, AWS will tell me how to connect to this server:
- Open an SSH client. (find out how to connect using PuTTY)
- Locate your private key file (aws-ipg-cp-adn-devtech.pem). The wizard automatically detects the key you used to launch the instance.
- Your key must not be publicly viewable for SSH to work. Use this command if needed:
chmod 400 aws-ipg-cp-adn-devtech.pem
- Connect to your instance using its Public IP:
54.67.70.195
ssh -i aws-ipg-cp-adn-devtech.pem [email protected]
As indicated, I connected to my Linux server from my Mac terminal with SSH:
Now I am ready to install Node.Js environment. Firstly run ‘yum update’:
$ sudo yum update
Next, I will go ahead to install node.js from source code:
$ sudo yum install gcc-c++ make
$ sudo yum install openssl-devel
$ sudo yum install git
$ git clone git://github.com/joyent/node.git
$ cd node
The source code of Node.Js is downloaded to my linux server.
To check out one version of Node, I view all available Node tags with following command:
$ git tag -l
I’d like to use Node 0.10.32, so I check it out with following command, you can choose your favourite version. And then I start installing it from source code, it may take a few minutes to complete.
$ git checkout v0.10.32
$ ./configure
$ make
$ sudo make install
Now my Node is installed on my Linux server :
I also need to install some useful NPM packages, but when I do that, it reminds me that the command is not found.-
$ sudo npm install express
sudo: npm: command not found
[ec2-user@ip-172-31-25-230 node]$
so I need to add it to sudo’s path so that I can install more packages.
$ sudo vi /etc/sudoers
Append the /usr/local/bin to the secure_path as below:
After saving this file and exit to the command line, I can install express and others:
It seems the environment if one, but to confirm,I need to create a very sample Node.Js application to verify the environment works fine.
[ec2-user@ip-172-31-25-230 ~]$ mkdir firstnodeapp
[ec2-user@ip-172-31-25-230 ~]$ cd firstnodeapp/
[ec2-user@ip-172-31-25-230 firstnodeapp]$ ls
I created the Node.Js sample on my local machine and then upload the source code to Linux server. BTW, I use CyberDuck to upload files into EC2 Linux, which is very helpful. Be sure to check “use public key authentication”, it will ask you the *.pem file, which is downloaded from AWS when creating the instance. Once I connect to the EC2 instance from CyberDuck, I can drag and drop files into it to upload them.
A few seconds latter, my files are uploaded, let’s go back to the terminal of EC2 instance to start the Node.Js application.
[ec2-user@ip-172-31-25-230 testnodeapp]$ ls
app.js bin package.json public routes views
[ec2-user@ip-172-31-25-230 testnodeapp]$ npm install
[ec2-user@ip-172-31-25-230 testnodeapp]$ chmod -R +x .
[ec2-user@ip-172-31-25-230 testnodeapp]$ sudo DEBUG=testnodeapp ./bin/www
Now the Node.Js application is running (I am using 80 port when I created the Node.Js sample) , I did not use the default 3000 port because my security group in AWS does not allow it. Yes, I can edit the security group to add 3000 port, but HTTP/80 makes more sense to me for a web server. Now I can now access it from browser. It is just a samplest express site, but the Node.Js environment looks fine now.
Next step, I will create a new AMI based on this instance, I do not want to repeat all these steps every time when I need a NodeJs Linux instance :) Go to the AWS Console, right click the instance and choose Image -> Create Image:
Give it a name:
Now the AMI is being created. if go to Images -> AMIs from left panel, it shows my custom AMIs.
OK, I wil stop here, next I can start up a new instance with Node.Js environment ready by launching an instance from my customised AMI, and more important, I can launch more instances like this to do auto scaling latter.