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.

010bf 1fym1texwgr0ccn7x8ndlbg

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

2cdf0 1iuzcpdulxpcwftzuzharwa

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

a1b37 1q9nhirkklp td6rky 9i q

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

deb70 1eqpe7qvtq5gwfjzu4paqsq

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

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

Thirdly, we use Nginx as 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:

    $ sudo systemctl restart nginx

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

    $ sudo chown -R ubuntu /var/www

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:

fb8da 1l srjbjjr00frbhvtdipsw

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


Profile picture

Experience in software development, application architecture, and deploying cloud solutions for enterprise customers. Strong hands-on skills with a Master's degree in Computer Science and business acumen with a master of business administration (MBA) in Finance. Certified in Amazon Web Services (AWS), Google Cloud Platform (GCP), Microsoft Azure, Kubernetes (CKA, CKAD, CKS, KCNA) and Scrum (PSM, PSPO) with experience in building banking products from scratch. Connect on Linkedin

© 2022, @victorleungtw