DEV Community

Cover image for How to Deploy a Node.js App to DigitalOcean?
olasperu
olasperu

Posted on

How to Deploy a Node.js App to DigitalOcean?

Node.js is an open-source, server-side JavaScript runtime environment that allows you to build and run JavaScript code on the server rather than just in the browser. It was created by Ryan Dahl in 2009 and has since gained significant popularity and adoption in the web development community.

Key features and characteristics of Node.js include:

  • JavaScript: Node.js uses JavaScript, a language already familiar to front-end developers, for both server-side and client-side scripting. This enables developers to use a consistent language and share code between the server and the client.
  • NPM (Node Package Manager): Node.js comes with a powerful package manager called npm, which provides access to a vast ecosystem of reusable libraries and modules. Developers can easily integrate third-party libraries into their projects to save time and effort.
  • Cross-Platform Compatibility: Node.js is cross-platform, which means it can run on various operating systems, including Windows, macOS, and Linux.
  • Community and Ecosystem: Node.js has a large and active community of developers and contributors. This has led to the creation of numerous libraries, frameworks, tools, and resources that can help developers streamline their development process.

Node.js is often used in combination with popular web frameworks such as Express.js, which simplifies the process of building web applications and APIs. Additionally, Node.js ability to handle asynchronous operations efficiently has made it a preferred choice for building real-time applications, such as chat applications and online collaboration tools.

How to Deploy a Node.js Project to DigitalOcean?

Deploying a Node.js app to DigitalOcean involves several steps, from setting up a server to configuring your app and deploying it. Here's a high-level overview of the process:

  • Create a DigitalOcean Account:
    If you don't already have a DigitalOcean account, sign up for one.

  • Create a Droplet:
    A Droplet is a virtual private server on DigitalOcean. Create a new Droplet with your desired specifications (size, operating system, etc.). Choose a Linux distribution like Ubuntu for the operating system.

  • Access Your Droplet:
    Once your Droplet is created, you can access it using SSH. Use a terminal to connect to your Droplet's IP address using the following command:

ssh root@your_droplet_ip
Enter fullscreen mode Exit fullscreen mode
  • Update the System: Run the following commands on your Droplet to update the system packages:
apt update
apt upgrade
Enter fullscreen mode Exit fullscreen mode
  • Install Node.js and npm: Install Node.js and npm on your Droplet. You can do this by running:
apt install nodejs
apt install npm
Enter fullscreen mode Exit fullscreen mode
  • Clone Your Node.js App:
    Clone your Node.js app's code from your version control system (e.g., Git) to your Droplet using git clone.

  • Install Dependencies:
    Navigate to your app's directory and install its dependencies using:

npm install
Enter fullscreen mode Exit fullscreen mode
  • Set Up a Process Manager: You might want to use a process manager like pm2 to keep your Node.js app running in the background and automatically restart it if it crashes. Install pm2 using npm:
npm install -g pm2
Enter fullscreen mode Exit fullscreen mode
  • Start your app using pm2:
pm2 start app.js
Enter fullscreen mode Exit fullscreen mode
  • Configure a Reverse Proxy: You can set up a reverse proxy using Nginx to handle incoming HTTP requests and forward them to your Node.js app. Install Nginx:
apt install nginx
Enter fullscreen mode Exit fullscreen mode

Create a new Nginx server block configuration to route requests to your app. You'll need to update the server block's configuration to match your app's domain and port.

  • Configure Domain and DNS:
    If you have a domain name, point it to your Droplet's IP address using your domain registrar's DNS settings.

  • Restart Nginx:
    After making changes to your Nginx configuration, restart Nginx to apply the changes:

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Top comments (0)