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
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., create-droplet.sh), make it executable
(chmod +x create-droplet.sh
), and then run it by executing ./create-droplet.sh
from your terminal, alternatively bash -x create-droplet.sh
. 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.
#!/bin/bash
# DigitalOcean API token
DO_API_TOKEN="your_digitalocean_api_token_here"
# Droplet name and region
DROPLET_NAME="magento-2-droplet"
DROPLET_REGION="nyc3"
# Magento image slug
MAGENTO_IMAGE_SLUG="magento-2-ubuntu-20-04"
# 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"]}' "https://api.digitalocean.com/v2/droplets" | jq -r '.droplet.id')
# Wait for droplet creation to complete
echo "Waiting for droplet creation to complete..."
status=""
while [[ "${status}" != "active" ]]; do
sleep 5
status=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${DO_API_TOKEN}" "https://api.digitalocean.com/v2/droplets/${droplet}" | jq -r '.droplet.status')
echo "Droplet status: ${status}"
done
# Get droplet IP address
ip=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer ${DO_API_TOKEN}" "https://api.digitalocean.com/v2/droplets/${droplet}" | jq -r '.droplet.networks.v4[0].ip_address')
echo "New droplet created with IP address: ${ip}"
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 https://magento.com/tech-resources/download --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-email=admin@example.com --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}"
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.
#!/bin/bash
# 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
droplet_ids=()
# Loop through each Droplet name and send GET request to DigitalOcean API to retrieve Droplet details
for name in "${droplet_names[@]}"
do
response=$(curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_ACCESS_TOKEN" "https://api.digitalocean.com/v2/droplets?tag_name=$name")
droplet_id=$(echo "$response" | jq -r '.droplets[0].id')
# Check if the Droplet ID was found
if [[ -n "$droplet_id" ]]; then
droplet_ids+=($droplet_id)
echo "Droplet $name found with ID $droplet_id."
else
echo "Droplet $name not found."
fi
done
# 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[@]}"
do
curl -X DELETE -H "Content-Type: application/json" -H "Authorization: Bearer $DIGITALOCEAN_ACCESS_TOKEN" "https://api.digitalocean.com/v2/droplets/$id"
echo "Droplet $id has been destroyed."
done
else
echo "Droplet removal cancelled."
fi