Deploying a Node.js application to a DigitalOcean cloud server involves a series of steps, including setting up the server, configuring it, and deploying your Node.js application. Here's a general guide:
-
Create a DigitalOcean Account:
- If you don't have a DigitalOcean account, sign up for one at DigitalOcean.
-
Create a Droplet (Virtual Machine):
- Log in to your DigitalOcean account.
- Click on the "Create" button and select "Droplets."
- Choose your preferred operating system (Ubuntu is commonly used).
- Select a plan based on your requirements.
- Choose a data center region.
- Add any additional options you need.
- Click "Create Droplet."
-
Access Your Droplet:
- Once your Droplet is created, you'll receive an email with login details or use SSH to connect:
ssh root@your-droplet-ip
-
Update the System:
- After logging in, update the system packages:
sudo apt update sudo apt upgrade
-
Install Node.js:
- You can install Node.js using a package manager. For example, using NodeSource:
sudo apt install curl curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt install -y nodejs
-
Install Nginx (Optional, for Reverse Proxy):
- If you want to use Nginx as a reverse proxy:
sudo apt install nginx
-
Configure Nginx (Optional):
- Create an Nginx server block:
sudo nano /etc/nginx/sites-available/your-app
Example Nginx configuration:
server { listen 80; server_name your-domain-or-ip; location / { proxy_pass http://localhost:your-nodejs-port; 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:
sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled sudo nginx -t sudo systemctl restart nginx
-
Deploy Your Node.js App:
- Copy your Node.js application files to the server, either manually or using version control.
-
Install Application Dependencies:
- Navigate to your application folder and install dependencies:
cd /path/to/your/app npm install
-
Start Your Node.js App:
- Start your Node.js application. You may want to use a process manager like PM2:
npm install -g pm2 pm2 start your-app.js
-
Configure Firewall (Optional):
- If you have a firewall enabled, configure it to allow traffic on the necessary ports. For example, for Nginx:
sudo ufw allow 'Nginx Full'
-
Configure Domain (Optional):
- If you have a domain, configure your domain's DNS settings to point to your server's IP address.
-
Secure Your Server:
- Follow best practices for securing your server, including disabling the root login, setting up SSH keys, and configuring a firewall.
-
Set Up SSL (Optional):
- Consider securing your application with SSL. You can use Let's Encrypt for free SSL certificates:
sudo apt install certbot sudo certbot --nginx -d your-domain
Follow these steps, and you should have your Node.js application deployed on a DigitalOcean cloud server. Adjust the commands and configurations based on your specific application and requirements.