Setting up a Rails Server and Deploying with Capistrano on Fedora from Scratch
videos

Setting up a Rails Server and Deploying with Capistrano on Fedora from Scratch

Tutorial Details
  • Diffficulty: Intermediate - Advanced
  • Technology: Rails
  • Length: 60 Minute Video

T

his article and video tutorial will teach you how to setup a basic Fedora server for Rails and PostgreSQL deployments. First, we’ll setup Apache and PostgreSQL. Then, we’ll use phpPgAdmin to create our application’s user and databases. After that, we’ll setup the Ruby platform using Passenger to run our application. Once all the components are installed, we’ll prep our application for deployment using Capistrano.

I’ll show you how to use Capistrano to automate remote tasks and take advantage of other features.


On With The Show


Understanding the Deployment Process

There is always a lot of confusion around deploying Rails applications. This tutorial hopes to sort some of that out. Most people know LAMP: Linux, Apache, MySQL, and PHP. We will setup LAPR: Linux, Apache, PostgreSQL, and Ruby. Setting up a LAPR server is very similar to setting up a LAMP server. The only wrinkle is getting Rails to talk to Apache. Thankfully, there is Passenger aka mod\_rails. Passenger is like mod\_php. It makes running Rails applications easy as pie. In order to run a Rails application through Apache, create a virtual host pointing the document root to the applications public directory and you’ll be riding on rails.

Capistrano is another part that people may not be familiar with. Capistrano is a Ruby gem designed to execute tasks on one or more remote machines. You’ll need SSH access to use Capistrano. Capistrano, affectionately known as Cap, automates the deploy process. We can use cap to take our code from some a repo and push it to the server, stop/start/restart the server, write custom tasks required by our application (think install required gems), or disable/enable a maintenance page. Using cap is not required but it sure beats using FTP to copy all the files around! Cap’s real power comes from the ability to write custom tasks in Ruby to manipulate the server. I’ve written a lot of applications that allow user file uploads. Then on the server side, some directory needs to be created with proper permissions for the uploads to succeed. It’s easy enough to write a cap task to create the directory and set the permissions. Then, if you ever change servers, you can simply run the cap task to setup the server again. There are many things you can do with Capistrano. You could even automate this entire tutorial to set up any number of machines at once!


Tutorial Sandbox

In order to complete this tutorial, you’ll need SSH + sudo access. If you don’t have a spare server sitting around, you can create one in VirtualBox. You can easily create a new VM and network it with your host system. I did this for the tutorial. When you start your virtual machine, make sure you use a bridged adapter so your VM gets an IP on the same subnet. I started with a fresh install without any customization. If you have access to a VPS like SliceHost, you can use these instructions as well.

Be sure to view the screencast before analyzing the code below.


Creating The Deployer

    $ sudo adduser -m deployer
    $ sudo passwd deployer
    $ sudo visudo deployer ALL=(ALL) NOPASSWD: ALL
    $ su deployer
    $ mkdir ~/.ssh
    $ touch ~/.ssh/authorized_keys2
    $ chmod -R 0700 ~/.ssh
    # copy your public key and paste it into the authorized_keys2 file
    $ service sshd start

Setting Up Postgres

    $ sudo yum groupinstall "PostgreSQL Database"
    $ sudo service postgresql initdb
    $ sudo service postgresql start
    $ su - postgres
    $ psql -d template1
    $ alter user postgres with password 'yourpostgresuserpassword';
    $ \q
    # Replace ident in /var/usr/lib/pgsql/data/pg_hba.conf with md5
    $ passwd postgres
    # set extra security in /etc/phpPgAdmin/config.inc.php to false
    # add 'Allow from YOUR_IP_HERE' to vhost in /etc/httpd/conf.d/phpPgAdmin.conf
    # enable http in the firewall
    $ sudo yum install httpd
    $ sudo service httpd start
    $ sudo service postgresql restart

Configuring Ruby, RubyGems, and Passenger

    $ sudo yum groupinstall Ruby
    $ sudo yum install rubygems
    $ sudo gem install gemcutter
    $ sudo yum install postgresql-devel
    $ sudo gem install pg
    $ sudo gem install passenger
    $ yum install gcc-c++ httpd-devel apr-devel
    $ sudo passenger-install-apache2-module
    # create this file /etc/http/conf.d/passenger.conf with these contents:
      LoadModule passenger_module     /usr/lib/ruby/gems/1.8/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
      PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.9
      PassengerRuby /usr/bin/ruby

    $ sudo setenforce 0
    $ sudo service httpd restart

Creating the Deployer Folder

    $ sudo mkdir /var/www/html/apps
    $ sudo chown deployer:apache /var/www/html/apps
    $ sudo yum install git
    # at this point, create your databases in phpPgAdmin
 

Configuring Apache

    # echo "Include vhost.d/*.vhost" >> /etc/httpd/conf/httpd.conf
    $ sudo mkdir /etc/httpd/vhost.d
    $ sudo touch /etc/httpd/vhost.d/nettuts-demo.vhost
    # update that files conttents to:
      
          ServerName www.nettuts-demo.com
          DocumentRoot /var/www/html/apps/nettuts-demo/current/public
          
            Options FollowSymLinks
              Allow from all
              Options -MultiViews
          
          
          RewriteEngine On
          RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
          RewriteCond %{SCRIPT_FILENAME} !maintenance.html
          RewriteRule $ /system/maintenance.html [R=302,L]
      

Complete Cap File

    set :application, "nettuts-demo"
    set :repository,  "git://github.com/Adman65/Nettuts-Capistrano-Deployments.git"

    set :user, :deployer

    set :deploy_to, "/var/www/html/apps/#{application}"

    set :use_sudo, false

    set :scm, :git

    role :web, "192.168.1.112"                          # Your HTTP server, Apache/etc
    role :app, "192.168.1.112"                          # This may be the same as your `Web` server
    role :db,  "192.168.1.112", :primary => true # This is where Rails migrations will run
    role :db,  "192.168.1.112"

    default_run_options[:pty] = true

    namespace :deploy do
       task :start do ; end
       task :stop do ; end
       task :restart, :roles => :app, :except => { :no_release => true } do
         run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
       end

       desc "Installs required gems"
       task :gems, :roles => :app do
         run "cd #{current_path} && sudo rake gems:install RAILS_ENV=production"
       end
       after "deploy:setup", "deploy:gems"   

       before "deploy", "deploy:web:disable"
       after "deploy", "deploy:web:enable"
    end

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.edhsystems.com eric

    Haven’t even watched and can tell I will like it. I was wondering what was taking so long to post.

    • eric

      This was very nicely done. However I am nearly blinded by your choice of terminal color. Please fix this next time. Also please edit your screencast when it is this long. you might have been able to shave 5-10 minutes off of this.

  • http://craigleith-ski-clubs.blogspot.com Charon

    thanks a lot for the article…it is an eye opening..amazing info

  • aryan

    Video is not being loaded?

  • http://www.jordanwalker.net Jordan Walker

    Very interesting article, something new to learn.

  • http://swimandsing.com Jamie

    I like this kind of tut. BUT the green on black terminal is terrible for a screen cast!

    Thanks

  • Brett Millett

    Great Tut,

    Passenger really makes it easy. I come from a PHP world so learning stuff like this certainly demystifies a very large learning curve which I think has always been the biggest detriment to switching from traditional Web development methods such as PHP to newer languages and frameworks.

    I had deployed the redmine application a few months ago and had to learn this all on my own (wish this post would have been here back then.) The best nugget I got form this tut is capistrano. I can clearly see, you can use it for web application deployment for just about anything. I had always thought it was a “rails tool.”

    Thanks!

  • http://www.chinatopeast.com.cn massager

    Your article is nice.Thanks for your sharing,it helps me more.I will look forward to your more wonderfull articles.Have a good time.

  • Vik

    I was looking around at rails deployment strategies and along comes this screencast! Yet another well-timed post!

  • waveslider

    I’m using Google Chrome 5.0.342.7 beta & Firefox 3.5 on Ubuntu 9.10 with Adobe’s flashplugin installed, but I am unable to view the screencast. The button is totally unresponsive when I click it.

    I am able to view YouTube videos, listen to Pandora, and view flash animations all over teh intarwebs, no problem. Any idea what could be causing this problem? Wrong flash player version, maybe?

  • Adam Hawkins
    Author

    Hey guys, thanks for the comments. I never thought the green/black would cause a problem. I’ll keep that in mind for next time.

    Hope you guys learned something.

    • Jordan

      From a security perspective, disabling PW login on the deployer account and using keys is some protection, however having a user with PW to sudo disabled is less than ideal. Could you create a group for deployer that has the privileges necessary for Capistrano to work, so that you could leave PW for Sudo enabled?

  • http://newdailyblog.blogspot.com Tahsin Hasan

    Hello, nice post. thanks. see more on vps @ http://newdailyblog.blogspot.com/2010/06/what-is-vps.html.

  • Dickenbock

    I would never give the deployer account sudo priv – that is just asking for serious trouble. Instead you should give appropriate access priv to deployer account and make them the owner of the deployment dir. If you have multiple apps on a server, create separate deployer accounts for each.

  • http://www.makeurownrules.com Kapil

    Nice Article !!! Thx for sharing your experience. I found another good article at http://www.makeurownrules.com/ruby-on-rails/minify-compress-synch-amazons3-capistrano which is explain about uploading the minified/compressed static content on Amazon S3.

  • http://mirac.me Miraç

    Thanks a lot of. I found it.

  • http://master-degree-art.blogspot.com Thomas

    I am very impressed with this site.
    keep up the good work.

  • http://mba-degree-program.blogspot.com Jame

    Thanks for your info. very helpful.

  • Steve Jonson

    GREAT STUFF!

    The rails community needs more stuff like this. Where you go all the way not leaving anything out. It’s one thing to get rails going in development, but deploying and going production is a lot more difficult and there are few tutorials like yours.

    Thanks a lot! Really helped me with a lot of stuff even if I m on ubuntu and ruby 1.9.3 and rails 3.2.1.