Configure Ruby on Rails with Phusion Passenger (mod_rails)

The configuration of a new Ruby on Rails (RoR) environment has always been a bit of a struggle thanks to mongrel, mod_ruby,… but thanks to Phusion Passenger the configuration and deployment of RoR applications has become an easy job.

I will describe how to configure Ruby on Rails with Phusion Passenger on a Debian (Debian GNU/Linux 5.0.4 (lenny)) server.

1. We start by resynchronizing the package index files with their sources

# apt-get update

2. Then we check if MySQL is installed

# mysql -V
mysql  Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (x86_64) using readline 5.2

You should run the following command if MySQL isn’t installed

# apt-get install mysql-server-5.0

Please choose a secure MySQL root password when this question is asked by the MySQL installer

3. We can now start by installing ruby

# apt-get install ruby irb rdoc

These 3 packages will be configured automatically on your server so no further configuration is required. After the installation of these packages you should be able to run the ruby-command

# ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

4. We now need to install the rubygems module. Gems is the package manager for ruby which gives the users an easy way to install additional ruby modules.There are 2 methods to install gems onto your system. There is the apt-get method, and there is the manual one. We could use apt-get to install gems but this has a limitation, you won’t be able to update your ruby configuration using

# gem update --system

So we choose the manual method to install gems. Don’t copy the next link before you have checked the rubyforge.org website for the most recent version of rubygems.

# cd /usr/src
# wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
# tar -xvzf rubygems-1.3.7.tgz
# cd rubygems-1.3.7
# ruby setup.rb
RubyGems 1.3.7 installed
 
=== 1.3.7 / 2010-05-13
 
NOTE:
...
New features:
...
Bug fixes:
...
RubyGems installed the following executables:
        /usr/bin/gem1.8

Now for the ease of use we will create a link to run the binary gem1.8 as the command gem.

# ln -s /usr/bin/gem1.8 /usr/bin/gem # gem -v 1.3.7

5. We will now move on to the installation of Rails, this component can be easily installed by launching the following command

# gem install rails

It can take several minutes before you will see any output but you just have to be patient.

Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.8
Successfully installed activerecord-2.3.8
Successfully installed rack-1.1.0
Successfully installed actionpack-2.3.8
Successfully installed actionmailer-2.3.8
Successfully installed activeresource-2.3.8
Successfully installed rails-2.3.8
8 gems installed
Installing ri documentation for rake-0.8.7...
Installing ri documentation for activesupport-2.3.8...
Installing ri documentation for activerecord-2.3.8...
Installing ri documentation for rack-1.1.0...
Installing ri documentation for actionpack-2.3.8...
Installing ri documentation for actionmailer-2.3.8...
Installing ri documentation for activeresource-2.3.8...
Installing ri documentation for rails-2.3.8...
Installing RDoc documentation for rake-0.8.7...
Installing RDoc documentation for activesupport-2.3.8...
Installing RDoc documentation for activerecord-2.3.8...
Installing RDoc documentation for rack-1.1.0...
Installing RDoc documentation for actionpack-2.3.8...
Installing RDoc documentation for actionmailer-2.3.8...
Installing RDoc documentation for activeresource-2.3.8...
Installing RDoc documentation for rails-2.3.8...

We can now check if rails was installed succesfully.

# rails -v
Rails 2.3.8

6. It is now time to test by creating a rails application. My webroot is configured under /var/www so I’ll use this in my test but you should verify your webroot before following these steps.

# cd /var/www
# rails hello
# cd hello
# script/server

This will result in the following output

=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000
/usr/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)

We get this error because the ruby openssl library is required so we install this.

# apt-get install libopenssl-ruby
# script/server -d
=> Booting WEBrick
=> Rails 2.3.8 application starting on http://0.0.0.0:3000

The “-d” parameter starts rails and detaches the process so that you can continue working within your shell.

7.  We can now move on to installing Phusion Passenger (mod_rails) as the configuration of Ruby and Rails has succeeded.

# apt-get install ruby-dev
# gem install passenger
# passenger-config --version
2.2.15

We now need to configure the Passenger apache module, this can be done by the following command. You’ll probably get some errors that certain modules are missing but you can easily install them to comply with the Passenger requirements.

# passenger-install-apache2-module
The Apache 2 module was successfully installed.
 
Please edit your Apache configuration file, and add these lines:
 
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /usr/bin/ruby1.8
 
After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!
 
Press ENTER to continue.

Check if your apache config (mine is /etc/apache2/apache2.conf) has the line /etc/apache2/mods-enabled/*.load. If not you can just add this line to your apache config.

8. We will now create the apache Passenger module and activate it.

# cd /etc/apache2/mods-available/
# touch passenger.load
# vi passenger.load

enter the following lines and save the file.

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /usr/bin/ruby1.8
 
# ln -s ../mods-available/passenger.load passenger.load
# /etc/init.d/apache2 restart
# a2enmod

You can now find “passenger” in this list so just type “passenger” and press enter to activate this apache module. Now restart apache.

# /etc/init.d/apache2 restart

9. The final step is to let apache know where to find your app. Therefore you need to modify the apache configuration file of the vhost that will run your app, for example /etc/apache2/sites-enabled/000-default. Following this manual the following lines need to be configured in this config file:

DocumentRoot /var/www/hello/public
PassengerAppRoot /var/www/hello/

10. You now have a working Ruby on Rails environment with Phusion Passenger. If you want more information about Phusion Passenger there is always the user guide at http://www.modrails.com/documentation/Users%20guide%20Apache.html.

Leave a Reply

Your email address will not be published. Required fields are marked *