Installing Ruby on Rails and Configuring Mongrel on Ubuntu

Installing Ruby on Rails and Configuring Mongrel on Ubuntu
Back to our developlment machine this time we will take a look at how to install Ruby on Rail with Mongrel Cluster on the Ubuntu Box. We decided to go with Mongrel the obvious choice for Ruby appliations plugged with Apache.

I assume you do not have Ruby or Mongrel installed while going through this guide.

Some of the Ruby sources are not available by default being third party. Edit /etc/apt/sources.list so they can be fetched:

#sudo pico /etc/apt/sources.list


Add the following (or uncomment if already present):

deb http://us.archive.ubuntu.com/ubuntu feisty universe
deb-src http://us.archive.ubuntu.com/ubuntu feisty universe


Run a system update, upgrade and fetch necessary build tools:

#sudo apt-get -y update
#sudo apt-get -y dist-upgrade
#sudo apt-get -y install build-essential


Install Ruby and various Ruby Tools/Libraries:

#sudo apt-get -y install ruby ri rdoc libmysql-ruby ruby1.8-dev irb1.8 libdbd-mysql-perl libdbi-perl libmysql-ruby1.8 libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 rdoc1.8 ri1.8 ruby1.8


Fetch and install RubyGems:

#wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
#tar zxvf rubygems-0.9.4.tgz
#cd rubygems-0.9.4
#sudo ruby setup.rb


Install the Rails GEM:

#sudo gem install rails -y


Install FastCGI:

#sudo apt-get -y install libpcre3 libfcgi-dev libfcgi-ruby1.8 libfcgi0c2


Install Mongrel:

#sudo gem install mongrel -y
#sudo gem install mongrel_cluster -y


Setup Mongrel Cluster Startup Script:

#sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/mongrel_cluster
#sudo chmod +x /etc/init.d/mongrel_cluster
#sudo update-rc.d mongrel_cluster defaults
#sudo mkdir -p /var/run/mongrel_cluster
#sudo chown -R www-data:www-data /var/run/mongrel_cluster


Edit /etc/init.d/mongrel_cluste and add the following linesr:

#sudo pico /etc/init.d/mongrel_cluster

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local:/usr/local/sbin:/usr/local/bin
USER=www-data


Copy/Create/Download the root of your Rails application to the /var/www directory:

#cd /var/www/
#sudo rails myapp
#sudo chown -R www-data:www-data /var/www/phpmyadmin/
#sudo chown -R www-data:www-data /var/www/myapp/


Setup the Mongrel Cluster Rails config file(4 nodes):

#cd /var/www/myapp
#sudo mongrel_rails cluster::configure -e production -p 8000 -N 4 -c /var/www/myapp -a 127.0.0.1 –user www-data –group www-data


Let Mongrel Cluster know about your Mongrel Cluster Rails config file:

#sudo mkdir /etc/mongrel_cluster
#cd /etc/mongrel_cluster
#sudo ln -s /var/www/myapp/config/mongrel_cluster.yml myapp.yml


Add Apache Modules:

#sudo a2enmod proxy_balancer proxy_http proxy deflate php5


Create a new Apache site (e.g. /etc/apache2/sites-available/myapp):

#sudo vim /etc/apache2/sites-available/myapp


Add your custom VirtualHost directive (this will be for the default localhost):

<VirtualHost *:80>
DocumentRoot /var/www/myapp/public

#ServerName myapp.example.com
#ServerAlias myapp myapp.example.com
<Directory “/var/www/myapp/public”>

Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

# Configure mongrel_cluster
<Proxy balancer://myapp_cluster>
Order allow,deny
Allow from all
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
BalancerMember http://127.0.0.1:8003
</Proxy>

RewriteEngine On

# Prevent access to .svn directories
RewriteRule ^(.*/)?.svn/ - [F,L]
ErrorDocument 403 “Access Forbidden”

# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]

# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]

# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]

# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://myapp_cluster%{REQUEST_URI} [P,QSA,L]

# Deflate
AddOutputFilterByType DEFLATE text/html text/plain text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

ErrorLog /var/log/apache2/myapp-error_log
CustomLog /var/log/apache2/myapp-access_log combined
</VirtualHost>


Add your site to the Apache configuration and disable the default:

#sudo a2dissite default
#sudo a2ensite myapp


Install Subversion:

#sudo apt-get -y install subversion


Configure Subversion:

#cd /var/www/myapp
#sudo svnadmin create /var/www/svn
#sudo svnadmin create /var/www/svn/myapp
#sudo rm -rf /var/www/svn/myapp/conf
#sudo ln -s /var/www/svn/conf /var/www/svn/myapp/conf
#sudo svn mkdir -m 'Initial Layout' file:///var/www/svn/myapp/branches file:///var/www/svn/myapp/tags file:///var/www/svn/myapp/trunk
#sudo svn import -m 'Initial Import' . file:///var/www/svn/myapp/trunk
#sudo svn propset svn:ignore 'database.yml' config/
#sudo svn propset svn:ignore 'mongrel_cluster.yml' config/
#sudo svn propset svn:ignore '*' log/
#sudo svn propset svn:ignore '*' tmp/cache
#sudo svn propset svn:ignore '*' tmp/pids
#sudo svn propset svn:ignore '*' tmp/sessions
#sudo svn propset svn:ignore '*' tmp/sockets


Start the servers:

#sudo /etc/init.d/mongrel_cluster stop
#sudo /etc/init.d/apache2 stop
#sudo /etc/init.d/mongrel_cluster start
#sudo /etc/init.d/apache2 start


The configuration is a bit tedious, do mail in case you find any errors while going through the procedures.