Skip to content

Instantly share code, notes, and snippets.

@jcave
Last active August 29, 2015 14:00
Show Gist options
  • Save jcave/11332940 to your computer and use it in GitHub Desktop.
Save jcave/11332940 to your computer and use it in GitHub Desktop.
Getting Set Up on Digital Ocean 1-click rails - Unicorn / Nginx / Mysql

Rails App on Digital Ocean / Ngnix / Unicorn

So I'm somewhat of a newb when it comes to setting up servers to deploy my Rails apps on. I love building the apps, but have been spoiled with Heroku's simple git push setup. This has been great, but I'm at the point now that I need something a bit better then their 'Hobby' setup and I don't want to pay for the next step up. I also looked at this as a good learning experience since I also know very little about the linux world. I chose DigitalOcean.com to play with because they have a $5 a month plan that you pay hourly by; about $0.007 an hour. You can create/destroy as many virtual servers as you want so it's great to play on. Here's what I did to get started. I'm sure there's plenty of things here that may not be 'optimal' to folks that are much better at it then me, but in case you're new like me, I'm hoping these directions may help you a bit; or at least get you started.

I started by reading this file, along with several others from Digital Ocean and Google searches, so it might be a good overview before getting started.

  1. Sign up for an account at digitalocean.com. Create a droplet using the pre-configed Rails/ngix/unicorn option and using an ssh key if you set one up first. [An overview can be found here.] (https://www.digitalocean.com/community/articles/how-to-set-up-ssh-keys--2) If you don’t set up an SSH key, that's okay, DigitialOcean (DO) will send you one to your email.
  2. Log in using ssh root@ip.address.of.server. Enter your password if needed.
  3. Type passwd into the console to change the password of root to something you can remember.
  4. If you want add a new user so you don’t have to use root then type adduser [username] in the console. Feel free to fill out the FullName part and just skip the rest.
  5. Give that user sudo privileges by typing. visudo. Scroll down to privileges specification and copy the root line that says root ALL=(ALL:ALL) ALL but change root to the new username you added. Once complete, save the file and try to ssh in to verify it worked correctly with ssh [username]@ip.address.of.server. It's recommended for security purposes to change the port of ssh and limit who can log in, but that's something you can Google if you want.
  6. Once in, install the newest ruby version by using the already installed Ruby Version Manager (rvm)
  7. Type rvm install ruby-2.1.1 or whatever ruby version you want. Once installed, make it your default by running rvm use 2.1.1 —default.
  8. Update apt-get installer by running apt-get update.
  9. Update your gems by running gem update. You also may want to update gem in general by typeing gem update --system.
  10. You can use the built in SFTP that DigitalOcean provides, but I prefer git. In order use git, you need to install it. Type apt-get install git.
  11. Digital Ocean sets you up with a default rails program/root path at /home/rails. We will need to clone our project into the /home location and then update the paths in our nginx and unicorn configs. To do this, browse to cd /home.
  12. Clone your project to the /home directory by typing git clone [https address of git project] (perhaps from Github).
  13. Now it's time to update a few config files. For reference, the main nginx config file is at /etc/nginx/nginx.conf. We won't have to touch this one. If you look inside it, you'll see it pulls in another config which we will need to touch below.
  14. Update the default site nginx config at /etc/nginx/sites-enabled/default and change the root path from ../rails/ to ../[your-project-name]/ then save.
  15. Update the default unicorn config at /home/unicorn/unicorn.conf. Change the working directory from ../rails/ to ../[your-project-name]/ and save.
  16. Update the other unicorn config at /etc/default/unicorn. Change the app_path from ../rails/ to ../your-project-name/ and if you updated the ruby version using rvm above, you'll need to add it to the PATH variable. To get this run which ruby from the command line, copy it, and add it to the PATH in this file then save.
  17. Move to the directory of your rails app and run gem install rails bundler unicorn rake i18n to install some of the needed basic gems to go with your new version of ruby you installed with rvm.
  18. After that completes, you can now bundle your app by running bundle install in your rails app directory.
  19. If you are using mysql, you may need to install it on your local machine first in order to bundle and get your Gemfile.lock in order. You can't just run gem install mysql or else you get errors. To get it to work run brew install mysql then gem install mysql then bundle install in order to deploy. If you don't have homebrew installed it's and easy install by just googling for it.
  20. Migrate your db to production by running rake db:migrate RAILS_ENV=production.
  21. If you have seed data, seed your db running rake db:seed RAILS_ENV=production.
  22. Precompile your assets by running RAILS_ENV=production bundle exec rake assets:precompile.
  23. Restart nginx by running service nginx restart. I'm not totally sure if you need to do this or not, but I did.
  24. Stop the current unicorn process service unicorn stop and start a new unicorn_rails one by browsing to your app directory and typing unicorn_rails -E production -D. The -D daemonizes it so it doesn't die when you log out. If it's not working you can remove the -D and you can see the output to help debug. I don't totally understand why regular unicorn didn't work, but for some reason it kept looking for my old version of ruby. This was the only way i could get it working. If you have an instance of unicorn or unicorn_rails running already you may get an error. To kill it type ps aux | grep 'unicorn’ to find the process and then kill [id], then try again.
  25. Your site now should be up and running on your droplet IP's address - http://ip.address.goes.here/.
  26. Now configure the domain. Where your domain is hosted, add a subdomain and point it to the digital ocean droplet IP.
  27. In Digital Ocean admin enter a new subdomain or domain with the droplet IP and select the droplet you want it to go to. This could take some time to resolve, so check using ping [subdomain]. I thought I screwed something up, but it turned out it just took a little while to populate.

And there you go. Notes from a total newb who hacked away at it until something came up. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment