Deploying a Node.js Application on DigitalOcean
Welcome to this comprehensive guide on deploying a Node.js application on DigitalOcean! By the end of this tutorial, you'll be able to deploy your very own Node.js project to a live server.
Let's get started! 🚀
Table of Contents
- Why DigitalOcean?
- Prerequisites
- Setting Up a Droplet
- Configuring SSH Keys
- Installing Node.js and Git
- Transferring Your Project
- Starting Your Application
- Setting Up a Domain (Optional)
- Quiz 🎯
1. Why DigitalOcean?
DigitalOcean is a popular cloud platform that offers developers a simple, reliable, and scalable infrastructure. It's a great choice for deploying Node.js applications due to its ease of use, affordable pricing, and robust functionality.
2. Prerequisites
Before we dive in, make sure you have the following:
- A Node.js project that is version controlled with Git
- A DigitalOcean account (sign up here)
3. Setting Up a Droplet
A Droplet is a virtual private server (VPS) in DigitalOcean. To create one, follow these steps:
- Log in to your DigitalOcean account.
- Click on the green "Create" button on the top right corner.
- Choose "Droplets" and select the desired region.
- Choose an image that includes Node.js, such as Ubuntu 20.04 with Node.js 14.x.
- Select a size based on your project's requirements.
- Give your Droplet a unique hostname and choose an SSH key for secure access.
- Click on "Create" to deploy your Droplet.
4. Configuring SSH Keys
To connect to your Droplet securely, you'll need to generate an SSH key pair on your local machine. Follow these steps:
- Open Terminal on your local machine.
- Run the following command to generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Follow the prompts to save your new SSH key in the default location (
~/.ssh/id_rsa) and provide a passphrase if desired.
5. Installing Node.js and Git
Once your Droplet is ready, let's SSH into it and install Node.js and Git:
- Copy your SSH key to your Droplet with the following command:
cat ~/.ssh/id_rsa.pub | ssh root@your_droplet_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
- Now, SSH into your Droplet with the following command:
ssh root@your_droplet_ip
- Provide your SSH key's passphrase when prompted.
- Once logged in, update the package lists for upgrades:
sudo apt-get update
- Install Node.js with the following command:
sudo apt-get install nodejs
- Install Git with the following command:
sudo apt-get install git
6. Transferring Your Project
Now, let's clone your project from GitHub and install its dependencies:
- Clone your project with the following command:
git clone https://github.com/your_username/your_project.git
- Change to your project directory:
cd your_project
- Install the project's dependencies:
npm install
7. Starting Your Application
Start your Node.js application with the following command:
node app.js
Replace app.js with the name of your application's main file if it differs.
8. Setting Up a Domain (Optional)
To connect a custom domain to your DigitalOcean Droplet, follow these steps:
- Register a domain (e.g., GoDaddy) or purchase a domain from DigitalOcean.
- Log in to your DigitalOcean account and go to Networking > Domains.
- Click on "Add Domain" and enter your domain name.
- Choose a Droplet and create the DNS record.
- Go to the DNS provider's control panel (e.g., GoDaddy) and set up the A records pointing to your Droplet's IP address.
- Wait for the DNS changes to propagate (up to 48 hours).
Once your domain is set up, update your application's configuration to use the new domain name.
Quiz 🎯
That's it! You've successfully deployed a Node.js application on DigitalOcean. Happy coding! 🎉