write a bash script that will create an ubuntu webserver on digitalocean
- User
write a bash script that will create an ubuntu webserver on digitalocean
- Assistant
Sure! Here is a bash script that will create an Ubuntu web server on DigitalOcean:
#!/bin/bash # Set your DigitalOcean API token API_TOKEN="your_digitalocean_api_token" # Set your desired hostname, size, region, and image slug HOSTNAME="example-webserver" SIZE="s-1vcpu-1gb" REGION="nyc1" IMAGE="ubuntu-20-04-x64" # Create Droplet echo "Creating droplet..." droplet_id=$(curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $API_TOKEN" -d '{ "name":"'$HOSTNAME'", "region":"'$REGION'", "size":"'$SIZE'", "image":"'$IMAGE'", "ssh_keys":null, "backups":false, "ipv6":true, "user_data":null, "private_networking":null, "volumes": null, "tags":["webserver"] }' "https://api.digitalocean.com/v2/droplets" | jq '.droplet.id') if [ -z "$droplet_id" ]; then echo "Failed to create droplet" exit 1 fi echo "Droplet created with ID: $droplet_id" # Wait for Droplet creation to complete droplet_ip="" while [ -z "$droplet_ip" ]; do echo "Waiting for Droplet creation to complete..." sleep 5 droplet_ip=$(curl -s -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $API_TOKEN" "https://api.digitalocean.com/v2/droplets/$droplet_id" | jq '.droplet.networks.v4[0].ip_address') done echo "Droplet IP address: $droplet_ip" # SSH into Droplet and configure web server echo "Configuring web server..." ssh root@$droplet_ip 'apt update && apt install -y nginx' echo "Web server configured successfully" # Cleanup echo "Cleaning up..." unset droplet_id unset droplet_ip echo "Done"