Deploying an App on DigitalOcean and Vagrant

Writing software is awesome, but it’s only part of the picture. We have to deploy that software to make it accessible to the world. Until now, I’d always used Heroku — it took all of 30 seconds to deploy my applications, but I felt like I was missing parts of the picture. So today I deployed an app manually using Vagrant and Digital Ocean. I can now say with confidence that I love writing software more than I like dealing with infrastructure, but that infrastructure is interesting too, and that Heroku is an incredibly powerful tool. Huge thanks go out to my husband for this one — he actually does operations for a living, and was able to make it accessible to me.

The Tools

Digital Ocean provides virtual servers for software developers. It’s about $10/month, depending on what you’re using it for. (If you sign up through this link, you’ll get $10 credit on DigitalOcean. Full disclosure — I’ll get $25)

Setup

  • Get an API key from Digital Ocean
  • brew cask install vagrant
  • vagrant plugin install vagrant-digitalocean — documentation for that here

Configuration

In the root directory of my rails app, created a file called Vagrantfile. It looked like this:

Vagrant.configure('2') do |config| 
config.vm.hostname = 'wandermap'
config.vm.provider :digital_ocean do |provider, override|
override.ssh.private_key_path = '~/.ssh/id_rsa'
override.vm.box = 'digital_ocean'
override.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
provider.token = Secret.digital_ocean_key
provider.image = 'ubuntu-14-04-x64'
provider.region = 'sfo1'
provider.size = '1gb'
end

config.vm.provision :shell, path: 'install.sh'
end
#!/bin/bash sudo apt-get install -y software-properties-common build-essential libpq-dev postgresql git nodejs imagemagick 
sudo apt-add-repository ppa:brightbox/ruby-ng
sudo apt-get update
sudo apt-get install -y ruby2.2 ruby2.2-dev
gem install bundler bundle install --gemfile=/vagrant/Gemfile
sudo -u postgres psql -1 -c "CREATE USER db_service WITH PASSWORD '(put password here)';"
sudo -u postgres psql -1 -c "ALTER USER db_service WITH SUPERUSER;"
rake --rakefile=/vagrant/Rakefile db:create db:migrate
cd /vagrant
ip="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)"
rails s -p 80 -d --binding=$ip

Installations

A lot of this are things you’ve likely done on your local machine and forgotten about, but since you’re basically setting up a new server, it requires a lot of installations that your app is dependent on.

  • Build Essential: This contains the C and C++ compilers that Rails Requires
  • libpq-dev: PostgreSQL requires this
  • Postgres: what I’m using for my database
  • Git: I had some gems in my gemfile downloaded directly from Git
  • Node: A dependency for one of my gems.
  • Imagemagick: A dependency for using Paperclip/AWS to upload images

App Configuration

bundle — once again, it’s a Rails app. I did have to specify the path to the Gemfile, since Vagrant doesn’t automatically run from within the app

Commands

The first time you’re creating your app, run vagrant up. This will spin up your server, Digital Ocean Droplet, put your code onto the server, and run all of the commands in your install.sh script.

Domains

I have a domain hosted on DreamHost, and was able to add an A name pointing to the IP address that DigitalOcean provided. Your process may be different depending on your domain setup. If you destroy and re-create your Vagrant Box, your IP address will change, so you’ll need to update this as well.

Senior Software Engineer | www.adriennedomingus.com

Senior Software Engineer | www.adriennedomingus.com