Ruby on Rails, The Full Monty on a Debian box
Published on the 2nd of March 2009Hosting Rails apps on a Debian based Apache webserver isn't easy. Fortunately Passenger has made it a lot more gentle. This article explains how to install Ruby, Rails and Passenger on a Debian Etch machine running Apache 2.
Important is to define our "start of point". I'm not going to talk you through the installation process of Debian, MySQL or Apache. There are loads of tutorials out there that explain how to install those.
Now I mention it... if you don't know how to do this.. the following might be to complicated :-/
Back on topic. This is pretty much the software present before we start installing Ruby on Rails:
- Debian Etch (with all latest stable packages)
- Apache 2
- MySQL 5
- Subversion client
All packages were installed using apt-get (the stable releases).
Beside the required software, I've added a user called 'rubyrocks'. The Rails application has been placed inside the public_html of the user's home folder: /home/rubyrocks/public_html
Now we've determined our start of point, go log into your Debian machine and 'su' as root. Run the command:
apt-get install ruby rubygems
This installs both the Ruby runtime and the Ruby gems installer. Gems are Ruby's own 'apt packages'. There is a lot of discussion wheter you should use them or not. For now, I found it's the only 'stable' way to install Rails on my machine.
Next. In order to install passenger we require the following packages:
apt-get install build-essential apache2-prefork-dev
After those we can install the necessary Gems.
gem install rails gem install rake gem install passenger
If all goes well we've now successfully installed Ruby on Rails. In order to get Ruby hosted by Apache we've installed the Passenger gem.
To actually install/ enable passenger we need to run the following command: passenger-install-apache2-module
On my installation the passenger-install-apache2-module wasn't available.
This is due the fact ruby, rake and other commands are installed inside a gems directory, and not the /usr/bin.
I did the following to fix it:
# The first line will update the current $PATH export PATH=/var/lib/gems/1.8/bin/:$PATH # Create symlinks to the rake and rails command ln -s /var/lib/gems/1.8/bin/rake /usr/bin/rake ln -s /var/lib/gems/1.8/bin/rails /usr/bin/rails passenger-install-apache2-module
Follow all the steps in the installer, and if necessary, fix any errors.
If all went well, Passenger is now installed. Time to create our first Rails app hosted by Apache!
The following is purely an example, but it should give you a good idea.
Start by creating a new site:
nano /etc/apache2/sites-available/rubyrocks
Put the following in the file:
<VirtualHost 192.168.1.2:80 ServerAlias *.rubyrocks.test <a ="http://www.rubyrocks.test">www.rubyrocks.test</a> DocumentRoot /home/rubyrocks/public_html/public DirectoryIndex index.html index.rb RailsBaseURI / </VirtualHost>
As you can see in the above example, I'm hosting my app inside the public_html folder of the user rubyrocks.
Apart from that, change the IP to your own..
Now we still have to enable the ruby runtime for apache. We'll do this by creating a .load file inside the available mods folder.
nano /etc/apache2/mods-available/ruby.load
And add the following
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.so PassengerRoot /var/lib/gems/1.8/gems/passenger-2.0.6 PassengerRuby /usr/bin/ruby1.8
Save the file, and execute the following commands
a2ensite rubyrocks a2enmod ruby /etc/init.d/apache2 reload
Crossing our fingers here.... hopefully you won't get any errors :-)
If all went well you can now check your site at http://rubyrocks.test (if you get a 404, make sure your /etc/hosts file has the right line).
