Article posted Sat Feb 25 2023 Updated Thu Mar 02 2023

Scripts: Simplify Magento 2 Deployment using DigitalOcean Droplets

This script is simply an example of launching a droplet for Magento from any bash enabled terminal. You will also need to ensure that jq is installed on your local machine, as the script uses it to parse JSON responses from the DigitalOcean API. For macOS this can be installed using Homebrew (brew install jq).

More information on the DO Droplet Image can be fouund here

Creating a droplet

To use this script, replace your_digitalocean_api_token_here with your actual DigitalOcean API token.

After you have made the necessary changes, save the script to a file (e.g.,, make it executable (chmod +x, and then run it by executing ./ from your terminal, alternatively bash -x The script will create a new droplet on your DigitalOcean account, deploy Magento on it, and then print the droplet's IP address to the terminal.

Please only use these scripts as a reference and starting block for your own scripts. This information is provided as an example of how someone could achieve automation using Magento 2 and DigitalOcean.

# DigitalOcean API token

# Droplet name and region

# Magento image slug

# Create droplet
echo "Creating droplet..."
droplet=$(curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer ${DO_API_TOKEN}" -d '{"name":"'${DROPLET_NAME}'","region":"'${DROPLET_REGION}'","size":"s-1vcpu-1gb","image":"'${MAGENTO_IMAGE_SLUG}'","ssh_keys":null,"backups":false,"ipv6":false,"user_data":null,"private_networking":null,"volumes":null,"tags":["magento"]}' "" | jq -r '')

# Wait for droplet creation to complete
echo "Waiting for droplet creation to complete..."
while [[ "${status}" != "active" ]]; do
  sleep 5
  status=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${DO_API_TOKEN}" "${droplet}" | jq -r '.droplet.status')
  echo "Droplet status: ${status}"

# Get droplet IP address
ip=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${DO_API_TOKEN}" "${droplet}" | jq -r '.droplet.networks.v4[0].ip_address')

echo "New droplet created with IP address: ${ip}"

Installing Magento via script/command line

If you also want to install Magento through command line, add the below code. Please ensure all parameters after bin/magento setup:install have been set correctly!

# Install Magento
echo "Installing Magento..."
ssh root@${ip} "wget --no-check-certificate && tar -zxvf download --strip 1 -C /var/www/html && rm download && chown -R www-data:www-data /var/www/html && cd /var/www/html && sudo -u www-data php bin/magento setup:install --base-url=http://${ip}/ --db-host=localhost --db-name=magento --db-user=root --db-password=root --admin-firstname=admin --admin-lastname=admin --admin-user=admin --admin-password=admin123 --language=en_US --currency=USD --timezone=America/New_York --use-rewrites=1"
echo "Magento has been installed on the droplet with IP address: ${ip}"

Example of removing droplets

The following example shows how we can destroy a droplet through a desktop bash script. It takes a droplet name as an input, and checks against the DO API to ensure it exists. It will then ask for confirmation before deleting the ENTIRE droplet. NO BACK-UPS. This script requires jq for response handling.

This script example does NOT cover backing up a server or database! It simply deletes anything with a matching name. Please do not use this script as-is and only use it as a reference for your own script.

# Prompt user for Droplet names
echo "Enter the name(s) of the Droplet(s) to remove (separated by spaces):"
read -a droplet_names

# Initialize array to store Droplet IDs

# Loop through each Droplet name and send GET request to DigitalOcean API to retrieve Droplet details
for name in "${droplet_names[@]}"
    response=$(curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_ACCESS_TOKEN" "$name")
    droplet_id=$(echo "$response" | jq -r '.droplets[0].id')

    # Check if the Droplet ID was found
    if [[ -n "$droplet_id" ]]; then
        echo "Droplet $name found with ID $droplet_id."
        echo "Droplet $name not found."

# Prompt user to confirm before deleting Droplet
echo "Are you sure you want to delete the Droplet(s) with ID(s) ${droplet_ids[@]}? [y/n]"
read confirm

if [[ $confirm == "y" ]]; then
    # Loop through each Droplet ID and send DELETE request to DigitalOcean API to destroy Droplet
    for id in "${droplet_ids[@]}"
        curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_ACCESS_TOKEN" "$id"
        echo "Droplet $id has been destroyed."
    echo "Droplet removal cancelled."