Deploy Node on Digital Ocean with PM2
Published:
Learn how to create a backend Node.js server and deploy it on Digital Ocean with a popular Node process manager called PM2.
Outline
- Introduction
- Create Node App with PM2
- Deploy Linux Server on Digital Ocean Droplet
- Install Server Dependencies and Start Server
All of this project’s code can be found in the First Look monorepo on my GitHub.
Introduction
Serverless deployment is becoming easier and easier every year, but there will always be a subset of use cases that require a persistent, running server. Projects with stricter requirements around performance, computation, storage, concurrency, and isolation may opt for a more traditional deployment strategy and host a Linux server.
In this tutorial we will deploy a Node.js application on Digital Ocean with PM2. PM2 is a production process manager for Node.js applications. It contains a built-in load balancer that allows you to keep applications alive indefinitely and it can also reload applications without downtime.
Create Node App with PM2
We will generate a minimal Node.js application. The only dependency we will install is pm2
.
If we look at our package.json
file in the root of our project we will see:
Create HTTP Server
index.js
will return a header and paragraph tag.
Start Server on Localhost
Enter the following command to start your development server and see your project.
The file is served to localhost:8080
. You should see the following message in your terminal.
Open localhost:8080 to see your application.
Configure Node App for PM2
Create a PM2 ecosystem configuration file.
Terminal output:
Open the newly created ecosystem.config.js
file.
We’ll make a few adjustments to the apps
object.
Create GitHub Repository
Create a repository on GitHub or use the gh repo create
command with the GitHub CLI.
In your Node project initialize a Git repository.
Create a new repository, set the remote name from the current directory, and push the project to the newly created repository.
Verify that your project was pushed to main.
That was the easy part. Here be servers.
Deploy Linux Server on Digital Ocean Droplet
There are many ways to host a Linux server, if you are comfortable with other providers you should be able to host this example project essentially anywhere you can host a Node server. We will create an account on Digital Ocean which provides $100 of free credits to get started.
Click “Get Started with a Droplet” to get started with a droplet.
Select Ubuntu 21.04 x64 and the Shared CPU plan.
Select the cheapest option, Regular Intel with SSD and $5 a month.
We do not need block storage. Pick the datacenter region closest to your location.
Setup SSH Keys
Click “New SSH key” to enter a new SSH key.
SSH keys provide a more secure way of logging into a virtual private server than using a password alone.
Generate an RSA Key Pair
There are several ways to use SSH; one is to use automatically generated public-private key pairs to simply encrypt a network connection, and then use password authentication to log on.
Another is to use a manually generated public-private key pair to perform the authentication, allowing users or programs to log in without having to specify a password.
Terminal output:
SSH is an authentication method used to gain access to an encrypted connection between systems with the intent of managing or operating the remote system.
SSH keys are 2048 bits by default. This is generally considered to be good enough for security, but if you think your 13 line JavaScript project might be a target for Advanced persistent threats you can include the -b
argument with the number of bits you would like such as ssh-keygen -b 4096
.
This prompt allows you to choose the location to store your RSA private key. Press ENTER to leave the default which stores them in the .ssh
hidden directory in your user’s home directory.
Create a Password
Terminal output:
Copy Key to the Clipboard
Paste the key into the SSH key content input and id_rsa.pub
for the name input.
Choose a Hostname
In a minute or so your server will be created and deployed.
Login to Server from Terminal
The username is root
and the password is whatever you used when you created your server.
Enter Password
Install Server Dependencies and Start Server
When you provision a Digital Ocean droplet or other common Linux based virtual machines, it is likely that the server does not include Node by default. Since the purpose of this tutorial is to deploy a Node application from scratch, we have chosen a fresh Linux box that needs to have Node installed. However, because of its ubiquity in web development, many hosting providers include the ability to provision a server with Node pre-installed.
Install Node
Let’s begin by installing the latest LTS release of Node.js, using the NodeSource package archives. First, install the NodeSource Personal Package Archive in order to get access to its contents. Use curl
to retrieve the installation script for Node 12.
Install Node with apt-get
and check Node version.
Terminal output:
Install Yarn with apt-get
and check Yarn version.
Terminal output:
Clone GitHub Repository and Install Node Modules
Start App as a Process with PM2
Display the application’s log with pm2 log
.
Open 144.126.219.200:8080.