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
- Go to the DigitalOcean dashboard.
- Click on "Create" and select "Droplets".
- Choose an appropriate plan for your needs.
- Select the datacenter region closest to your users.
- Choose an image. For Express.js, we recommend using the Ubuntu 20.04 image.
- Set a hostname for your droplet.
- Choose a private network or add a new one.
- Click "Create".
Step 2: Connect to Your Droplet
- Once your droplet is created, you will see its IP address.
- 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
- 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
- Install your project dependencies:
npm install
Step 5: Deploy Your Application
- Start your Express.js application:
npm start
- 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.
- Install Nginx:
sudo apt install -y nginx
- Create a new Nginx configuration file for your application:
sudo nano /etc/nginx/sites-available/<YOUR_APP_NAME>
- 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;
}
}
- Enable the new configuration:
sudo ln -s /etc/nginx/sites-available/<YOUR_APP_NAME> /etc/nginx/sites-enabled/
- Test the Nginx configuration:
sudo nginx -t
- 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.
- Install MongoDB:
sudo apt install -y mongodb-org
- Start the MongoDB service:
sudo systemctl start mongod
- Configure MongoDB to start on boot:
sudo systemctl enable mongod
- Connect to MongoDB and create a new database:
mongo
use <DATABASE_NAME>
- 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.
- Go to the DigitalOcean dashboard.
- Click on "Backups" in the sidebar.
- 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.