AskHandle Blog
How Do I Deploy a Node.js Application on DigitalOcean Droplet?

How Do I Deploy a Node.js Application on DigitalOcean Droplet?
Setting up and deploying a Node.js application on a DigitalOcean Droplet can seem challenging at first, but with the right steps, you can get your application running smoothly. This guide walks you through the complete process of deploying your Node.js application on a DigitalOcean Droplet.
Initial Setup Requirements
Before starting the deployment process, make sure you have:
- A DigitalOcean account
- A Node.js application ready for deployment
- Basic command line knowledge
- SSH key pair for secure access
Creating Your Droplet
The first step is creating a Droplet on DigitalOcean. Choose Ubuntu as your operating system, as it works well with Node.js applications. Select a plan that matches your needs - the $5/month basic Droplet works fine for small applications. Pick a datacenter region closest to your target users for better performance.
Accessing Your Droplet
After creating your Droplet, connect to it using SSH. Use this command in your terminal:
1ssh root@your_droplet_ipReplace 'your_droplet_ip' with the actual IP address of your Droplet.
Setting Up the Server Environment
Once connected, update your system and install the required packages:
1apt update
2apt upgradeInstall Node.js and npm using these commands:
1curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
2apt install -y nodejsVerify the installation:
1node --version
2npm --versionTransferring Your Application
You can transfer your application files to the Droplet using several methods. The most common way is using Git. Install Git on your Droplet:
1apt install gitThen clone your repository:
1git clone your_repository_urlIf your code is not in a repository, you can use SCP to transfer files:
1scp -r local_project_path root@your_droplet_ip:/root/appSetting Up Your Application
Navigate to your application directory and install dependencies:
1cd your_application_directory
2npm installCreate a production configuration file if needed. Test your application locally on the Droplet to make sure everything works:
1node app.jsUsing PM2 for Process Management
PM2 is a process manager for Node.js applications. It keeps your application running and manages crashes or system reboots. Install PM2:
1npm install pm2 -gStart your application with PM2:
1pm2 start app.jsSet up PM2 to start on system boot:
1pm2 startup systemdSetting Up Nginx as Reverse Proxy
Install Nginx to handle incoming traffic:
1apt install nginxCreate a new Nginx configuration file:
1nano /etc/nginx/sites-available/your_appAdd this basic configuration:
1server {
2 listen 80;
3 server_name your_domain.com;
4
5 location / {
6 proxy_pass http://localhost:3000;
7 proxy_http_version 1.1;
8 proxy_set_header Upgrade $http_upgrade;
9 proxy_set_header Connection 'upgrade';
10 proxy_set_header Host $host;
11 proxy_cache_bypass $http_upgrade;
12 }
13}Enable the configuration and restart Nginx:
1ln -s /etc/nginx/sites-available/your_app /etc/nginx/sites-enabled/
2nginx -t
3systemctl restart nginxFinal Security Steps
Set up a firewall to protect your Droplet:
1ufw allow OpenSSH
2ufw allow 'Nginx Full'
3ufw enableYour Node.js application should now be running on your DigitalOcean Droplet. You can access it through your domain name or Droplet IP address. The application will stay running even if you log out of the server, and it will automatically restart if the server reboots or if the application crashes.
This setup provides a solid foundation for running your Node.js application in production. You can further improve it adding SSL certificates, monitoring tools