28

I'm interested in hosting nodejs applications in a cloud and I'm looking for a free cloud hosting for my purpose. I've found that Amazon has one but I have the following question: Are there any tutorials around how I can set up and run nodejs application in Amazon EC2?

EDIT: Can you provide any good hostings for nodejs (except heroku)?

Erik
  • 14,060
  • 49
  • 132
  • 218
  • You can have Micro Instance for 1 year for free. Micro Instance type is very slow. You install Node.js on EC2 exactly the same way if it were real Server. – Roman Newaza May 14 '12 at 06:20

8 Answers8

43

I've been using Node.js with Amazon EC2 for a while and was quite happy with both of them. For the moment AWS seems to be the cheapest and the most robust cloud provider, so picking up Amazon wouldn't be a mistake. There's nothing special about running Node.js in the cloud - you work with it like if it were your own PC. Below are some general steps to follow for the simplest Node.js application running on EC2 Ubuntu server:

  1. Create Amazon EC2 account.

  2. From AWS console start t1.micro instance with any Ubuntu AMI (example).

  3. Login via SSH to your instance.

  4. Install node.js: sudo apt-get install nodejs

  5. Create new file test_server.js with the following content:

    require("http").createServer(function(request, response){
      response.writeHeader(200, {"Content-Type": "text/plain"});  
      response.write("Hello World!");  
      response.end();
    }).listen(8080);
    
  6. Start the server: node test_server.js

  7. Check it's working from another console: curl http://localhost:8080

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
nab
  • 4,751
  • 4
  • 31
  • 42
  • 5
    Also make sure to allow incoming traffic on port 8080 in your security group. You might also need something like [forever](https://github.com/nodejitsu/forever) to make your server persistent. – Blaise Feb 04 '13 at 12:41
  • 1
    This was helpful, but I also found simple things like installing a vaguely current nodejs (and indeed npm) to be something of a chore. (It didn't help that nodejs.org was down much of last night.) – podperson Oct 10 '13 at 13:42
9

Check out these tutorials (updated for 2021)

  1. How to Deploy a Node.js Application On AWS EC2 Server
  2. How to Deploy a Node.js application in AWS EC2
  3. How To Deploy Your Node.js App On AWS With NGINX And SSL
almypal
  • 6,612
  • 4
  • 25
  • 25
  • I used your second link, substituting node.js version 0.10.24 where the author has 0.6.8. It was a piece of cake. – Cᴏʀʏ Jan 20 '14 at 20:09
  • 2
    The first link seems to be dead. The second link is fine until you get to npm install, where it's fatally broken. The third link points to an article seems to no longer exist. – canadiancreed Aug 13 '14 at 14:31
  • Links 2 doesn't work now. Please delete your answer or fix the links. – Adam Gawne-Cain Jan 06 '22 at 09:27
6

Based on this tutorial, here's an updated step by step:

1) Make an account on Amazon Web Services.

2) Create an EC2 instance; I chose Ubuntu micro.

3) Configure Security Group (name it "Node") and add ports:

HTTP (80), HTTPS (443), and a custom TCP port for your Node app (e.g. 3000)

4) Launch the instance and save the pem file (private key), e.g. "node.pem".

5) On Windows - install Cygwin + OpenSSH package. it is also recommended to install WinScp to have "explorer like" access to the linux.

6) Open Cygwin Terminal as Administrator, and set correct permissions to "node.pem" file:

chown :Users node.pem
chmod 400 node.pem

7) Find your EC2 instance public DNS name in the EC2 dasboard, and connect to it with SSH:

ssh -i node.pem ubuntu@{your EC2 public DNS name}

8) Update Ubuntu and install NodeJS:

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

9) Copy your NodeJS application into the EC2 instance (via Cygwin, or Winscp).

10) Install all of your Node app required modules:

cd /home/ubuntu/My_Node_App
npm install --save

11) Re-route ports with IPtables so that your app can be accessed on default http port 80:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000

To view the iptables routing entries, run:

sudo iptables -t nat -L

If you need to remove routing entry (first line), run:

sudo iptables -t nat -D PREROUTING 1

12) Run your app as a background process:

sudo nohup node app.js &

To kill your app process:

ps -ef | grep app.js
sudo kill {proccess id number}
Noam Manos
  • 15,216
  • 3
  • 86
  • 85
5

My blog post on how to deploy Node-based apps on EC2: http://devblog.daniel.gs/2014/01/deploying-node-apps-on-aws-ec2-with.html

Explaining:

  • Deploying Node apps from your github repo (private+public)
  • Automating the deployment process using scripts
  • Reverse proxy using Nginx
  • and using Forever utility.

Hope this helps.

Daniel K.
  • 947
  • 3
  • 11
  • 21
1

There are quite some hosting solutions for Node.js available, here are a couple of these:

Joyent

Joyent is the corporate sponsor and trademark owner of Node.js and provides an appealing alternative to Amazon EC2 for many things, not the least Node.js hosting of course, see the Joyent's Node.js Development Environment (please check the Node.js™ Development SmartMachine Terms of Service though).

Apparently they are just restructuring this development offering though:

For the past year, Joyent Cloud has provided a free development sandbox for users of Node.js. Over time, the community has made it clear that they want more tools and more capacity. To this end, we are excited to announce a partnership with Nodejitsu to provide both of these in a world-class Node.js development environment with Nodejitsu's development and management tools running on Joyent Cloud's Infrastructure-as-a-Service platform. The new service will launch very shortly.

Accordingly, it is not entirely clear yet how the pricing options for a production hosting of a Node.js solution will end up, but given Joyent's competitive pricing, I'd expect a similar option at least.

Cloud Foundry

The Cloud Foundry Open Platform as a Service Project support Node.js as well, amongst many other frameworks (which makes the platform so exciting), The platform is getting quite some traction recently and is meanwhile used by several solution Platform as a service (PaaS) providers as their backend accordingly - amongst these are (in no particular order and not necessarily complete):

  • AppFog - Simple PaaS for Java, Node, .Net, Ruby, PHP, MySQL, Mongo, PostgreSQL, and more... Freedom to move between IaaS at will with the easiest pricing in the cloud.
  • Cloud Foundry (VMware) (corporate sponsor of Cloud Foundry) - Deploy and scale applications in seconds, without locking yourself into a single cloud.
  • Iron Foundry - Iron Foundry is an open source project that extends Cloud Foundry™ to the .NET ecosystem by providing services, installers, and developer tools.

Most of these are in beta still and the pricing isn't settled yet, but given the competition I'd expect quite some interesting options here over time.

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
0

The easiest way to run node.js for free on EC2 is IMHO on Heroku.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
0

check out this complete tutorial here. This tutorial shows how to install Node.js on EC2 and configure HTTP ports and nginx for port forwarding as well as using supervisor to run the Node.js forever as it normally stops on closing your SSH console session.

  • Please write/quote the answer here. While a link to a solution is always nice, it is valuable to have the relevant parts here. See http://stackoverflow.com/questions/how-to-answer – Florian Dec 04 '13 at 12:54
  • Suffers from the same issue as other tutorials, dies at npm install. – canadiancreed Aug 13 '14 at 14:50
0

I just went through the Heroku sign-up and application tutorial. Could not have been easier. What a delightful experience...

...right up to the point where you can't have a MongoDB instance as a free gear. The minimum cost (other than a free trial month) is $18/month per GB of storage.

Honestly, the better choice then is Openshift. It's got three free gears which is enough for a lot of beginner stuff like what I'm doing. Both Heroku and Openshift are within Amazon's space but their customer interface is different. I thought Heroku's was easier for beginners to get started but as I mentioned, there's no free lunch on the database side of things.

Michael Blankenship
  • 1,639
  • 10
  • 16
  • Update: I've just added the MongoLab:sandbox but they made me enter a credit card against my Heroku account before provisioning. This is worrisome in that I'll need to more closely watch database size so that nothing accidentally makes this grow beyond the free zone, inviting credit card charges. – Michael Blankenship May 24 '15 at 21:38