Novell Home

Configuring Ruby Rails for Apache on SUSE Linux Enterprise Server

From Developer Community

Contents

Configuring Ruby Rails for Apache on SUSE Linux Enterprise Server

by Gordon Mathis and Thomas Yeung

Introduction

These instructions are for a default installation of the SUSE Linux Enterprise Server, but have also been tested on SUSE 9.3 and SUSE 10. Ruby Rails requires Ruby version 1.8.2 or greater with the exception of 1.8.3 which does not work. SUSE 10 ships with Ruby 1.8.2 so you can enable Ruby via the YaST utility and skip the instructions on for Installing Ruby and start with instructions for Installing RubyGems if you are using SUSE 10.

Installing Ruby, RubyGems, and Rails

*** IMPORTANT ***
Execute YaST and from the Software category select Install and Remove Software and then search and select the following packages before doing any part of this install (You will have problems installing Ruby Gems if you don't install zlib-devel before installing Ruby).
zlib-devel
make
gcc

Download, Compile and Install Ruby

At http://www.rubyonrails.org select the Get Started icon and download and uncompress the latest source for Ruby

tar zxvf ruby-1.8.4.tar.gz

cd ruby-1.8.4
./configure --prefix=/usr
make
sudo make install

Check installation by typing ruby -v which will echo the version ruby 1.8.4 (2005-12-24) [i686-linux]

Download and Install RubyGems

From that same web page download and uncompress RubyGems rubygems-0.8.11.tgz

(06-28-2006: version 0.9.0 released -- instructions below remain the same, just different filename)

tar zxvf rubygems-0.8.11.tgz
cd ../rubygems-0.8.11
sudo ruby ./setup.rb

Install Rails

Now that RubyGems is installed, install Rails and it dependencies. This will prompt you to install the necessary dependencies. Answer 'Y' to each question.

sudo gem install rails

Creating a sample Rails application

Before you start creating applications make sure the database you plan to use is installed so that the path for the socket property in the config/database.yml is detected correctly.

Installing MySQL

Execute YaST and from the Software category select Install and Remove Software and then search for mysql. Select mysql and mysql-client packages and click Accept. While still in the YaST utility select the System category and then the Runlevel Editor. From the Runlevel Editor: Service screen scroll down the list and select mysql and then click the Enable button and then the Finish button.

Create a test application and start the WEBrick server

cd ~
rails www.rubytest.com
cd www.rubytest.com
ruby script/server

Open a web browser and browse to http://localhost:3000

At this point you should see a Welcome aboard screen telling you how to get started. Even though welcome screen has basic instructions they can still be a little vague especially for someone who is new to Ruby Rails which is why we are going to go into more detail (Everything is easy once you have done it).

Create a sample database

Create a text file named company.sql with following content:

create database company;
use company;

create table employees (
  id int(8)  not null auto_increment,
  firstName varchar(128) ,
  lastName varchar(128) ,
  hireDate date ,
  biography text ,
  primary key(id)
);

insert into employees values(0, 'Donald', 'Duck', '1934-6-9',  'Famous for The Wise Little Hen');
insert into employees values(0, 'Mickey', 'Mouse', '1928-11-18', 'Famous for Steamboat Willie');
insert into employees values(0, 'Captin', 'Hook', '1953-11-18',  'Famous for Peter Pan');

Using the file to create and populate a sample database with the following command:

mysql -u root < company.sql

Tell Rails how to find this database by modifying the file /home/developer/www.rubytest.com/config/database.yml and changing the database name to company. Leave the username as root and the password empty. Notice that this properties file supports several different databases and includes sections for development, test, and production for each database. For now we just need to change database property in the development section, for the mysql database.

development:
  adapter: mysql
  database: company
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock

Create the Controller class to handle web requests from the user with the following command:

ruby script/generate controller employee

This will create a /home/developer/www.rubytest.com/app/controllers/employee_controller.rb file. Edit this file and add the following line. This single scaffold command will bring the database to life allowing us to create, read, update, and delete (CRUD) employees in our database!

class EmployeeController < ApplicationController
	scaffold :employee
end

Use the following command to create a Employee Model class that will hold data from the employees table in the database.

ruby script/generate model employee

Set the default routing to this application instead of the welcome screen. To do this modify the /home/developer/www.rubytest.com/config/routes.rb and uncomment the following line:

	# map.connect , :cotroller => "welcome"

and replace the word welcome with employee

	map.connect , :controller => "employee"

Now rename or delete the /home/developer/www.rubytest.com/public/index.html file.

Restart the WEBrick server
Ctrl-C
ruby script/server

So just to double check we had to do the following six steps:

  1. Create and populate a database
  2. Modify the database.yml to point the database
  3. Create a Controller class and add the scaffold command
  4. Create a Model class
  5. Set the default routing
  6. Restart the WEBrick server

Open a web browser and browse to http://localhost:3000

At this point you should see the employees sample database and should be able to add, delete, and modify records.

If you get the message: "No such file or directory – /tmp/mysql.sock" you can either modify the MYSQL_UNIX_ADDR value in the /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/vendor/mysql.rb file to point to /var/lib/mysql/mysql.sock or you can create the symlink shown below. Either way restart the WEBrick server.

sudo ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Although this is a great start, the goal of this little tutorial was to use Apache in place of the WEBrick web server. So lets continue on.

Type Ctrl-C to stop the WEBrick server.

Using Ruby Rails with the Apache Web server

From YaST select the Network Services category and then HTTP Server

Click Continue at the dialog window to install the apache2 and apache2-prefork packages (this is only shown on a new installation)

Select the radio button to enable the HTTP Service.

Select Hosts from the list and click the Edit button

At the Configured Hosts screen click the Add button to create a new Virtual Host

At the New Host Information screen enter the following (substituting your username for developer).

Server Name: www.rubytest.com
Server Contents Root: /home/developer/www.rubytest.com/public/
Administrator E-Mail: developer@novell.com
Select Determine Request Service by HTTP Headers radio button
Click OK (On SUSE 10 click Next and then OK at the second screen)

At the Configured Hosts screen select the www.rubytest.com host and click the Edit button.
Note: On SUSE 10 the Directory option will already exists and just needs it options modified as shown below.
At the www.rubytest.com Configuration screen click the Add button Select Directory from the combo box and click the OK button. Enter /home/developer/www.rubytest.com/public/ for the path and then enter the following options in the list

AllowOverride all
Order allow,deny
Allow from all

Click OK at the Configured Hosts screen Click Finish at the HTTP Server Configuration Screen

*** IMPORTANT ***
By default, the Apache rewriter module is not loaded. In order to enable virtual host and URL re-direction to work, you need to turn the mod_rewrite on. In addition, you also need to load the fastcgi module.

Open the file /etc/sysconfig/apache2 and find the line that starts with APACHE_MODULES=. At the end of the string add the words rewrite and fastcgi.

Restart the apache server.

/etc/init.d/apache2 restart

Now all you need to do is edit the /etc/hosts file and Add a temporary Host Domain Name that you can use until you are ready to purchase a Domain Name and go live. (Make sure you enter the systems actual IP Address and not just localhost)

su
vi /etc/hosts

127.0.0.1		localhost
137.65.137.138	        www.rubytest.com


Open a web browser and browse to http://www.rubytest.com. You should see the same employee sample database. However this time it is being render by Apache in stead of WEBrick.

Installing FastCGI

By default, Ruby on Rails with work with CGI. However, it will be so much faster to use FastCGI instead. FastCGI uses long running process that initialize the Ruby interpreter and the Rails framework only at start-up. The database connection is established on the first query and kept for the lifetime of the process. In addition, your application code is cached in the production environment. However, you need to install additional three packages:

  1. mod_fastcgi
  2. fcgi
  3. ruby-fcgi
  4. ruby-devel


Install YaST Packages

Execute YaST and from the Software category select Install and Remove Software and then search for ruby. Select apache2-mod_fastcgi, ruby-fcgi, and ruby-devel then click Accept.


Install FastCGI

Go to http://www.fastcgi.com

Select download link for the Apache section and download the latest version of the file e.g fcgi-2.4.0.tar.gz

tar zxvf fcgi-2.4.0
cd fcgi-2.4.0
./configure --prefix=/usr
sudo make install
sudo gem install fcgi

To enable FastCGI on your Rails application, you need to modify the hidden .htaccess file in your application's public directory (/home/developer/www.rubytest.com/public). Change the following line:

RewriteRule ^(.*)$ dispatch.cgi [QSA, L]

to

RewriteRule ^(.*)$ dispatch.fcgi [QSA, L]

Restart the apache server.

/etc/init.d/apache2 restart

Open a web browser and browse to http://www.rubytest.com.

For additional tutorials visit http://www.rubyonrails.org.

Cateogry: Developer Library

Novell® Making IT Work As One

© 2009 Novell, Inc. All Rights Reserved.