Lädt...


🔧 How to Deploy a Node.js App to DigitalOcean Droplet or Other Linux VM


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Deploying a Node.js application to a DigitalOcean Droplet or any Linux virtual machine is important to make your project online.

This guide uses SSH, GitHub, PM2, and Nginx for efficient and reliable deployment to deploy a Node.js app to DigitalOcean.

You can use it for other VM also.

We will use PM2 here mostly because it is already pre installed and working for the default set the DigitalOcena gives.

But, there are some reasons to use PM2 as well.

PM2 is a powerful process manager for Node.js applications, designed to make your life easier when running and managing production apps. It goes beyond simply starting your application—it provides a suite of features that ensure your app runs smoothly and reliably, even in the most demanding environments.

We have a WhatsApp group for job posts using the process described in this post.

You can join it for latest jobs. You can also find and post jobs on our job board.

Why You Should Use PM2

  1. Automatic Restart

    PM2 automatically restarts your app if it crashes, ensuring zero downtime. This is a lifesaver for production environments where reliability is critical.

  2. Process Management

    It allows you to manage multiple applications on a single server effortlessly. With simple commands like pm2 start, stop, restart, and delete, you have full control over your processes.

  3. Built-In Monitoring

    PM2 provides real-time monitoring of your app's performance, showing CPU and memory usage. This helps you identify bottlenecks and optimize your app.

  4. Startup Scripts

    When you reboot your server, PM2 ensures your apps start automatically by generating a startup script tailored to your system.

  5. Log Management

    PM2 consolidates logs from all your applications into one place. You can view, tail, or clear logs with a single command, making debugging much more manageable.

  6. Cluster Mode

    Want to utilize all CPU cores on your server? PM2's cluster mode makes it easy to scale your app horizontally, maximizing performance.

  7. Ease of Use

    With a clean and intuitive CLI, PM2 is beginner-friendly while still offering advanced features for experienced developers.

  8. Cross-Platform

    PM2 works seamlessly on various operating systems, including Linux, macOS, and Windows, making it a versatile choice for any environment.

PM2 has Its robust features and simplicity make it a go-to solution for production deployments. Whether you're scaling a startup app or managing a high-traffic application, PM2 ensures stability and provides peace of mind. It empowers developers to focus on building features instead of worrying about process management, making it an invaluable tool in your arsenal.

Prerequisites

  1. Node.js Application: A ready-to-deploy Node.js app.
  2. DigitalOcean Droplet: Set up with Ubuntu or similar.
  3. SSH Access: Configured for secure login.
  4. Nginx: For reverse proxy setup.

If you don't have any Node.js app to test, you can use our example.

$git clone https://github.com/OnlyCoiner/digitalocean_deployment_examples.git
$cd node
$yarn
$yarn test
$yarn dev 

Set Up SSH and GitHub

  1. Create the Droplet

Alt text

Create an account or log into DigitalOcean and create a Node.js Droplet. You can use its console and use the commands below.

Alt text

Use its default nodesource_setup.sh script and you will have most of requirements ready including git, node, yarn or npm etc. Otherwise, you can install them manually as you did in your laptop.

./nodesource_setup.sh
  1. Transfer SSH Key

Save your SSH key ~/.ssh/your_private_key to the Droplet or your VM to download your project from GitHub or others with $git pull command.

You can make one here https://github.com/settings/keys for GitHub if you don't have it yet.

$scp ~/.ssh/your_private_key root@<your-droplet-ip>:~/.ssh/
$chmod 600 ~/.ssh/your_private_key
  1. Configure SSH Access

Edit the SSH config file on your server.

This way, you can also push your updates to a git repository if you want to.

$vim ~/.ssh/config

Edit with the text below.

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/your_private_key
  AddKeysToAgent yes
  1. Clone the Repository

Clone your app from GitHub.

git clone https://github.com/OnlyCoiners/digitalocean_deployment_examples.git

Use PM2 to Manage Your Node.js App

  1. Stop Default Apps

DigitalOcean’s default Node.js app may be running. Stop and delete it first.

$pm2 list
$pm2 stop hello
$pm2 delete hello
$pm2 save
$pm2 save --force
  1. Start Your Node.js App

Depending on your setup, use one of the following commands

$pm2 start npx --name onlycoiners_node -- tsx ./src/index.ts

You will see some logs from pm2 and other messages in your console.

OnlyCoiners server is running on port 8888
  1. Save and Set Up PM2
$pm2 list
$pm2 save
$pm2 startup

Use $pm2 list to see if your app is working and $pm2 save to save your updates.

Use $pm2 startup to make your app working after you close your SSH connection to your droplet or VM.

  1. Check Logs
$pm2 logs
$pm2 logs onlycoiners_node

Your app might not be working at first attempt. Use $pm2 logs or $pm2 logs <yourappname> to debug.

Configure Nginx as a Reverse Proxy

  1. Edit Nginx Configuration
$sudo vim /etc/nginx/sites-available/default

Edit it to use the code snippet below instead and the port number for your app.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location ^~ /assets/ {
        gzip_static on;
        expires 12h;
        add_header Cache-Control public;
    }

    location / {
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://localhost:8888;
    }
}

This way, you will be able to curl to the IP for your project directly instead of assining your port number.

  1. Restart Nginx

After the update, restart your nginx for your update to work.

$sudo systemctl restart nginx

Test Your Setup

  1. Verify Nginx and Node.js

Use curl to test whenever you make updates so you can test if it is working or not.

$curl localhost:8888  # Node.js app
$curl localhost       # Nginx reverse proxy

$curl <your-server-ip>

They should show all same results with "Create, Earn and Network with OnlyCoiners! Join us https://www.onlycoiners.com" or others depedning on your project.

Use $curl <your-server-ip> after you close SSH connectio to your droplet or VM to see if the app is still working after you close the connection.

  1. Check PM2 Status

Optionally, you can test PM2 is managing your app properly with these again.

$pm2 list
$pm2 logs

Troubleshooting Tips

  • Check application logs for errors:
$pm2 logs onlycoiners_node
  • Ensure your SSH key and Nginx configuration are correct.

Conclusion

Deploying a Node.js app to DigitalOcean or a Linux VM with PM2 and Nginx ensures reliability and scalability. By following this guide, you’ll be able to make your project work with DigitalOcean or other Linux virtual machines.

...

🔧 How to Deploy a Node.js App to DigitalOcean Droplet or Other Linux VM


📈 67.97 Punkte
🔧 Programmierung

🔧 Deploy a Medusa Server on DigitalOcean Droplet with Easypanel: A Step-by-Step Guide


📈 49.26 Punkte
🔧 Programmierung

🔧 Using Terraform to deploy a web site to a DigitalOcean droplet with Cloudflare


📈 49.26 Punkte
🔧 Programmierung

🔧 How to Host Multiple Node.js Applications on a Single DigitalOcean Droplet with PM2 and Nginx


📈 46.98 Punkte
🔧 Programmierung

🔧 Como Implantar um Aplicativo Node.js em um Droplet do DigitalOcean e outra VM


📈 46.98 Punkte
🔧 Programmierung

🔧 Cómo Implementar una Aplicación Node.js en un Droplet de DigitalOcean y otra VM


📈 46.98 Punkte
🔧 Programmierung

🔧 Safely Updating Your Deployed Next.js App on a DigitalOcean Droplet.


📈 42.77 Punkte
🔧 Programmierung

🔧 Generate a SSL with certbot on digitalocean droplet


📈 40.26 Punkte
🔧 Programmierung

🔧 New DigitalOcean Droplet Setup


📈 40.26 Punkte
🔧 Programmierung

📰 DigitalOcean App Platform: Helping developers easily build, deploy, manage, and scale apps


📈 28.8 Punkte
📰 IT Security Nachrichten

🔧 How To Deploy A Flask App On Digitalocean


📈 28.8 Punkte
🔧 Programmierung

🔧 Deploy MERN Stack in Digitalocean (2024 version)


📈 26.28 Punkte
🔧 Programmierung

🔧 How to deploy a backend application to DigitalOcean using Docker and Encore


📈 26.28 Punkte
🔧 Programmierung

🔧 How to deploy a static website to DigitalOcean


📈 26.28 Punkte
🔧 Programmierung

🎥 Simplifying AI deployments with DigitalOcean Partners: Hugging Face - Deploy 2025


📈 26.28 Punkte
🎥 Video | Youtube

🎥 Redefining Reliability: How DigitalOcean's AI-SRE is Revolutionizing Uptime - Deploy 2025


📈 26.28 Punkte
🎥 Video | Youtube

🎥 Simplifying AI deployments with DigitalOcean Partners: Aquazeel - Deploy 2025


📈 26.28 Punkte
🎥 Video | Youtube

🔧 Deploying Flask App in Digital Ocean Droplet


📈 25.49 Punkte
🔧 Programmierung

🎥 Automatic Node Repair on DigitalOcean Kubernetes


📈 24.01 Punkte
🎥 Video | Youtube

🕵️ Lepton CMS 2.2.2 Droplet Permission Manager tool.php Add_droplets SQL Injection


📈 22.97 Punkte
🕵️ Sicherheitslücken

🕵️ Lepton CMS 2.2.2 Droplet Permission Manager tool.php Add_droplets SQL Injection


📈 22.97 Punkte
🕵️ Sicherheitslücken

🐧 My Digital Ocean droplet is being brute forced. What do I do?


📈 22.97 Punkte
🐧 Linux Tipps

🐧 sed not working on my digital ocean droplet


📈 22.97 Punkte
🐧 Linux Tipps

🕵️ Mattermost Packages up to 5.16.2 Droplet exposure of resource


📈 22.97 Punkte
🕵️ Sicherheitslücken

🔧 PostgreSQL Migration From Digital Ocean DBaaS to Digital Ocean Droplet


📈 22.97 Punkte
🔧 Programmierung

🔧 How to accessible multiple services via different domain or subdomain in DO droplet by Nginx


📈 22.97 Punkte
🔧 Programmierung

🎥 Premium CPU Optimized 96 vCPU Droplet Demo


📈 22.97 Punkte
🎥 Video | Youtube

🎥 Introducing Droplet Autoscaling: Seamless Scaling for Your Workloads


📈 22.97 Punkte
🎥 Video | Youtube

🎥 Introducing Droplet Autoscaling: Seamless Scaling for Your Workloads


📈 22.97 Punkte
🎥 Video | Youtube

🔧 How to Fix No Internet Connection in Digital Ocean's Droplet


📈 22.97 Punkte
🔧 Programmierung

matomo