Wednesday, October 19, 2011

Install Ruby on Rails on RHEL 6 with Apache

I couldn't find any decent guides on how to install Rails 3 on RHEL 6 so here it is.  This also works on Centos 6 of course. We'll be using Apache with Phusion Passenger (mod_rails). This is an apache module that will serve up Rails applications in a similar way to how PHP is handled. 

You can skip this on Centos, but for RHEL you need to log into RHN or your Satellite server and enable the optional server repo for your machine (required for ruby-devel and rubygems).

Install the required development packages.
yum install gcc gcc-c++ make curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-devel

Install ruby, ruby-devel, and rubygems:
yum install ruby ruby-devel rubygems

Next, use gems to install Rails and Passenger. (Note this takes a while.)
gem install rails passenger

Build the Passenger apache module by running:
passenger-install-apache2-module

That will also give you some relevant instructions on how to configure Apache. Now let's create a test application.

cd /var/www/html
rails new railstest
cd railstest
Edit Gemfile and add:
gem 'therubyracer'
Run bundle install to install therubyracer javascript engine (takes a minute or two)
bundle install

Generate our hello world controller:
rails generate controller hello

Edit config/routes.rb and uncomment the next to last line
match ':controller(/:action(/:id(.:format)))'
Edit  app/views/hello/index.html.erb add some hello world text.

At this point you can test the application to make sure it works with the default rails server.  Run rails server and open http://hostaddress:3000/hello and you should see your hello world text.  Kill this and proceed with your apache configuration.

This assumes you have a default httpd install.  Change this accordingly if you already have other vhosts configured. (Note the RailsEnv option, you'll need to set this accordingly in the future)
Add the following to /etc/httpd/conf/httpd.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.9/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.9
PassengerRuby /usr/bin/ruby

<VirtualHost *:80>
DocumentRoot /var/www/html/railstest/public
<Directory /var/www/html/railstest/public>
AllowOverride All
Options -MultiViews
RailsEnv development
</Directory>
</VirtualHost>
Restart apache, browse to http://hostaddress/hello and you should see your hello world text!

1 comments:

Mr_Moustache said...

Thanks for the guide, it works on CentOS 6.
2 fix:
I had to remove one of the 2 versions of rack that where installed:
gem uninstall rack --version 1.4.0
And I had to create the db/development.sqlite3 base with the command:
rake db:create