How to setup a Linux web server using the command line, Part 6 : Mongrel Clusters

#
# Configuring and Administering Mongrel Clusters for your Rails Sites
#

Now that we have our Rails site’s vhost setup and enabled in Apache server, and perhaps we’ve enabled our site with Subversion and Capistrano, we can create a Mongrel Cluster configuration to run the wonderful Ruby code we’ve written.  We definitely want our Mongrel Cluster to startup when the server reboots, and if we’ve capified our application with Capistrano in Part 5, we’ll want Capistrano to be able to restart our Mongrel Cluster after deploying a new version of our application.

# on remote server

# configure rails application’s mongrel cluster (into application root directory!!!)
# make sure to set the correct port number (-p) and number of mongrels (-N)

# capistrano sites use the “/current” directory as their root
mongrel_rails cluster::configure -e production -p 8000 -N 2 -c /home/super/public_html/mydomain1.com/current -a 127.0.0.1

# or if you’re not using capistrano, your config command may be
mongrel_rails cluster::configure -e production -p 8000 -N 2 -c /home/super/public_html/mydomain1.com -a 127.0.0.1

# start mongrel_cluster

mongrel_rails cluster::start

# configure mongrel_cluster autostart on reboot (once for server)

# copy mongrel_cluster resource to startup (init.d) directory
sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/

# set executable permissions on the mongrel_cluster startup resource
sudo chmod +x /etc/init.d/mongrel_cluster

# update rc.local boot file
sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults

# make sure symlink is in place for working ruby installation
sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby

# add mongrel user and add to www-data group
sudo useradd mongrel
sudo usermod -a -G www-data mongrel

# create symlink for your mongrel_cluster.yml so it survives reboots (for each site/vhost)

# make directory for mongrel cluster settings
sudo mkdir /etc/mongrel_cluster

# capistrano users
sudo ln -s /home/super/public_html/mydomain1.com/current/config/mongrel_cluster.yml /etc/mongrel_cluster/mydomain1.com.yml

# non-capistrano users
sudo ln -s /home/super/public_html/mydomain1.com/config/mongrel_cluster.yml /etc/mongrel_cluster/mydomain1.com.yml

# Subversion and Capistrano users: At this point, you’ll want to make a copy of the “config/mongrel_cluster.yml” file into your local working copy, commit it to svn, and “cap deploy” your application so it is regenerated in a new capistrano “release”. For example, on your local machine:

# navigate to your local working copy root
cd ~/work/myproject1_trunk

# create a new file, and copy-paste from remote file
nano config/mongrel_cluster.yml

# commit the new file to subversion
svn add config/mongrel_cluster.yml
svn commit -m “added mongrel cluster config file”

# deploy your changes with capistrano
cap deploy

# Subversion and Capistrano users: Now back to your remote server and continue…

# control mongrel_clusters (from your application root directory!!!)

cd ~/public_html/mydomain1.com/current

mongrel_rails cluster::status
mongrel_rails cluster::stop
mongrel_rails cluster::start
mongrel_rails cluster::restart

# and
mongrel_cluster_ctl status
mongrel_cluster_ctl stop
mongrel_cluster_ctl start
mongrel_cluster_ctl restart

# now let’s make sure your site is enabled in apache

ls /etc/apache2/sites-enabled
# if you don’t see “mydomain1.com” listed, enable it
sudo a2ensite mydomain1.com

# (re)start apache server

sudo /etc/init.d/apache2 start
# or
sudo /etc/init.d/apache2 reload

Done!

Now you should be able to reboot your remote server and see your apache2-enabled, mongrel_cluster-riding, subversion-tracked, and capistrano-deployed websites from a web browser

http://mydomain1.com
http://mydomain2.net
http://mydomain3.ws
# secure versions (self-signed)
https://mydomain1.com
https://mydomain2.net
https://mydomain3.ws

Really done!

Leave a Comment

You must be logged in to post a comment.