So you've found DigitalOcean, and realized that it makes it dead simple to host your own Ruby on Rails application through their one-click set up. The question is, now what? Here are five easy steps to get your Rails app set up on DigitalOcean.
The first thing you'll need to do is replace the initial files with those on your local machine. The easiest way to do this is through SFTP, which is already set up for you and ready to go. To find the SFTP credentials that you'll need, simply SSH into your newly created VPS (droplet). All sorts of important information will be printed when you log in through the message of the day:
The part that we're most concerned with now is right at the top, where it gives you the SFTP information for your VPS. This will allow you to SFTP right into the
/home/rails directory, which is there your Rails app lives. Once you've SFTP'ed in, go ahead and replace the starter application with your own.
Note: Remember to specify SFTP in your FTP client, not regular FTP. FTP will not work!
The next step is to get your database set up on DigitalOcean. By default, DO gives you a MySQL server that is already running, so we'll make use of it.
To connect it to your Rails app, you'll first need to install a gem to handle the connection. I've found that the mysql2 gem works best. So, in your
gemfile you should have:
source 'https://rubygems.org' .. group :production do .. gem 'mysql2' end
You'll also need to edit your
database.yml file, which can be found under
/home/rails/config. The credentials for this can also be found in the message of the day. An example file would look something like this:
development: .. test: .. production: adapter: mysql2 database: rails username: rails password: find_in_ssh_thing host: localhost
Finally, once you've uploaded the change to DO, the last thing to do is migrate your database. Remember to migrate the production database, though!
RAILS_ENV=production rake db:migrate
If you're switching from a different kind of host, like Heroku, you may forget that you have to manually precompile your assets. You're lucky, though – it's easy!
RAILS_ENV=production rake assets:precompile
If you run into problems, try running this instead:
RAILS_ENV=production rake assets:precompile:primary
After precompiling your assets, you'll need to take ownership of them. This is also retrievable through the message of the day, but to save you time:
chown -R rails:www-data /home/rails
This will allow
rails:www-data to take full ownership of the application, and everything should run smoothly.
By default, DigitalOcean sets you up with Unicorn as a Rails server. After making any changes (anything from steps 1-3), you should restart Unicorn. This can be done by simply running:
service unicorn restart
That's it! Now you have your Rails app up and running with DigitalOcean!
Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more