- Knowledgebase Home
- » Browse by Service
- » (dv) & Nitro Servers
- » (dv) 3.5
- » Web Technologies
- » Ruby On Rails
Ruby on Rails using Mongrel Clusters
SUMMARY
Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications using the Apache webserver. Proxying requests to a mongrel cluster is just one way of serving your Ruby on Rails web applications.
STOP:
- The following steps require being logged in with the user root.
- If you currently do not have your root user enabled or our Developer Tools installed please visit the Root Access & Developer Tools section of your AccountCenter.
- Most of these steps are accomplished by simply copying/pasting the commands from each step.
- Whenever installing Third-Party software please consult the official documentation. (mt) Media Temple does not support the installation and configuration of software not installed at time of service activation. Please consult our (dv) Scope of Support page for further explanation.
- All of our KnowledgeBase articles use the domain ‘mt-example.com’ as a placeholder. Please make sure to replace this with your actual domain name when applicable.
- If you get an error, you may want to update your gems with the following command:
gem update --system
STEPS
1. Update ruby to 1.8.6:
The (dv) 3.x servers come with ruby 1.8.5 installed. It is recommended to upgrade to 1.8.6. Installing yum is the best way to remove the older version.
First install yum by following this article.
Now that yum is installed we will remove ruby. Please answer yes at the confirmation dialog:
yum remove ruby
Now we will download the source for ruby 1.8.6 from ruby's FTP site:
wget ftp://ftp.ruby-lang.org:21/pub/ruby/1.8/ruby-1.8.6.tar.gz
Decompress the file and change to the new directory:
tar xzvf ruby-1.8.6.tar.gz && cd ruby-1.8.6
Now we will compile the source and install the new binaries on your server at /usr/local/bin/:
./configure && make && make install
As of October 28, 2008, the latest version of rubygems is 1.3.1. This article is based on that version. Newer versions may not be compatible with the rest of the steps in this document.
Refresh your profile and verify that you are now running ruby 1.8.6
source /etc/profile;ruby -v
If your upgrade was successful you should see the following output:
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]
2. Install Rubygems:
Create a configuration file with the proper directive and refresh your environment:
echo 'export PATH=/usr/local/bin:$PATH' > /etc/profile.d/ruby.sh; source /etc/profile
Download the RubyGems distribution:
wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
Unpack the archive:
tar -xzvf rubygems-1.3.1.tgz
Change to the decompressed directory:
cd rubygems-1.3.1
Add the following environment variable and create your gems directory:
export GEM_HOME=/usr/local/rubygems/gems; mkdir -p $GEM_HOME
Execute the following ruby script to set up ruby with the above path:
ruby setup.rb all --prefix=/usr/local/rubygems
As of October 28, 2008, the latest version of rubygems is 1.3.1. This article is based on that version. Newer versions may not be compatible with the rest of the steps in this document.
Create a /etc/profile.d/rubygems.sh file with vi:
vi /etc/profile.d/rubygems.sh
Copy the following contents into the file:
export GEM_HOME=/usr/local/rubygems/gems
export GEM_PATH=/usr/local/rubygems/gems
export RUBYLIB=/usr/local/rubygems/lib
export PATH=/usr/local/rubygems/gems/bin:/usr/local/rubygems/bin:$PATH
Refresh your environment profile once more:
source /etc/profile
3. Install Rails and related dependencies:
When installing gems make sure to choose the latest version for ruby, and NOT mswin32. This option is usually option 1, but pay close attention as some gems will not have 'ruby' as the first choice.
gem install rails
gem install daemons
gem install fastthread
gem install mongrel
gem install mongrel_cluster
gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql
If you get the following error during any of the above steps...
Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR: While executing gem ... (Gem::GemNotFoundException)
Could not find 'X' (> 0) in any repository
...simply run the same command again. It should process successfully at that point.
4. Configure the mongrel_cluster daemon:
Create the 'mongrel' user:
adduser mongrel
As of this writing, the newest version of mongrel_cluster is 1.0.5. You may manually install newer versions, but that might result in changes not covered by this article.
Copy the init script for mongrel_cluster to the init.d directory and make it executable:
cp /usr/local/rubygems/gems/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/; chmod +x /etc/init.d/mongrel_cluster
Configure the mongrel_cluster daemon to start automatically:
/sbin/chkconfig --level 345 mongrel_cluster on
Create some necessary directories that mongrel_cluster needs to exist:
mkdir /etc/mongrel_cluster
mkdir /var/run/mongrel_cluster
5. Set up your initial 'testapp' domain:
Create a new domain inside of your Plesk Control Panel. We will call it testapp.mt-example.com
Edit and/or create /var/www/vhosts/testapp.mt-example.com/conf/vhost.conf with vi:
vi /var/www/vhosts/testapp.mt-example.com/conf/vhost.conf
Insert all of the following lines into that file:
<Directory /var/www/vhosts/testapp.mt-example.com>
Options +FollowSymLinks
</Directory>
RewriteMap servers rnd:/var/www/vhosts/testapp.mt-example.com/rails/servers.txt
<Directory /var/www/vhosts/testapp.mt-example.com/httpdocs>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://${servers:server}/$1 [P]
</Directory> Rebuild the apache configuration to read this new vhost.conf file using the Plesk command-line tool websrvmng:
/usr/local/psa/admin/sbin/websrvmng -a
Change to the domain's directory:
cd /var/www/vhosts/testapp.mt-example.com
Create the 'rails' directory and change ownership to your domain user. Install a test application called testapp using the rails command:
mkdir rails; chown username:psaserv rails; cd rails
rails testapp
Now we must remove the default 'httpdocs' directory and create a symbolic link (symlink) from the testapp's public folder:
mv /var/www/vhosts/testapp.mt-example.com/httpdocs /var/www/vhosts/testapp.mt-example.com/httpdocs.old
ln -s /var/www/vhosts/testapp.mt-example.com/rails/testapp/public /var/www/vhosts/testapp.mt-example.com/httpdocs
This command will set up a servers.txt file for your mongrel_cluster setup to use 3 mongrel instances. You can modify this command by adding another pipe and incrementally go up on the port number. For example: '127.0.0.1:4000|127.0.0.1:4001|127.0.0.1:4002|127.0.0.1:4003' will have 4 mongrel daemons standing by. You MUST make special note of this to match the command you use when generating your mongrel_cluster.yml file in the next step of this document.
Create a file called servers.txt in /var/www/vhosts/testapp.mt-example.com/rails/servers.txt with the following proxy information:
echo 'server 127.0.0.1:4000|127.0.0.1:4001|127.0.0.1:4002' > /var/www/vhosts/testapp.mt-example.com/rails/servers.txt
Generate the mongrel_cluster.yml file:
cd /var/www/vhosts/testapp.mt-example.com/rails/testapp
mongrel_rails cluster::configure -N 3 -p 4000 -e production -a 127.0.0.1 -c /var/www/vhosts/testapp.mt-example.com/rails/testapp --user mongrel --group mongrel
The '-N 3' part of the above command will configure your mongrel_cluster.yml to run 3 mongrel daemons. Increasing this value is contingent on the configuration of your servers.txt file above. The '-p 4000' setting is for the starting port. In the case of running multiple apps you should start with '5000' or '6000', etc. as you add more applications. This should also be noted when creating additional servers.txt files.
Modify the line in /var/www/vhosts/testapp.mt-example.com/rails/testapp/config/mongrel_cluster.yml to reflect the appropriate pid file directory:
perl -pi -e 's/tmp\/pids\/mongrel\.pid/\/var\/run\/mongrel_cluster\/mongrel\.pid/g' /var/www/vhosts/testapp.mt-example.com/rails/testapp/config/mongrel_cluster.yml
Symlink mongrel_cluster.yml to /etc/mongrel_cluster/testapp.yml:
ln -s /var/www/vhosts/testapp.mt-example.com/rails/testapp/config/mongrel_cluster.yml /etc/mongrel_cluster/testapp.yml
Test the apache configuration:
/etc/init.d/httpd configtest
If all is OK, reload the apache configuration:
/etc/init.d/httpd reload
Modify /var/www/vhosts/testapp.mt-example.com/rails/testapp/public/.htaccess to contain the proper directive:
cp /var/www/vhosts/testapp.mt-example.com/rails/testapp/public/.htaccess /var/www/vhosts/testapp.mt-example.com/rails/testapp/public/.htaccess.old && echo 'ErrorDocument 500 "Application error Rails application failed to start properly"' > /var/www/vhosts/testapp.mt-example.com/rails/testapp/public/.htaccess
The .htaccess file in the public directory should not contain any directives to start dispatch.cgi. This will cause errors that will show up in the error_log and make your application unusable. These need to be removed from the .htaccess file so that Apache will proxy all requests to the Mongrel cluster. The only line in the file should be:
ErrorDocument 500 "Application error Rails application failed to start properly"
Fix permissions for your application's 'sessions' and 'log' folders:
chown -R mongrel:mongrel /var/www/vhosts/testapp.mt-example.com/rails/testapp/tmp/sessions
Start the mongrel_cluster daemon:
/etc/init.d/mongrel_cluster start
If you encounter an error similar to this read below:
** Ruby version is not up-to-date;
loading cgi_multipart_eof_fixit is normal to proceed.
Mongrel will present this warning if the Ruby version is below 1.8.6. You may attempt to upgrade to a newer version of Ruby if you like but it is not necessary. This error can be ignored.
You can now view your application at: http://testapp.mt-example.com/
Congratulations!!
Revisions:
05-05-2009: Changed article to point to new yum article. Thanks to Jerson V.
01-30-2009: Fixed "tar xzvf ruby-1.8.6.tar.gz && cd ruby-1.8.6" Thanks for the comment Stephen K
12-24-2008: Updated with instructions for using rubygems 1.3.1. Added steps for upgrading ruby to version 1.8.6. Other clarifications to ease overall install. Happy Holidays!!
09-03-2008: Updated article to reflect new RubyGems version.
05-14-2008: Fixed the categories this article was set to.
05-07-2008: : Made some slight corrections to code examples to verify proper syntax.
05-06-2008: : Fixed some issues from our import from the old KnowledgeBase. - Thanks to Mike Nicholaides for pointing this out.
Fields marked with an asterisk(*) are required. Comment on this article