Article

Table of Contents
Theme:
Was this article helpful?

1  out of  4 found this helpful

Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles!

Deploy an Express Web Server with Pm2 and Nginx on Ubuntu 20.04

Author: Quantum

Last Updated: Tue, Feb 2, 2021
Node.js Ubuntu Web Servers

Introduction

This guide explains how to create a Node.js app with Express, deploy it with the pm2 process manager, and publish it with an Nginx reverse proxy.

Prerequisites

This guide requires:

  • A Vultr cloud server running Ubuntu 20.04
  • Nginx
  • Node.js LTS version
  • The npm Package Manager
  • pm2 process manager
  • The nano text editor

This guide assumes that no web sites exist under Nginx. This tutorial will remove the default Nginx configuration in step 6.

1. Install the Dependencies

  1. SSH to the server, or connect with the Vultr web console.
  2. Update all your dependencies.

    $ sudo apt update
    
  3. Install Node.js

    $ sudo curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
    $ sudo apt install -y nodejs nano nginx
    
  4. Confirm Node.js was installed properly.

    $ node -v
    

    This command should say something like v14.x.x

  5. Install pm2.

    $ sudo npm i -g pm2
    

2. Project Setup

  1. Initialize a new project.

    $ mkdir express-website
    $ cd express-website
    $ npm init -y
    
  2. Install Express.js

    $ npm i express
    

3. Add Demo Source Code

  1. Create your project's main file.

    $ nano index.js
    
  2. Paste the following into your editor.

    const express = require("express"); // Acquire the express package and assign it to a variable called "express"
    const app = express(); // Calls the method "express()" and assigns it's output to "app". "express()" will create an express app for you.
    
    app.get("/", (req, res) => { // Creates sort of a listener for when there are "GET" requests to the "/" (root) path. Takes in req (request) and res (response)
        res.send("Hello world!"); // For the response, send a string "Hello World!"
    });
    
    app.listen(3000, () => { // Tells the app to start on port 3000. This function below is run when
        console.log("Server listening on port 3000!"); // Say in the console "Server listening on port 3000!"
    })
    
  3. Save and exit the file.

4. Test the App

Run your app:

$ node index.js

If it works, it reports "Server listening on port 3000!". Type CTRL + C to exit.

5. Daemonize the App

Daemonize the app with pm2.

$ pm2 start index.js

To verify it has daemonized, run pm2 list.

6. Nginx Reverse Proxy

  1. Create your server block in Nginx.

    $ nano /etc/nginx/sites-enabled/express.conf
    
  2. Paste the following in your nano editor:

    server { listen 80; # Listen on port 80 listen [::]:80; # Listen on port 80 for ipv6

    server_name _;
    
    location / {
        proxy_pass http://127.0.0.1:3000;
    }
    

    }

  3. Save and exit the file.

  4. Remove the default Nginix site.

    $ sudo rm /etc/nginx/sites-enabled/default
    
  5. Restart nginx

    $ systemctl restart nginx
    
  6. Verify it works by entering your server's IP in your browser.

Conclusion

If everything is in working order, you have made an Express app, daemonized with pm2 and reverse proxied it with Nginx. Now, you should have acquired an understanding of:

  • Basic usage of apt package manager
  • Understanding and usage Node.js and npm
  • Basic usage of the pm2 package manager
  • Nginx and reverse proxying with nginx
  • Troubleshooting problems

References

Want to contribute?

You could earn up to $600 by adding new articles