Setting up Ruby and Rails
Installing required packagesFirst you’ll have to get the required ruby packages:
apt-get install libruby libruby1.8 ruby ruby1.8 libzlib-ruby1.8 libyaml-ruby1.8 rdoc1.8 libiconv-ruby1.8 irb1.8 libreadline-ruby1.8 libcurses-ruby1.8 libbigdecimal-ruby1.8 libdrb-ruby1.8 liberb-ruby1.8 Now that everything is in place, get
RubyGems. Either
compile it from source or install Debian packages by adding
deb http://www.sgtpepper.net/hyspro/deb ./
deb-src http://www.sgtpepper.net/hyspro/deb ./
to your
/etc/apt/sources.list and running
apt-get install libgems-ruby libgems-ruby1.8
Now you should have met all requirements to install
rails with
gem. To do this, run:
gem install rails
Installation script will prompt you about installing dependencies (
rake,
activerecord,
actionpack,
actionmailer) – press Y to all questions.Creating a new Rails application Go to the directory of your choice and run this command:
rails ;
Chances are, if you did everything as written above, it will print a lot of commands and exit without errors, leaving you with the following folder hierarchy:
/path/to/your_application
app/
config/
db/
doc/
lib/
log/
public/
script/
test/
vendor/
CHANGELOG
README
Rakefile
Congratulations, you’ve just created a framework for your first Ruby On Rails app! You can now start a WEBrick servlet by running
cd your_application
ruby script/server
which will fire up a webserver on your port 3000 (open your browser and point to 127.0.0.1:3000) or proceed to set up Apache for running your Rails application. Modifying Apache config file Debian has a pretty usable Apache configuration by default setup so it won’t be covered in this article but supposing you already have some page running on your webserver, I’ll show you how to setup a VirtualHost for Rails application we have recently created. We begin by openning Apache’s config file (that is
/etc/apache/httpd.conf) in your favorite editor (mind you that this file is owned by root so you should either start the editor in root console or by using
sudo). Find the VirtualHost section (the last one) and add these lines right there:
ServerName some_name
DocumentRoot /path/to/your_application/public/
ErrorLog /path/to/your_application/log/apache.logOptions ExecCGI FollowSymLinks
AddHandler cgi-script .cgi
AllowOverride all
Order allow,deny
Allow from all
Now we have to change the permissions of app’s folder to give Apache full access to its contents. Apache process is running as user
www-data of group
www-data so open a root console and run
chown -R www-data:www-data /path/to/your_application
And now we can make Apache reload its config file and see the results by running (as root):
/etc/init.d/apache reload
But first we have to make sure that our VirtualHost is resolved by your browser. If your server is running locally, the easiest way is to open (once again as root)
/etc/hosts in your editor and add
some_name (replacing it with the name you set in the VirtualHost config above) to the end of line starting with
127.0.0.1 to have something like this:
127.0.0.1 localhost some_name
That’s it, now you can open your browser and type:
http://some_name Apache FastCGI setup for Ruby Installing required packages Now you have to install FastCGI module for your version of Apache and Ruby’s
FCGI bindings. For Apache 1.3.x:
apt-get install libapache-mod-fastcgi
apt-get install libfcgi-ruby1.8
For Apache 2.0.x:
apt-get install libapache2-mod-fastcgi
apt-get install libfcgi-ruby1.8
Modifying Apache config file Open /etc/apache/httpd.conf in your favorite editor and add these lines:
FastCgiIpcDir /tmp/fcgi_ipc/
Open .htaccess file in your application’s
public/ folder and change the line
RewriteBase /dispatch.cgi
to
RewriteBase /dispatch.fcgi
to let Apache and Rails know that you want to use FastCGI from now on.