Running and testing Node.js, Express and MongoDB on Amazon Web Services

This tutorial will guide you trough setup process of Ubuntu on AWS. After that you will learn how to setup Node.js, Python, Express and some other interesting things too. This will show some basics, so if you are pro, come later when something better appears. 🙂

Let start by creating account on

Select free tier. After you confirmed your identity, login in and go to EC2 management console. You will see there your running instances. Your view will be empty son we should create new one. Click on “Launch instance”.  You will get new windows with machines. Choose Ubuntu whichever version it is. Type you can leave default and every other following step until you come to “Configure security group”.

Click on create new security group and add it name “Node” and description “Node”. SSH protocol you already have, so what i left is HTTP and HTTPS to add. After you added them, add Custom TCP rule with port 8080 and another one 9000. For all of them make sure that source is “Anywhere”.

After you finished and launched instance, you will get new window for key pairs. You should create new key pair add name as node and download it. It is really important to download this file on some known location, because you will use it later most of the times.

Now that you created new instance you will maybe need to wait for a few minutes for process to be finished. You can go to EC2 management and found you instance there.

Now you have to open Terminal and type “ls”. It will list al of your folders. Find the one that you downloaded it to. For example we will imagine that we downloaded it to Desktop. Type “cd Desktop”, and you will change folder from “home” to “Desktop”. Now type “ssh -i node.pem ubuntu@..” and now add address from your server. You can find that when you click on your running instance. Mine instance is and my address is You can use one of there but not both and add them to command. Command will look like this:

ssh -i node.pem

Now you will get one error which is connected to permission of the key that you already have. Another thing is that key may have another extension like .txt instead of .pem. Delete any other extension and leave only .pem.

Go back to terminal and type “ls -l” and you will get complete list of files on Desktop with all theirs permissions and owners. You can find there your file with all of its parameters. Let us change those parameters now. First lets set the owner. Type “chown :stuff (be carefull here, on my machine is stuff, on you maybe different. Look at the owner of other files and type it here. Off course, do not include this text in brackets it that command) node.pem”. Now type “ls -l” and you will se that owner is changed. Maybe that file already had an owner so nothing has changed. Next is to change permissions. Type “chmod 600 node.pem”. Type “ls -l” and permissions are changed now. Now we can login at our server.

Type this again “ssh -i node.pem” and you will be able now to enter your server.

Now that you entered your server, good practice is to update it. Type “sudo apt-get update” and wait a little bit until it is finished.

Next step is to install python. Type “sudo apt-get install python” and enter. Python will be installed and you next step can be executed.

Type “sudo apt-get install libssl-dev g++ make” and when asked type “yes” to confirm. Now we can compile everything that we need. In the meantime go to Node.js website, to downloads section and copy link of the source code. This is the current link. “node-v6.9.5.tar.gz” on their website. It will be probably updated so be careful.

Now lets download it by typing “wget full-link-that-you-copied”. Type “ls” and you will see it downloaded on your PC. Now you have to extract it by command “tar -xvf node-v6.9.5.tar.gz”. Type “ls” and you will see that you got new directory. Go in to that directory with command “cd node-v6.9.5/”.

This next step is not possible if you did not install the Python, so go back and do it again. Now you will execute binaries, make and install them with this command:

“./configure && make && sudo make install” (this might take a while)

Type “ls” and you will see a lots of different files and folders created. Now type “cd ../” to go back in to former folder. Now you will create new directory with: “mkdir test1“. Then go into that folder “cd test1“. That will be folder for our project. We will create now new file with VIM. Google it for more information 😛

vim test1.js

Now you have new file. If you want to type something you have to press letter I. With that you will enter insert mode. If you want to exit Insert mode, just press Esc.

Now copy and paste this code into the file you just created.

var http = require(‘http’);

var port = 9000;

http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type’: ‘text/plain’}); res.end(‘Hello there, world!\n’);

console.log(‘Listening on port’, port);

Now to save it type “😡“. This piece of code is self explanatory. It will print Hello world and it will be on port 9000. If it is successfully executed it will print log.

Now you can run it with this command: “node test1.js“. Go to your EC2 in AWS and click on your running instance and copy public NDS or Public IP address and paste it in your browser. Dont forget to add :9000 to specify the port. You should see the message Hello world and that means that you successfully installed and and configured your Node.js server. But there is one catch. If you close your running Terminal, and refresh site, it will be unavailable. Wee need to trigger it somehow to work without interruptions. To achieve that we will use NOHUP. With this command we will run it as background process. Type “nohup node test1.js &“. To see if that process is in the background you can check it by typing “ps -ef“. Now you can close and reopen terminal and process will still be in the background. If you need for some reason to stop that process, just type “kill IDoftheprocess“.

Now it is time install Express. Express is fast, unopinionated, mimalist web framework for Node.js. Run the terminal, login to your server and type “sudo npm install express“. Press enter and that is it. Create new directory, called it Test2. Go to that directory, use vim to create another file “vim test2.js“. Press “I” and paste this:

var express = require(‘express’);
var app = express();

app.get(‘/’, function (req, res){ res.send(‘Hello there, world!\n’);

var port = 9000;
console.log(‘Listening on port’, port);

Save it and run it. Now go to web, type address again, add port number and you will see it running. Use nohup  again or jus write sudo node test2.js &  and press enter.

Now the big question. Why are we using port 9000. One of the reason is for you not to type sudo  before every command and other one is for security reasons. We don’t want to add :9000 every time we enter our site, so we will reroute that 9000 to 80 by this command:

“sudo iptables -A PREROUTING -t nat -i eth0 -p tcp –dport 80 -j REDIRECT –to-port 9000”

Now when you run the site again without selecting the port, it will work. Next step is to create MongoDB. Type:

sudo app apt-get install mongodb

It will ask for your password, or not, and then you will be asked to confirm. Press Y and that is it. After process is completed machine will be started. Smart thing to do is to update repositories. Type  sudo apt-get update.

Once update is completed, just in case, yuo can start service. Type sudo service mongodb start and press enter. Now type mongo and you will get some information as shell, connection and help. That is all for this lesson today. Next time, we will connect everything and test it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s