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:

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04

PM2 Cheat sheet: https://devhints.io/pm2 https://pm2.keymetrics.io/docs/usage/quick-start/#restart-application-on-changes

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 ]

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

6. Restart Nginx - OPTIONAL

After changes to Nginx configuraiton restart Nginx.

$ sudo systemctl restart nginx     [ restart nginx for .conf updates ]

Was this helpful?