Deploy Koa.js Application to AWS EC2 ubuntu instance

January 07, 2017

I am developing a Koa application, which is a new web framework designed by the team behind Express. Here is a step-by-step tutorial on how to deploy the koa.js application on your Amazon Web Service (AWS) ubuntu server.

Firstly, launch the Ubuntu instance on AWS. Then you need to change the security group.

1  FyM1TEXwgr0cCN7x8ndLBg

Otherwise, if you hit the public domain in the browser, it would be stuck at “Connecting” state until timeout. And the site can’t be reached as shown as the screenshot below:

1  iUZcPDULxPCwfTzuzHarwA

By default, the launch wizard group only has type ssh.

1  q9NHIrkKlP  td6RKY  9i  Q

Click “Edit” button add HTTP port 80 and HTTPS port 443 inbound rule:

1  EqPE7qvTq5GWfJZU4PaqsQ

Secondly, ssh into your instance, install nodejs according to the official documentation:

$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -$ sudo apt-get install -y nodejs

Thirdly, we use Nginx as a reverse proxy server:

$ sudo apt-get update$ sudo apt-get install nginx

Open the configuration file and edit as below. Be careful not to miss the semicolon:

server {
listen 80 default\_server;
listen \[::\]:80 default\_server;

root /var/www/yourApp;
    location / {        proxy_pass http://localhost:3000;        proxy_http_version 1.1;        proxy_set_header Upgrade $http_upgrade;        proxy_set_header Connection 'upgrade';        proxy_set_header Host $host;        proxy_cache_bypass $http_upgrade;    }

Save the file and restart Nginx service:

$

Finally, clone your git repository to the path /var/www/yourApp, you will get permission denied, so change ownership of the folder. You may replace the ubuntu part to ‘whoami’:

$ sudo

Run your server, for example, a simple app.js:

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

// logger

app.use(function *(next){ var start = new Date; yield next; var ms = new Date - start; console.log(‘%s %s - %s’, this.method, this.url, ms); });

// response

app.use(function *(){ this.body = ‘Hello World’; });

app.listen(3000);

Start the server:

$ node app.js

Open your browser and hit your public domain:

1  l  sRjbjJR00FRBhVtDIPsw

Done. Leave a comment below if you have any questions :)


Written by Victor Leung who is a keen traveller to see every country in the world, passionate about cutting edge technologies. Follow me on Twitter