Rails Deployment

November 2, 2013

In this post i describe the deployment of a Ruby on Rails Application to a Server which uses the following technologies:

  • RVM (Ruby Version Manager)
  • Ruby
  • Rails
  • Capistrano
  • Passenger
  • Apache

if rvm is not installed on your server, take a look at: rvm.

first grab your prefered Ruby Version via rvm

make and use a gemset

install Rails version

if you dont have a user for deployment like (deployer) just create it and set a password as requested

change the user rights for your directory recursive

change the owner of the rails directory on your server

change the group of your rails directory on your server

add deployer to your sudoers file

and make sure your deployer user and group is the owner of your application root directory

now you have to allow PasswordAuthentication
in your file “/etc/ssh/sshd_config” make the following changes

restart ssh

If you don’t have a private rsa key you can generate one on the commandline of your Linux OS (Client Side)

after typing the command above you will get a request to choose the path/file where to store the key and a passphrase
press return without input to choose the default file and path (something like: root/.ssh/id_rsa)
after this process you will find two files in “~/.ssh/id_rsa” and “~/.ssh/id_rsa.pub” if you are on linux
if you work on windows you will find them in [ C:/Users/username/.ssh ]
Now you have to bring the .pub file to “~/.ssh/authorized_keys” on your server

now you have to undo password authentication
in your file “/etc/ssh/sshd_config” make the following changes

restart ssh

Now you should be able to login via ssh to your server without getting prompted for any password

If you are new to Rails have a look to create a new Rails app.
Go to your Rails application directory and follow the steps below:

The Capistrano version described is Capistrano 2.x
add Capistrano to your Rails Apps Gemfile

Make a Bundle install

create Capfile and config/deploy.rb by typing the following command

now go to your Capfile in the root directory of your app and uncomment the following lines

run the following command on your server to list your ruby version. you will need this in the next step

Write a .gitignore file with contents that you don’t want to share with other people, like passwords

add the following lines to your Gemfile

copy/create database.yml to your Rails app, on the Server, into the shared directory.

copy config/initializers/secret_token.rb from development into the Rails apps shared directory on your server

copy config/environments/production.rb into the shared directory

create .rvmrc file in shared folder (only if you are using rvm)
change into the shared folder of you Rails app on server side and run the following command

login to mysql as root

create a new database

create user

set password for the new user

set user rights for this database

Update the Rails app code and run migrations for the first time

cap deploy:

Deploy your application

Leave a Comment

You must be logged in to post a comment.