August 23, 2013

Beginner

How To Launch Your Ruby on Rails App with the DigitalOcean One Click Image

Introduction


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.

Upload your Files


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:

ssh screenshot

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!

Setup your Database


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

Precompile your Assets


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

Own It!


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.

Restart the Web Server


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!

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

26 Comments

Write Tutorial
  • Gravatar aupstd 8 months

    thank you very much Au www.cloudwalks.com

  • Gravatar flyingearl 8 months

    Hi, Thank you for this guide. I've hit a brick wall though now. It's probably something simple but it's driving me mad now :s. When I try to open my app in the browser I get the following error: Operation not permitted - /home/rails/tmp/cache/assets/C26/690/sprockets%2F93708cb34051f3000884905e8669d74a I'm hoping you may be able to help. Thank you in advance.

  • Gravatar flyingearl 8 months

    Not quite sure but it's now working. I did a few commands but not sure which one worked.

  • Gravatar abachuk 8 months

    after I uploaded my app, I can access it only on port 3000, doesn't work on port 80 I see nginx 504 error "504 Gateway Time-out"

  • Gravatar Kamal Nasser 8 months

    @abachuk: Is unicorn running? What's the output of this command?

    sudo netstat -plutn
    Please pastebin your nginx config files as well.

  • Gravatar abachuk 8 months

    nginx.config http://pastebin.com/9jNXzKCF sudo netstat -plutn http://pastebin.com/8mURd7i0

  • Gravatar Kamal Nasser 8 months

    Seems like unicorn is timing out. Check unicorn's logs and nginx's error logs:

    tail /home/unicorn/log/unicorn.log
    nginx:
    tail /var/log/nginx/error.log

  • Gravatar abachuk 8 months

    if it's timing out - what do I do ? How can I fix it? http://pastebin.com/h19e1V77

  • Gravatar Kamal Nasser 8 months

    @abachuk: Did you install the gems? Run

    bundle install
    in your app's directory.

  • Gravatar abachuk 8 months

    I did. Same problem.

  • Gravatar Kamal Nasser 8 months

    @abachuk: Try to get more output from the error log and see which gems are missing and then install them using

    gem install gem-name
    tail -40 /home/unicorn/log/unicorn.log
    This process is outlined here as well.

  • Gravatar abachuk 8 months

    I followed that tutorial again. I still see ERROR -- : Could not find i18n-0.6.5 in any of the sources (Bundler::GemNotFound) and for some reason I see ruby 1.9.3 in log "/usr/local/rvm/rubies/ruby-1.9.3-p429/lib/ruby/gems/1.9.1/gems/bundler-1.3.5/lib/bundler/" when I do ruby -v I get 2.0.0-p247 'wich ruby' /usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby Should I open ticket or what do I do to make it work ?

  • Gravatar Kamal Nasser 8 months

    @abachuk:

    ERROR -- : Could not find i18n-0.6.5 in any of the sources (Bundler::GemNotFound)
    The
    i18n
    gem is missing. Install it:
    gem install i18n
    and restart unicorn:
    sudo service unicorn restart

  • Gravatar abachuk 8 months

    I did. Still the same.

  • Gravatar Kamal Nasser 8 months

    @abachuk: Does it still output the same error? What's the output of this command now?

    tail -40 /home/unicorn/log/unicorn.log

  • Gravatar abachuk 8 months

    that's my unicorn.log http://pastebin.com/5BHzqCA9

  • Gravatar abachuk 8 months

    am I missing any gems or is it a server problem ?

  • Gravatar abachuk 8 months

    someone on stackoverflow pointed that I may need to add socket file http://stackoverflow.com/questions/18706902/rails-4-nginx-and-unicorn-serves-only-public-files-html would that help?

  • Gravatar b.rq 4 months

    I completed the above steps but looks like unicorn is reading my development env instead of the production env This is my database.yml -> http://pastebin.com/qwESSFaf I am getting this issue: PG::ConnectionBad could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

  • Gravatar b.rq 4 months

    This guide isn't complete. You need to change the rails env from development to production in the unicorn configuration file: Modifying Rails Environment: By default, rails operates in a production environment, which can be changed by modifying /etc/default/unicorn and replacing -E environment on UNICORN_OPTS line. For example, to change to production environment: UNICORN_OPTS="-D -c $CONFIG_RB -E production" You should restart Unicorn after making configuration changes: service unicorn restart BTW I recommend use this one instead of this: https://www.digitalocean.com/community/articles/how-to-1-click-install-ruby-on-rails-on-ubuntu-12-10-with-digitalocean

  • Gravatar dananourie 2 months

    I've put my rails app in /home/rails/ProjectName But any time I tried to issue a rails or rake command, I get this error: rake aborted! database configuration does not specify adapter Ngnix has stopped working as well and unicorn is throwing errors. I'm temped to scrap the whole droplet and start over!

  • Gravatar Kamal Nasser 2 months

    @dananourie: Try prepending RAILS_ENV=production to your command. E.g.: RAILS_ENV=production rails c

  • Gravatar mell.christopher about 1 month

    My css isn't working after fresh install and upload. Using Twitter bootstrap for the css

  • Gravatar 1germes about 1 month

    I have the same problem, css and js files dont found

  • Gravatar Mesh Developer about 1 month

    Im haveing an impossible time trying to install the mysql2 gem. This: $ gem install mysql2 -v '0.3.15' Returns this: Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. /Users/Chase/.rvm/rubies/ruby-2.0.0-p451/bin/ruby extconf.rb checking for ruby/thread.h... yes checking for rb_thread_call_without_gvl() in ruby/thread.h... yes checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes checking for rb_intern3()... yes ----- Cannot find mysql_config at /usr/local/bin/mysql_config ----- *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/Users/Chase/.rvm/rubies/ruby-2.0.0-p451/bin/ruby --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/ --with-mysql-config --without-mysql-config extconf failed, exit code 1 Gem files will remain installed in /Users/Chase/.rvm/gems/ruby-2.0.0-p451@digitalocean_rails_4/gems/mysql2-0.3.15 for inspection. Results logged to /Users/Chase/.rvm/gems/ruby-2.0.0-p451@digitalocean_rails_4/extensions/x86_64-darwin-12/2.0.0/mysql2-0.3.15/gem_make.out ___ Im at a loss. Thank you in advance for any help that can be provided. Chase

  • Gravatar Mesh Developer about 1 month

    Sorry about the cluster I posted above. Not only can I not get the mysql2 gem installed properly, but I aparently can't use this comment field either.

Leave a Comment

Create an account or login:
Ajax-loader