1. Code
  2. Ruby
  3. Ruby on Rails

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

Scroll to top
60+ min read

This 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

Please accept marketing cookies to load this content.

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

1
2
    $ sudo adduser -m deployer
3
    $ sudo passwd deployer
4
    $ sudo visudo deployer ALL=(ALL) NOPASSWD: ALL
5
    $ su deployer
6
    $ mkdir ~/.ssh

7
    $ touch ~/.ssh/authorized_keys2
8
    $ chmod -R 0700 ~/.ssh

9
    # copy your public key and paste it into the authorized_keys2 file

10
    $ service sshd start

Setting Up Postgres

1
2
    $ sudo yum groupinstall "PostgreSQL Database"
3
    $ sudo service postgresql initdb
4
    $ sudo service postgresql start
5
    $ su - postgres
6
    $ psql -d template1
7
    $ alter user postgres with password 'yourpostgresuserpassword';
8
    $ \q
9
    # Replace ident in /var/usr/lib/pgsql/data/pg_hba.conf with md5

10
    $ passwd postgres
11
    # set extra security in /etc/phpPgAdmin/config.inc.php to false

12
    # add 'Allow from YOUR_IP_HERE' to vhost in /etc/httpd/conf.d/phpPgAdmin.conf

13
    # enable http in the firewall

14
    $ sudo yum install httpd
15
    $ sudo service httpd start
16
    $ sudo service postgresql restart

Configuring Ruby, RubyGems, and Passenger

1
2
    $ sudo yum groupinstall Ruby
3
    $ sudo yum install rubygems
4
    $ sudo gem install gemcutter
5
    $ sudo yum install postgresql-devel
6
    $ sudo gem install pg
7
    $ sudo gem install passenger
8
    $ yum install gcc-c++ httpd-devel apr-devel
9
    $ sudo passenger-install-apache2-module
10
    # create this file /etc/http/conf.d/passenger.conf with these contents:

11
      LoadModule passenger_module     /usr/lib/ruby/gems/1.8/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
12
      PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.9
13
      PassengerRuby /usr/bin/ruby
14
15
    $ sudo setenforce 0
16
    $ sudo service httpd restart

Creating the Deployer Folder

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

Configuring Apache

1
2
    # echo "Include vhost.d/*.vhost" >> /etc/httpd/conf/httpd.conf

3
    $ sudo mkdir /etc/httpd/vhost.d
4
    $ sudo touch /etc/httpd/vhost.d/nettuts-demo.vhost
5
    # update that files conttents to:

6
      <VirtualHost *:80>
7
          ServerName www.nettuts-demo.com
8
          DocumentRoot /var/www/html/apps/nettuts-demo/current/public
9
          <Directory /var/www/html/apps/nettuts-demo/current/public>
10
            Options FollowSymLinks
11
              Allow from all
12
              Options -MultiViews
13
          </Directory>

14
          

15
          RewriteEngine On

16
          RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
17
          RewriteCond %{SCRIPT_FILENAME} !maintenance.html
18
          RewriteRule $ /system/maintenance.html [R=302,L]
19
      </VirtualHost>

Complete Cap File

1
2
    set :application, "nettuts-demo"
3
    set :repository,  "git://github.com/Adman65/Nettuts-Capistrano-Deployments.git"
4
5
    set :user, :deployer
6
7
    set :deploy_to, "/var/www/html/apps/#{application}"
8
9
    set :use_sudo, false
10
11
    set :scm, :git
12
13
    role :web, "192.168.1.112"                          # Your HTTP server, Apache/etc

14
    role :app, "192.168.1.112"                          # This may be the same as your `Web` server

15
    role :db,  "192.168.1.112", :primary => true # This is where Rails migrations will run

16
    role :db,  "192.168.1.112"
17
18
    default_run_options[:pty] = true
19
20
    namespace :deploy do
21
       task :start do ; end
22
       task :stop do ; end
23
       task :restart, :roles => :app, :except => { :no_release => true } do
24
         run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
25
       end
26
27
       desc "Installs required gems"
28
       task :gems, :roles => :app do
29
         run "cd #{current_path} && sudo rake gems:install RAILS_ENV=production"
30
       end
31
       after "deploy:setup", "deploy:gems"   
32
33
       before "deploy", "deploy:web:disable"
34
       after "deploy", "deploy:web:enable"
35
    end