If you're looking to deploy an Express.js application on DigitalOcean, you've come to the right place. Below is a step-by-step guide to help you get started.

Prerequisites

Before you begin, make sure you have the following:

  • A DigitalOcean account
  • An SSH client installed on your local machine
  • Node.js and npm installed on your local machine

Step 1: Create a Droplet

  1. Go to the DigitalOcean dashboard.
  2. Click on "Create" and select "Droplets".
  3. Choose an appropriate plan for your needs.
  4. Select the datacenter region closest to your users.
  5. Choose an image. For Express.js, we recommend using the Ubuntu 20.04 image.
  6. Set a hostname for your droplet.
  7. Choose a private network or add a new one.
  8. Click "Create".

Step 2: Connect to Your Droplet

  1. Once your droplet is created, you will see its IP address.
  2. Open your SSH client and connect to your droplet using the IP address and your DigitalOcean account credentials.
ssh username@<IP_ADDRESS>

Step 3: Install Node.js and npm

To install Node.js and npm on your droplet, run the following commands:

sudo apt update
sudo apt install -y curl gnupg2 software-properties-common
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs

Step 4: Clone Your Project

  1. Clone your Express.js project from your local machine to your droplet:
git clone <REPO_URL> /path/to/your/project
cd /path/to/your/project
  1. Install your project dependencies:
npm install

Step 5: Deploy Your Application

  1. Start your Express.js application:
npm start
  1. Your application should now be running on your droplet.

Step 6: Set Up a Reverse Proxy

To ensure your application is accessible over the internet, you'll need to set up a reverse proxy. We recommend using Nginx for this purpose.

  1. Install Nginx:
sudo apt install -y nginx
  1. Create a new Nginx configuration file for your application:
sudo nano /etc/nginx/sites-available/<YOUR_APP_NAME>
  1. Add the following configuration to the file:
server {
    listen 80;
    server_name <YOUR_APP_NAME>;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  1. Enable the new configuration:
sudo ln -s /etc/nginx/sites-available/<YOUR_APP_NAME> /etc/nginx/sites-enabled/
  1. Test the Nginx configuration:
sudo nginx -t
  1. Restart Nginx:
sudo systemctl restart nginx

Step 7: Set Up a Database (Optional)

If your application requires a database, you can set up one on your droplet. We recommend using MongoDB for Express.js applications.

  1. Install MongoDB:
sudo apt install -y mongodb-org
  1. Start the MongoDB service:
sudo systemctl start mongod
  1. Configure MongoDB to start on boot:
sudo systemctl enable mongod
  1. Connect to MongoDB and create a new database:
mongo
use <DATABASE_NAME>
  1. Add your database connection string to your application's environment variables or configuration file.

Step 8: Set Up a Backup (Optional)

To ensure your data is safe, you can set up a backup for your droplet.

  1. Go to the DigitalOcean dashboard.
  2. Click on "Backups" in the sidebar.
  3. Click "Create Backup" and follow the instructions.

Conclusion

Congratulations! You've successfully deployed your Express.js application on DigitalOcean. If you need further assistance, check out our Community Forum or contact support.