How to Setup a Koa.js Node Application on Ubuntu 20.04 with Nginx

Updated on November 21, 2023
How to Setup a Koa.js Node Application on Ubuntu 20.04 with Nginx header image

Overview

Koa.js is a modern web framework built on Node.js. It allows developers to build high-performace and scalable web applications by focusing on a concise and extendible API built on asynchronous functionality in Node.js.

Prerequisites

Install Nodejs & Yarn

Koa requires Node.js version v7.6.0 or higher.

  1. Install the latest LTS version of Nodejs.

     $ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
     $ sudo apt-get install nodejs
  2. Verify successful Nodejs installation.

     $ node -v
     v16.xx.x
  3. Install Yarn.

     $ curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
     $ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     $ sudo apt-get update && sudo apt-get install yarn
  4. Verify successful Yarn installation.

     $ yarn -v
     1.2x.xx

Setup Application

  1. Create the project root directory.

     $ cd ~
     $ mkdir koaserver && cd koaserver
  2. Initialize the Node.js project environment through yarn and answer the question prompts according to your project.

     $ yarn init
  3. Install Koa.

     $ yarn add koa
  4. Create index.js.

     $ touch index.js
  5. Add sample code to index.js.

     const Koa = require("koa");
     const app = new Koa();
    
     app.use(async (ctx) => {
             ctx.body = "Hello World";
     })
    
     app.listen(3000);

Setup PM2

PM2 will manage the Node.js application to run in the background as a system process. PM2 will automatically restart the server if it crashes, handle logs, and monitor performance.

  1. Install PM2.

     $ sudo yarn global add pm2
  2. Confirm successful installation.

     $ pm2 -V
     5.x.x
  3. Navigate to the project folder and start the Koa server through PM2.

     $ cd ~/koaserver
     $ pm2 start index.js --name koaserver
  4. Set PM2 to run on startup.

     $ eval "$(pm2 startup | tail -1)"
  5. Save PM2 configuration.

     $ pm2 save

Setup Reverse Proxy through Nginx

  1. Install Nginx.

     $ sudo apt-get install nginx
  2. Delete default Nginx server block.

     $ sudo rm /etc/nginx/sites-enabled/default 
  3. Create a new Nginx server block.

     $ sudo touch /etc/nginx/sites-enabled/koaserver.conf
  4. Add the following text to the file.

     server {
         listen 80;
         listen [::]:80;
    
         server_name _;
    
         location / {
             proxy_pass http://127.0.0.1:3000;
         }
     }
  5. Restart Nginx.

     $ sudo systemctl nginx restart
  6. Verify a working server response by navigating to your server's IP address in a browser. You should see a Hello World message.

Setup Firewall

For added security, configure a firewall to restrict access to internal service ports.

  1. Setup UFW rules.

     $ sudo ufw allow 22,80,443/tcp

Add any other ports according to your application and needs.

  1. Enable UFW.

     $ sudo ufw enable
  2. Verify UFW working.

     $ sudo ufw status
     Status: active

Conclusion

For more information, refer to the official Koa.js documentation.