Wednesday, October 19, 2011

Install Ruby on Rails on RHEL 6 with Apache

UPDATE: If you want to install with a newer version of Ruby, try this excellent guide:

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:

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/
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
Restart apache, browse to http://hostaddress/hello and you should see your hello world text!


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

Big G Man said...

Mate fantastic guide well done.

If you guys get the same error as me

ERROR: does not appear to be a repository

Dont fret its your proxy server just change line 3 to

gem install rails passenger --http-proxy=http://proxyaddress:port

Traktion said...

Is it normal for ruby to be running taking 80% or so of a CPU?

Even if I have no virtual hosts using ruby, it still uses a lot of CPU time. So, it seems to related to the apache module itself.

Any feedback would be appreciated!

Traktion said...

To answer my own question, my woes were related to a kernel leap second issue. It seems that ruby had an acute problem with this.

It was fixed by running the following, which reset the clock:

date -s "`date`"

Zydoon said...

Thanks Buddy,

It's october 2012, now there's an RPM for passenger you can change the way of getting it:
yum -y install mod_passenger
there's even a native build of passenger

but we still need gem install rails