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.
- 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.
- Log in using
ssh [email protected]
. Enter your password if needed. - Type
passwd
into the console to change the password of root to something you can remember. - 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. - Give that user sudo privileges by typing.
visudo
. Scroll down to privileges specification and copy the root line that saysroot 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 withssh [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. - Once in, install the newest ruby version by using the already installed Ruby Version Manager (rvm)
- Type
rvm install ruby-2.1.1
or whatever ruby version you want. Once installed, make it your default by runningrvm use 2.1.1 —default
. - Update apt-get installer by running
apt-get update
. - Update your gems by running
gem update
. You also may want to update gem in general by typeinggem update --system
. - 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
. - 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 tocd /home
. - Clone your project to the
/home
directory by typinggit clone [https address of git project]
(perhaps from Github). - 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. - 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. - Update the default unicorn config at
/home/unicorn/unicorn.conf
. Change the working directory from../rails/
to../[your-project-name]/
and save. - 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 runwhich ruby
from the command line, copy it, and add it to the PATH in this file then save. - 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. - After that completes, you can now bundle your app by running
bundle install
in your rails app directory. - 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 runbrew install mysql
thengem install mysql
thenbundle install
in order to deploy. If you don't have homebrew installed it's and easy install by just googling for it. - Migrate your db to production by running
rake db:migrate RAILS_ENV=production
. - If you have seed data, seed your db running
rake db:seed RAILS_ENV=production
. - Precompile your assets by running
RAILS_ENV=production bundle exec rake assets:precompile
. - Restart nginx by running
service nginx restart
. I'm not totally sure if you need to do this or not, but I did. - Stop the current
unicorn
processservice unicorn stop
and start a newunicorn_rails
one by browsing to your app directory and typingunicorn_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 regularunicorn
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 typeps aux | grep 'unicorn’
to find the process and thenkill [id]
, then try again. - Your site now should be up and running on your droplet IP's address - http://ip.address.goes.here/.
- Now configure the domain. Where your domain is hosted, add a subdomain and point it to the digital ocean droplet IP.
- 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. ;)