Search:  
Gentoo Wiki

HOWTO_RoR


Please improve it in any way that you see fit, and remove this notice {{Cleanup}} from the article. For tips on cleaning and formatting see Cleanup process


Apache2 Series


Main


Modules


Addons & Tunnels

  • Ruby on Rails

Tips


Configuring


Other

edit

This HOWTO demostrates to users how to install Ruby on Rails 1.1 on Gentoo, with Apache2 and SQLite, with some notes about using RoR 1.2.x.

Setting up Ruby On Rails 1.1

Since FastCGI and SQLite will be used, fastcgi and sqlite3 should be added into the use flag.

# nano /etc/make.conf
 USE="(existing use flags here) fastcgi sqlite3"

Now, rubyonrails is ready to be installed.

Code: Emerge rails
# emerge -av rails mod_fcgid swig
# gem install fcgi

If "gem install fcgi" fails simply try again, it might work this time.

After the rails is installed on the computer, it should be tested before integrating with Apache2 or SQLite.

Firstly, we should create the test application:

Code: Create test application
# cd /var/www/localhost
# rails test

and make sure the public and tmp folders are accessible by apache2 (they should be owned by apache:apache).

Code: Ensure proper permission
# cd /var/www/localhost/test
# chown -R apache:apache public tmp
# mkdir /var/www/localhost/fcgi-log
# chown -R apache:apache /var/www/localhost/fcgi-log

Since it is a good idea to log failure, we will modify the last line of dispatch.fcgi under the test/public directory so that it looks similar to this:

Code: /var/www/localhost/test/public/dispatch.fcgi
 RailsFCGIHandler.process!   '/var/www/localhost/fcgi-log/test_fcgi_crash.log'

test/public/.htaccess should also be modified to use fastcgi as well by making the following changes:

Code: /var/www/localhost/test/public/.htaccess
 - AddHandler fastcgi-script .fcgi
 + AddHandler fcgid-script .fcgi
 - RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
 + RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

The test application does not use database, so there is no need to edit test/config/database.yml.

The test application can now be tested with the built in web server, WEBrick. Make sure the following command is executed in the test folder.

Code: Testing test application
 # cd /var/www/localhost/test
 # ruby ./script/server -e test

Open the site http://localhost:3000 with your favourite web browser. You should see the welcome aboard page. Click on the "About your application’s environment" link to check that rails is indeed working. If you have troubles running it, such as encountering the "no route found to match "/rails/info/properties" with {:method=>:get}" error, try it this way:

Code: Testing test application
 # script/server

Run the following command as well

Code: Testing controller
 # script/generate controller home

and go to http://localhost:3000/home to ensure rails controller works. You should get a dispatch error.

Now that ruby on rails is working, we should set up the application to use SQLite.

Installing SQLite

Since emerge has already installed sqlite along with rails, we only need to install the SQLite gem.

Code: install sqlite3-ruby
 emerge -av sqlite3-ruby
 gem install sqlite3-ruby

Select sqlite3-ruby 1.1.0 (ruby) when prompted.

SQLite stores database into individual files. To create a database with SQLite (version 3), the following command will suffice.

Code: create database
 # sqlite3 db/test.db < /path/to/schema.sql

And modify config/database.yml so that the application will use SQLite:

Code: /var/www/localhost/test/config/database.yml
 development:
   adapter: sqlite3
   dbfile: db/dev.db
 
 test:
   adapter: sqlite3
   dbfile: db/test.db
 
 production:
   adapter: sqlite3
   dbfile: db/prod.db

If you are using older version of SQLite, put sqlite as adapter instead.

Setting up Apache2 for Rails Applications

Edit /etc/conf.d/apache2 so that fastcgi will be loaded:

 APACHE2_OPTS="-D DEFAULT_VHOST -D SSL -D SSL_DEFAULT_VHOST -D FCGID"


The rewrite modules included with Apache2 may be needed. Therefore, make sure the following is added to /etc/apache2/httpd.conf (assuming this is the location for apache's configuration file)

Code: /etc/apache2/httpd.conf
 LoadModule rewrite_module        modules/mod_rewrite.so
 #LoadModule fcgid_module          modules/mod_fcgid.so  
 <Directory /var/www/localhost/>
   AllowOverride all
 </Directory>
 
 <IfModule mod_fastcgi.c>
   FastCgiIpcDir /tmp/fcgi_ipc/
   # AddHandler fastcgi-script .fcgi
   AddHandler fcgid-script .fcgi
 </IfModule>
 
 #You may need to uncomment this line:
 Include /etc/apache2/vhosts.d/*.conf

Make sure the /tmp/fcgi_ipc directory is accessible by all:

Code: setting permission
 # mkdir /tmp/fcgi_ipc
 # chmod 777 /tmp/fcgi_ipc

As seen in /etc/apache2/modules.d/20_mod_fcgid.conf, the same operation should be done for directory /var/run/fcgidsock and file /var/run/fcgid_shm:

The following should be added into /etc/apache2/vhosts.d/00_default_vhost.conf

Code: /etc/apache2/vhosts.d/00_default_vhost.conf
 NameVirtualHost *:80
 
 <VirtualHost *:80>
   ServerAdmin webmaster@example.com
   DocumentRoot /var/www/localhost/test/public
   ServerName www.example.com
   ErrorLog /var/log/apache2/test-error_log
   CustomLog /var/log/apache2/test-access_log common
   <Directory /var/www/localhost/test/public>
       Allow from all
       AllowOverride all
       Order allow,deny
   </Directory>
   Options Indexes ExecCGI FollowSymLinks
   RewriteEngine On
 </VirtualHost>


To view your rails app go to http://www.example.com/home (make sure the proper DNS is set up). If you are greeted with an application error, you probably need to change the permissions for your rails directory:

 chown [APACHE USER] -R /var/www/localhost/test
 chmod g+w -R /var/www/localhost/test

substitute the apache user for [APACHE USER] (probably "www-data" or "apache").

Rake warnings

If script/server spews warnings that look like the following then you've got conflicting versions of modules installed. This usually happens if you run "gem update" after you've emerged rails.

 # script/server
 => Booting lighttpd (use 'script/server webrick' to force WEBrick)
 => config/lighttpd.conf not found, copying from /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/configs/lighttpd.conf
 => Rails application started on http://0.0.0.0:3000
 => Call with -d to detach
 => Ctrl-C to shutdown server (see config/lighttpd.conf for options)
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:334: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:363: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:224: warning: instance variable @ignore_new_methods not initialized
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:224: warning: instance variable @ignore_new_methods not initialized
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:41: warning: method redefined; discarding old allow_concurrency=
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/sqlserver_adapter.rb:456: warning: method redefined; discarding old remove_column
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/oracle_adapter.rb:119: warning: (...) interpreted as grouped expression
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/request.rb:169: warning: method redefined; discarding old relative_url_root
 /usr/lib/ruby/1.8/cgi/session/pstore.rb:17: warning: method redefined; discarding old []=
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/cgi_ext/raw_post_data_fix.rb:57: warning: ambiguous first argument; put parentheses or even spaces
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/cgi_ext/raw_post_data_fix.rb:8: warning: method redefined; discarding old initialize_query
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session/active_record_store.rb:129: warning: private attribute?
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session/active_record_store.rb:179: warning: method redefined; discarding old connection
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/prototype_helper.rb:640: warning: ambiguous first argument; put parentheses or even spaces
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/prototype_helper.rb:873: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/vendor/tmail/facade.rb:486: warning: method redefined; discarding old create_reply
 /usr/lib/ruby/gems/1.8/gems/actionwebservice-1.1.2/lib/action_web_service/protocol/xmlrpc_protocol.rb:6: warning: discarding old message
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:581: warning: method redefined; discarding old []=
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:590: warning: method redefined; discarding old []
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:595: warning: method redefined; discarding old keys
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:600: warning: method redefined; discarding old find_pair
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:607: warning: method redefined; discarding old []=
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:611: warning: method redefined; discarding old []
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:615: warning: method redefined; discarding old method_missing
 Exiting
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:334: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:363: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:224: warning: instance variable @ignore_new_methods not initialized
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/migration.rb:224: warning: instance variable @ignore_new_methods not initialized
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:41: warning: method redefined; discarding old allow_concurrency=
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/sqlserver_adapter.rb:456: warning: method redefined; discarding old remove_column
 /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/oracle_adapter.rb:119: warning: (...) interpreted as grouped expression
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/request.rb:169: warning: method redefined; discarding old relative_url_root
 /usr/lib/ruby/1.8/cgi/session/pstore.rb:17: warning: method redefined; discarding old []=
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/cgi_ext/raw_post_data_fix.rb:57: warning: ambiguous first argument; put parentheses or even spaces
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/cgi_ext/raw_post_data_fix.rb:8: warning: method redefined; discarding old initialize_query
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session/active_record_store.rb:129: warning: private attribute?
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session/active_record_store.rb:179: warning: method redefined; discarding old connection
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/prototype_helper.rb:640: warning: ambiguous first argument; put parentheses or even spaces
 /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/prototype_helper.rb:873: warning: `*' interpreted as argument prefix
 /usr/lib/ruby/gems/1.8/gems/actionmailer-1.2.1/lib/action_mailer/vendor/tmail/facade.rb:486: warning: method redefined; discarding old create_reply
 /usr/lib/ruby/gems/1.8/gems/actionwebservice-1.1.2/lib/action_web_service/protocol/xmlrpc_protocol.rb:6: warning: discarding old message
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:581: warning: method redefined;   discarding old []=
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:590: warning: method redefined; discarding old []
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:595: warning: method redefined; discarding old keys
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:600: warning: method redefined; discarding old find_pair
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:607: warning: method redefined; discarding old []=
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:611: warning: method redefined; discarding old []
 /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/initializer.rb:615: warning: method redefined; discarding old method_missing

I fixed this by totally unmerging rails and all its dependent packages:

 # emerge -av --unmerge actionpack actionwebservice activerecord activesupport actionmailer rake rails

and then relying on gem to manage rails:

 # gem install rails

Obviously, you can go the other way and let portage manage the packages for you. In my case, rails was at 1.1.4 in RubyGems and 1.1.2 in portage, so that won out. Just make sure you never ever run gem update if you're letting portage manage rails for you.

If gem gives you: ERROR: While executing gem ... (Gem::GemNotFoundException)

This fixed it for me:

 #rm /usr/lib/ruby/gems/1.8/source_cache
 #rm ~someuser/.gem/source_cache

And if that does not fix it, try:

 # gem update

And then repeat the 'gem install' command you were trying.

Notes on using Rails 1.2.3

Previous hackery unnecessary now on ~x86 - just edit:

Code: /etc/portage/package.keywords
 dev-ruby/rubygems ~x86
 dev-ruby/rails ~x86
 dev-ruby/rake ~x86
 dev-ruby/actionwebservice ~x86
 dev-ruby/actionpack ~x86
 dev-ruby/activesupport ~x86
 dev-ruby/activerecord ~x86
 dev-ruby/actionmailer ~x86
 

Then emerge rails.

I'm experimenting with adding 'dev-lang/ruby ~x86' to package.keywords also.

   - Thomas - http://www.railfrog.com/ - Simple Rails CMS

Using a MySql Database

Note, you might want to be root to do all of this (or you might not). Start by installing MySql (if you haven't already done so): instructions can be found in the Gentoo MySql Guide.

Navigate to the directory where you want to save your project, for example

   # cd /var/www/localhost

and create your ruby application with the following command

   # rails -d mysql my_application

where "my_application" is the name you want to give your application. You will probably want to create a database and user for your application to use

   # mysql -u root -h localhost -p
   # create database my_application
   # GRANT ALL PRIVILEGES ON my_application.* TO 'my_application'@'localhost'
   # exit

The above code will create a user and database with the same name as your application in the MySql database. IMPORTANT: The user does not have a password, so this is only suitable for local development.

Next navigate to the directory where you put your application and start the server

   # cd /var/www/localhost/my_application
   # script/server

Then open the application in your favourite web browser (something graphical is probably a good idea, for instance firefox) http://localhost:3000/. Hopefully you should see a page that says "Welcome Aboard". Underneath should be a section that says "Getting Started" and

  1. Create your databases and edit config/database.yml

Return to the console where you started the application and press "CTRL+C" to terminate it. Now open "config/database.yml" in your favourite text editor, it should look something like

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql
# On Mac OS X:
#   sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
# On Mac OS X Leopard:
#   sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
#       This sets the ARCHFLAGS environment variable to your native architecture
# On Windows:
#   gem install mysql
#       Choose the win32 build.
#       Install MySQL and put its /bin directory on your path.
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql
  encoding: utf8
  database: my_application_development
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  encoding: utf8
  database: my_application_test
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

production:
  adapter: mysql
  encoding: utf8
  database: my_application_production
  username: root
  password: 
  socket: /var/run/mysqld/mysqld.sock

Dont worry about the final two database settings, you only need the top one to get started, change the username to "my_application" and the database to "my_application", then save:

development:
  adapter: mysql
  encoding: utf8
  database: my_application
  username: my_application
  password:
  socket: /var/run/mysqld/mysqld.sock

Finally start the application again

# script/server

Goto http://localhost:3000/ and click "About your application’s environment," if it says something like:

Ruby version	1.8.6 (x86_64-linux)
RubyGems version	0.9.4
Rails version	2.0.2
Active Record version	2.0.2
Action Pack version	2.0.2
Active Resource version	2.0.2
Action Mailer version	2.0.2
Active Support version	2.0.2
Application root	/var/www/localhost/speedcoin
Environment	development
Database adapter	mysql

Then you are good to go.

Retrieved from "http://www.gentoo-wiki.info/HOWTO_RoR"

Last modified: Thu, 26 Jun 2008 12:57:00 +0000 Hits: 27,119