apache proxy balancer + mongrel clusters and deploying application with capistrano


So you want to setup production server with mongrel clusters and apache proxy balancer, also wants to use capistrano for deployment, huh. Take it easy, its very simple.

You need Apache 2.2 or later on your production server, and the following ruby gems on your both machine(server and local):

  • capistrano
  • mongrel
  • mongrel_cluster

I haven’t mentioned rails and rake gem as we are deploying a rails application so these gems are obvious.

Lets install above gems (if not installed) by issuing:

Now make sure that the following modules are enabled (they are disabled by default) :

  • mod-rewrite
  • mod-proxy
  • mod-proxy-http
  • mod-proxy-balancer

to check if they are enabled issue ” /etc/init.d/apachectl -M ” on server, it will list all the enabled modules. For Debian systems all enabled modules are in /etc/apache2/mods-enabled directory and all available modules are in /etc/apache2/mods-available directory, to enable them issue ” a2enmod MOD_NAME “.

Now create the production database(on server) and update database.yml for production database settings.

After this configure mongrel by issuing ” mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 2 -c ./ ” inside the rails application root directory(on client machine). This will create mongrel_cluster.yml in config directory. You can change parameters in this command, as -p 8000 specifies that mongrel instances will start up on port number starting 8000, -a 127.0.0.1 specifies that mongrel instances will listen to the localhost, -N specifies the number of mongrel instances and -c specifies the rails root directory.

Now its time to capistranize rails application, issue ” cap –apply-to ./ APP_NAME ” inside rails application root directory(on client machine), this will add two files(config/deploy.rb and lib/tasks/capistrano.rake) to the rails application. Edit deploy.rb according to your requirements. Also add the following code to deploy.rb :

Now on your local machine issue these two commands in only once ” rake remote:setup ” and ” rake remote:cold_deploy “, when you issue ” rake remote:setup ” it will prompt for the server password and create necessary directories on the server and ” rake remote:cold_deploy ” will deploy your code to the server. Next time whenever you want to deploy to the server you just need to issue ” rake remote:deploy ” not ” rake remote:cold_deploy “.

Now we are just one step back, we need to configure apache proxy balancer for mongrel instances. Add the following code to the httpd.conf file:

you need to change the above code according to you requirement. Also you need to restart apache server by issuing ” /etc/init.d/apachectl restart “. Now you are done.

But we also need to add a script to start mongrel instances when the server restarts, otherwise whenever the server restart there will no mongrel instance running.

Just create a file named mongrel_clusters (you can choose any name) in /etc/init.d directory with the following code:

You need to change the BASEDIR in the above code. Make this file executable by ” chmod +x /etc/init.d/mongrel_clusters
Issue these commands to add this script at system startup:

Debian: /usr/sbin/update-rc.d /etc/init.d/mongrel_clusters defaults
RedHat: /usr/sbin/chkconfig –add /etc/init.d/mongrel_clusters and /usr/sbin/chkconfig –level 2 /etc/init.d/mongrel_clusters on

Note: Use above instructions at your own risk, if your computer explodes its not my fault :-)


Reader Comments

Hi,
just some little additions :

mongrel_cluster gem include some capistrano extensions to forget abour setting tasks like restart by hand, just add :

require ‘mongrel_cluster/recipes’

at the begining of your deploy.rb.

Then, the gem provides a startup script you can use to handle your mongrel clusters, it’s /your/gem/path/mongrel_cluster-x.x.x/resources/mongrel_cluster and it reads the mongrel cluster configs in /etc/mongrel_cluster/ which allows you handle the start/restart of many different apps just by symlinking config files to this directory.

Hope it can help.

great,
Thanks Jonathan

in order for your machine to reboot properly you will still need to link the /etc/init.d/mongreel to /etc/rcN.d/ scripts

I have a problem to execute the command “rake remote:setup”.
I allways get an error like this:
rake aborted!
Don’t know how to build task ‘remote:setup’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1472:in `[]’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `each’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1711:in `run’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in `standard_exception_handling’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run’
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.3/bin/rake:7
/usr/local/bin/rake:16:in `load’
/usr/local/bin/rake:16
It seems that rake doesn’t know the command “remote:setup”.
Would be nice for helping!

Sebastian,

It seems that you have installed capistrano 2.0 . Issue ‘gem l cap’ to know your capistrano version.
rake remote:setup works for earlier version of capistrano, For capistrano 2.0 you need to issue:

rake deploy:setup
rake deploy:cold
rake deploy

Hmmm, thanks for help, but these commands give me the same error as described. :(

I found out:
instead to write rake it have to by cap. So the commands are now:

cap deploy:setup
cap deploy:cold
cap deploy

Important! In the config/deploy.rb file you mast set the correct path to the app dir!
The line have to look like this:
set :deploy_to, “/path/to/your/ror/app”

Thanks for helping!

sorry forgot about ‘cap’

[…] To see what I mean lets start with what Google suggests. The reverse proxying gets setup something like this: […]

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.