Nesula
1.0.0
1.0.0
  • Introduction
  • Overview
    • First steps
    • Architecture
    • Authentication
    • Authorization
    • Routing
    • Meta
    • Mail
    • Security
      • Content Security Policy
    • Configuration
    • Service Worker
    • Logger
  • Development
    • Angular
    • NestJS
  • Nesula Devops
    • Nesula Setup
      • Files: Git
      • Database: Postgres
      • Server: Nginx
      • .Env & environment.ts
      • SSL
      • Build & Serve
    • Deployment
      • Deploy: Development
      • Deploy: Staging
      • Deploy: Production
    • Installation [ Old ]
  • Developer Setup
    • Visual Studio Code
      • Extensions
      • Run & Debug: Launch.json
      • Remote SSH
    • Chrome
    • Postman
  • Server Setup
    • Ubuntu Server Setup
      • Settings
      • Security
      • Users
    • App Server Setup
    • Node.js Hello World
    • Command Line
Powered by GitBook
On this page
  • 1. Pre-Steps
  • 2. Node Hello World
  • 3. Nginx Config
  • 4. Run with PM2
  • 5. Configure SSL ?
  • 6. Restart Nginx - OPTIONAL

Was this helpful?

  1. Server Setup

Node.js Hello World

All the steps required to deploy a simple node app on a server.

Note: we will be creating an app for hello.ifactory.co and assume DNS entries for *.ifactory.co resolve to the server.

1. Pre-Steps

  • Have a Node Server prebuilt - see the other setup docs.

2. Node Hello World

Lets create a hello world Node app

$ mkdir /var/www/hello.ifactory.co/
$ touch /var/www/hello.ifactory.co/hello.js
$ nano /var/www/hello.ifactory.co/hello.js

Then paste into nano > hello.js the following code:

const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World from NodeJS!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

3. Nginx Config

Enter the following

$ sudo touch /etc/nginx/sites-available/hello.ifactory.co.conf    [ Create Nginx config file for hello.ifactory.co ]
$ sudo ln -s /etc/nginx/sites-available/hello.ifactory.co.conf /etc/nginx/sites-enabled/hello.ifactory.co.conf [ Create symlink to new site ]
$ sudo nano /etc/nginx/sites-available/hello.ifactory.co.conf     [ open file in nano editor ]
$ sudo systemctl restart nginx                                    [ restart nginx for .conf updates ]

Paste in the following config:

server {
    listen  80;
    listen [::]:80;
    server_name hello.linode.ifactory.co;

    root /var/www/hello.linode.ifactory.co;
    index index.html;

    location / {
        proxy_pass http://localhost:3000; #whatever port your app runs on
        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;
    }
}

4. Run with PM2

$ pm2 start /var/www/hello.ifactory.co/hello.js   [ Start node app wtih pm2 ]
$ pm2 startup                                     [ Generate an active startup script to restart current processes ]

Related Commands

$ pm2 list                               [ list all pm2 processes ]
$ pm2 stop <id, app_name or all>         [ stop app ]
$ pm2 reload <id, app_name or all>       [ reload ie  show code changes ]
$ pm2 restart <id, app_name or all>      [ restart app ]
$ pm2 delete <id, app_name or all>       [ delete app from PM2 ]

PM2 Setup and auto start on Reboot:

5. Configure SSL ?

First check the requirements are in place:

  1. DNS A Records point to server e.g. hello.ifactory.co and www.hello.ifactory.co

  2. Check if Firewall allows SSL port 22. If not add.

    $ ufw status
    $ sudo ufw allow 'Nginx Full'         [ allow HHTP & HTTPS ]
    $ sudo ufw delete allow 'Nginx HTTP'  [ remove HTTPS ]
  3. etc/nginx/sites-available/hello.ifactory.co.conf file exists and the server name is set to domains:

    server_name hello.ifactory.co www.hello.ifactory.co;
  4. Confirm Nginx Configuration is correct:

    $ sudo nginx -t
  5. Install Certbot

    $ sudo apt install certbot python3-certbot-nginx
  6. Obtain SSL Cetificate

    $ sudo certbot --nginx -d hello.ifactory.co -d www.hello.ifactory.co
  7. Verifying Certbot Auto-Renewal

    $ sudo systemctl status certbot.timer     [ checks the auto-renewal engine is running ]
  8. Test the Certbot renewal process

    $ sudo certbot renew --dry-run            [ mocks the renewal process ]

6. Restart Nginx - OPTIONAL

After changes to Nginx configuraiton restart Nginx.

$ sudo systemctl restart nginx     [ restart nginx for .conf updates ]
PreviousApp Server SetupNextCommand Line

Was this helpful?

PM2 Cheat sheet:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04
https://devhints.io/pm2
https://pm2.keymetrics.io/docs/usage/quick-start/#restart-application-on-changes
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04