Unix System Administration » Web Servers

ID #1019

How to install Ruby on Rails on FreeBSD?

One of the development frameworks that grows most these days is Ruby on Rails, a framework to develop agile applications on the web using the object-oriented Ruby language.

In this tutorial you'll learn how to install Ruby on Rails with MySQL on FreeBSD. You need to have your ports up-to-date. Let's start installing Ruby on Rails:

% su-
# pkg_add -r rubygem-rails

This is going to install the whole Ruby on Rails framework with all dependencies such as Ruby. Now let's install Apache:

# pkg_add -r apache2

Now let's install FastCGI. We need to compile it from ports because we want it to support Apache 2 instead of 1.3:

# cd /usr/ports/www/mod_fastcgi
# make install clean

Now we have FastCGI. We are going to install MySQL with UTF-8 support, therefore we install from ports:

# cd /usr/ports/databases/mysql51-server
# make WITH_CHARSET=utf8 install clean

In a few minutes we have MySQL installed with UTF-8 support. Let's edit Apache's configuration file to be able to use FastCGI so that pages load fast:

# ee /usr/local/etc/apache2/httpd.conf

Add the following code in the end of the configuration file:

	ServerName 127.0.0.1:80
	LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so
	AddHandler fastcgi-script .fcgi

Now let's add a virtual host (adapt as you see fit):

<VirtualHost *:80>
ServerAdmin webmaster@example.com.br
DocumentRoot /usr/local/www/data-dist/rails
ServerName localhost/rails
ErrorLog /var/log/httpd/testapp-error_log
CustomLog /var/log/httpd/testapp-access_log common
Options Indexes ExecCGI FollowSymLinks
RewriteEngine On
</VirtualHost>

Apache and FreeBSD don't work well together if you don't define a localhost host, so let's add it and also let's set Apache to start on FreeBSD bootup:

# ee /etc/rc.conf
		hostname="localhost"
		apache_enable="YES"

Restart FreeBSD and then, let's create a directory for your Rails files in your home directory:

% cd /home/username
% mkdir rails
% cd rails

This version of Ruby on Rails has a bug but we'll fix it:

% su-
# ee /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_support/clean_logger.rb

Remove all content and paste:

require 'logger'

class Logger #:nodoc:
  # Silences the logger for the duration of the block.
  def silence(temporary_level = Logger::ERROR)
    old_logger_level, self.level = level, temporary_level
    yield
  ensure
    self.level = old_logger_level
  end

  private
        if const_defined?(:Format) # Not defined in Ruby 1.8.3
                remove_const "Format"
        end
    Format = "%s\n"
    def format_message(severity, timestamp, msg, progname)
      Format % [msg]
    end
end

Now we just have to create a link (adapt as you see fit):

% cd /home/charles
% rails /home/charles/testapp
% cd /home/charles/testapp/
% ruby script/generate controller test
% su-
# cd /usr/local/www/
# rm data-dist (make sure the directory is empty)
# ln -s /home/charles/testapp/public data-dist

Now, Rails is fully working. Open http://localhost/ and you should see the welcome page of Rails.

Tags: -

Related entries:

Last update: 2007-01-11 17:01
Author: Charles A. Landemaine
Revision: 1.0

Digg it! Print this record Send to a friend Show this as PDF file
Propose a translation for Propose a translation for
Please rate this entry:

Average rating: 3.33 out of 5 (6 Votes )

completely useless 1 2 3 4 5 most valuable

You cannot comment on this entry