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:
Bashssh root@your_droplet_ip
Replace '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:
Bashapt update apt upgrade
Install Node.js and npm using these commands:
Bashcurl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - apt install -y nodejs
Verify the installation:
Bashnode --version npm --version
Transferring 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:
Bashapt install git
Then clone your repository:
Bashgit clone your_repository_url
If your code is not in a repository, you can use SCP to transfer files:
Bashscp -r local_project_path root@your_droplet_ip:/root/app
Setting Up Your Application
Navigate to your application directory and install dependencies:
Bashcd your_application_directory npm install
Create a production configuration file if needed. Test your application locally on the Droplet to make sure everything works:
Bashnode app.js
Using 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:
Bashnpm install pm2 -g
Start your application with PM2:
Bashpm2 start app.js
Set up PM2 to start on system boot:
Bashpm2 startup systemd
Setting Up Nginx as Reverse Proxy
Install Nginx to handle incoming traffic:
Bashapt install nginx
Create a new Nginx configuration file:
Bashnano /etc/nginx/sites-available/your_app
Add this basic configuration:
Nginxserver { listen 80; server_name your_domain.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade \\\\$http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host \\\\$host; proxy_cache_bypass \\\\$http_upgrade; } }
Enable the configuration and restart Nginx:
Bashln -s /etc/nginx/sites-available/your_app /etc/nginx/sites-enabled/ nginx -t systemctl restart nginx
Final Security Steps
Set up a firewall to protect your Droplet:
Bashufw allow OpenSSH ufw allow 'Nginx Full' ufw enable
Your 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