Install Ruby on Rails on Linux Debian Etch

Here is a second article about installing Ruby on Rails stack on a Linux Debian Etch. In our case, the host is our production server.

This article does not include Apache 2 , mySQL installation. Our server had been prepared with the LAMP stack.

This article includes Ruby, RoR, additional gems (Capistrano 2, openwferu-scheduler), Apache 2 / FastCGI configuration, mySQL securization, msmtp + GMail configuration to notify our application’s users.

We chose to use Apache 2/FastCGI because it’s simple to configure and we do not plan an intensive use of our application (we don’t care scaling for the moment).

Install Ruby:
As root or with sudo command.
$ aptitude install ruby libzlib-ruby rdoc irb

Install RubyGems 1.1.1
$ cd /tmp
$ wget http://rubyforge.org/frs/download.php/35283/rubygems-1.1.1.tgz
$ tar xzvf rubygems-1.1.1.tgz
$ cd rubygems-1.1.1
$ ruby setup.rb

By default the binary is named : /usr/bin/gem1.8
Create the lunk ‘gem’
$ cd /usr/bin
$ ln -s gem1.8 gem

Install MySQL C Ruby Bindings
$ aptitude install libmysqlclient15-dev
$ gem install mysql — –with-mysql-config=/usr/bin/mysql_config

Install rails 1.2.5
$ gem install rails -v 1.2.5 –include-dependencies

Upgrade to 2.0.2 :

$ gem update rails

Install Mongrel

$ apt-get install ruby1.8-dev
$ gem install mongrel
$ apt-get install dpatch fakeroot debhelper libtool dpkg-dev autoconf
automake m4 bison flex gcc make subversion cvs
$ gem install mongrel

Install Capistrano 2

$ gem install fastthread
$ gem install capistrano

Install openwferu-scheduler

$ gem install openwferu-scheduler

Configure Apache

Install fastcgi
$ wget http://www.fastcgi.com/dist/fcgi.tar.gz
$ tar xzvf fcgi-2.4.0.tar.gz
$ cd fcgi-2.4.0/
$ ./configure
$ make
$ make install
$ cd ..
$ rm fcgi-2.4.0 -drf

Install Apache2 mod_fcgid
$ apt-get install libapache2-mod-fcgid
$ /etc/init.d/apache2 force-reload

Verify the config for mod_fcgid :
Edit /etc/apache2/mods-enabled/fcgid.conf

<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
SocketPath /var/lib/apache2/fcgid/sock
DefaultInitEnv RAILS_ENV production
IdleTimeout 600
ProcessLifeTime 3600
MaxProcessCount 8
DefaultMinClassProcessCount 3
DefaultMaxClassProcessCount 3
IPCConnectTimeout 8
IPCCommTimeout 48
</IfModule>

Edit /etc/apache2/mods-enabled/fcgid.load

LoadModule fcgid_module /usr/lib/apache2/modules/mod_fcgid.so

Enable mod_rewrite

$ a2enmod rewrite
$ /etc/init.d/apache2 force-reload

Configure Apache2 :

Edit sites-enables/000-default

<VirtualHost *>
ServerAdmin administrator@yourcompany.org

DocumentRoot /var/www/<your app>/current/public/
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On

<Directory /var/www/<your app>/current/public/>
Options ExecCGI FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Edit <your app>/public/.htaccess (in your working copy then commit)

AddHandler fcgid-script .fcgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 “<h2>Application error</h2>Rails application failed to start properly”

Configure GMail :

Install msmtp :
$ apt-get install msmtp

Configure Certificates
$ mkdir /etc/.certs
$ chmod 775 /etc/.certs
$ cd /tmp; wget https://www.verisign.com/support/thawte-roots.zip
$ cd Thawte \Server\ Roots/
$ cp ThawtePremiumServerCA_b64.txt /etc/.certs/ThawtePremiumServerCA.crt

Configure /etc/.msmtprc (as root, then change ownership to your application running process ..can be www-data)

$ touch /etc/.msmtprc; chown www-data /etc/.msmtprc
$ nano /etc/.msmtprc

account gmail
host smtp.gmail.com
port 587
auth on
user <your account>@gmail.com
password <your GMail password>
tls_starttls on
tls on
tls_trust_file /etc/.certs/ThawtePremiumServerCA.crt
from <your account>@gmail.com

Configure /path/to/your/app/config/environment.rb (in your working copy then commit)

ActionMailer::Base.raise_delivery_errors = true

ActionMailer::Base.delivery_method = :msmtp

module ActionMailer
class Base
def perform_delivery_msmtp(mail)
thread = Thread.new do
IO.popen(”/usr/bin/msmtp -t -C /etc/.msmtprc -a gmail –”, “w”) do |sm|
sm.puts(mail.encoded.gsub(/\r/, ”))
sm.flush
end
end
thread.run
end
end
end

MySQL configuration :

We suppose our RoR application has already been created, then imported into a Subversion Repository. You already know how to connect to a production database (you already should haveĀ  done this setup locally). Now replicate this production database configuration on the production server:

$ mysql -uroot mysql
mysql> create database <app name>_production;
mysql> Query OK, 1 row affected (0.06 sec)

mysql> GRANT ALL PRIVILEGES ON <app name>_production.* TO ‘<user name>’@'localhost’ IDENTIFIED BY ‘<newPassword>’ WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.11 sec)
mysql> exit;

$ mysql -u<user name> -<NewPassword> <app name>_production

mysql> create table Toto(id int, test text);
Query OK, 0 rows affected (0.05 sec)
mysql> drop table Toto;
Query OK, 0 rows affected (0.03 sec)

mysql> exit;

Securizing mySQL
$ mysql -uroot -p
mysql> SET PASSWORD FOR root@localhost=PASSWORD(’<password>’);
mysql> exit
$ mysql -uroot -p<password>

In a further article i will explain the goal of our application, give some tricks we used, and explain how to configure Capistrano 2 for remote deployment from the developer’s desktop.

Tags: , , , , , , , , , ,

One Response to “Install Ruby on Rails on Linux Debian Etch”

  1. John Mettraux Says:

    Hello Laurent,

    thanks for this detailed walkthrough. I made sure to bookmark it.

    Just a piece of information : the gem openwferu-scheduler has been renamed rufus-scheduler : http://jmettraux.wordpress.com/2008/03/17/rufus-scheduler-105/

    The code written for the “openwferu-scheduler” will work “as is” with the new “rufus scheduler”.

    Cheers,

    John

Leave a Reply