╔═══════════════════════════════════════════════════════════════╗
║ ║
║ ☁️ Deploy to DigitalOcean ☁️ ║
║ ║
║ ┌─────────────────────────────────┐ ║
║ │ $ ssh root@your-droplet-ip │ ║
║ │ $ nvm install node │ ║
║ │ $ git clone your-repo │ ║
║ │ $ npm install │ ║
║ │ $ pm2 start app.js │ ║
║ └─────────────────────────────────┘ ║
║ ║
║ Get your app running in the cloud! 🚀 ║
║ ║
╚═══════════════════════════════════════════════════════════════╝
This guide will walk you through deploying your Node.js application to DigitalOcean. For a detailed video walkthrough, check out this tutorial: DigitalOcean Deployment Tutorial
ssh root@your-droplet-ip from your terminalOnce you’re in the console, install Node Version Manager (nvm) and Node.js:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install node
nvm use node
Verify the installation:
node --version
npm --version
Move to /var/www directory - this is the conventional location for web applications on Linux servers:
cd /var/www
Clone your project repository:
git clone https://github.com/your-username/your-repo.git
cd your-repo
npm install
Start your application:
npm run start
Test that it’s working by visiting http://your-droplet-ip:PORT in your browser.
Set your environment variables, including the PORT. See the Environment Variables Guide for details.
You can set them temporarily:
export PORT=3000
export DATABASE_URL=your-database-url
export API_KEY=your-api-key
Or add them to ~/.bashrc for persistence:
echo 'export PORT=3000' >> ~/.bashrc
echo 'export DATABASE_URL=your-database-url' >> ~/.bashrc
source ~/.bashrc
Or use a .env file with the dotenv package in your project.
Important: Stop your current process (Ctrl+C) and follow the video tutorial to set up PM2. This will keep your app running even after you log out.
Install PM2:
npm install -g pm2
Start your app with PM2:
pm2 start app.js --name my-app
PM2 commands you’ll need:
pm2 list - View running processespm2 stop my-app - Stop your apppm2 restart my-app - Restart your apppm2 logs my-app - View logspm2 startup - Set PM2 to start on system bootpm2 save - Save current process listWatch the video tutorial for detailed PM2 setup instructions!
Once your app is running, you can add a custom domain: