Skip to content

Instantly share code, notes, and snippets.

@jessekanner
Created January 5, 2022 13:26
Show Gist options
  • Select an option

  • Save jessekanner/0daf2e632f6aa66a76321f15d447d0b6 to your computer and use it in GitHub Desktop.

Select an option

Save jessekanner/0daf2e632f6aa66a76321f15d447d0b6 to your computer and use it in GitHub Desktop.
How To install a Node.js application on Ubuntu 20.04

How To install a Node.js application on Ubuntu 20.04

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

nginx - Basic Install

1a. Install nginx

sudo apt update
sudo apt install nginx
sudo ufw app list
sudo ufw allow 'Nginx HTTP'
sudo ufw status
systemctl status nginx
curl -4 icanhazip.com

1b. Install SSL with Certbot

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Optional:

sudo vi /etc/nginx/sites-available/example.com
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

1c. Check auto renewal SSL

sudo systemctl status certbot.timer



node.js - Basic Install

3a. Install node.js via NVM

cd ~
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs
node -v
sudo apt install build-essential

3b. Create test app

const http = require('http');

const hostname = 'example.com';
const port = 3000;

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

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

3c. Run test app

curl http://example.com:3000



PM 2 - Basic Install

4a. Install PM2

sudo npm install pm2@latest -g
pm2 start hello.js
pm2 startup systemd

4b. The last line of the resulting output will include a command to run with superuser privileges in order to set PM2 to start on boot:

[PM2] Init System found: systemd
sammy
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy

4c. Run the command from the output, with your username in place of sammy:

sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy

4d. As an additional step, we can save the PM2 process list and corresponding environments:

pm2 save

4e. You have now created a systemd unit that runs pm2 for your user on boot. This pm2 instance, in turn, runs hello.js.

sudo systemctl start pm2-sammy

4f. Check the status of the systemd unit:

systemctl status pm2-sammy

4g. other commands

pm2 stop app_name_or_id
pm2 restart app_name_or_id
pm2 list
pm2 info app_name
pm2 monit



nginx - Configuration

5a. Edit config file

sudo nano /etc/nginx/sites-available/example.com

5b. add block

server {
...
    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;
    }
...
}

5c. add extra blocks if requried

server {
...
    location /app2 {
        proxy_pass http://localhost:3001;
        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;
    }
...
}

5d. Test & restart nginx

sudo nginx -t
sudo systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment