Start and Stop All Your Mongrel_Cluster Processes

This has been updated. You can find the update here

So, Its possible to use mongrel cluster in a shared environment. I've shown that. But what about stopping and starting all these processes? No one wants to cap restart in every source directory after a server gets rebooted. And what if I need to shut them all down at once!? I can't be ghetto and sudo killall mongrel_rails, I've got self respect.

So to solve my little problem, I took the kick in the butt from Peter Cooper and finally created an init script for all the mongrel_clusters on a shared box.

The beauty of this is that when I add a new app to the server, I don't have to tell the script. Using conventions, it knows what rails apps I have installed.

Basically, when you call start, it iterates through the APP_DIR directory. When it finds a rails app with mongrel_cluster.conf in its config directory, it starts it, but not before it verifies that there isn't a pid under /log/. If it finds a pid, it just reports back to you that the app has already been started.

Calling stop does the inverse, with the same protections. Restart just does a stop then a start. If you've got a stuck process or some other wackyness going on, force_restart will skip past the checks and force restart your rails apps.

Huzzah! Another virtue of using conventions.

Grab the script here: svn export http://code.jtoe.net/rails/scripts/mongrel_processes.rb

Put it anywhere, chmod it, and symlink it to init.d

svn export http://code.jtoe.net/rails/scripts/mongrel_processes.rb
chmod 755 mongrel_processes.rb
sudo ln -s /home/dev/bin/mongrel_processes.rb /etc/init.d/mongrel_processes

If you're on (ubuntu|debian), add it to your start up processes like this:

sudo update-rc.d -f mongrel_processes defaults

And you're off...err automatically on...just reboot and try it out.

Popularity: 27% [?]

    Categories: Deployment, Rails     21 Comments »

quicktime_helper Plugin

Plugin #2. I built another helper to fulfill my need to embed Quicktime videos. Like the DRY developer I try to be, I decided I'd make a nice little plugin out of it. I took it even further by incorporating Geoff Stearns' great javascript Quicktime object, qtobject.

After including the 'qtobject.js' file in your view, using the quicktime_tag helper is simple:

quicktime_tag(:path_to_video_file, :id, :width, :height, options = {} )

Extra parameters that are used by the quicktime embed object can be added using the options hash. For Example:

quicktime_tag(:path_to_video_file, :id, :width, :height, {:loop => true, :autoplay => false} )

More Info: http://github.com/maddox/quicktime_helper/tree/master
Install


git clone git://github.com/maddox/quicktime_helper.git vendor/plugins/quicktime_helper

Popularity: 5% [?]

    Categories: Rails     4 Comments »

aim_status_helper Plugin

This plugin has been DEPRECATED. You can find the update here.

For a project I'm working on, I needed to display a user's AIM status. I thought it might be useful, so I extracted it to a plugin.

It's pretty simple to use, just pass in someone's AIM user name.

aim_status('maddox123456')

aim status maddox123456

Optionally, you can toss in the text you want to display.

aim_status('maddox123456', :show_name => 'Jon Maddox')

aim status Jon Maddox

UPDATE 9/21/06 I changed the ON url so it will work through a proxy like OFF url.

Popularity: 3% [?]

    Categories: Rails     4 Comments »

Clustered Mongrel In A Shared Environment

Working at a marketing firm, a lot of our client work doesn't end up as mission critical, high bandwidth sites. We end up pushing out projects that are either corporate sites requiring content control or content management apps that are used by a small subset of their employees.

Because of this, we have always deployed sites in a controlled, shared environment. We have our own boxes that our client work sits on. As we deploy more sites powered by Ruby and Rails, I've been on a mission for a nice deployment stack.

Since RailsConf, I've come to a satisfying decision of Apache 2.2, mod_proxy_balancer, and Mongrel. My next task was to get this stack working with multiple Rails apps in a shared environment. Luckily, getting this working is very trivial.

If you've read Coda's motivating post on getting this stack up and running, then some of this will be familiar to you. I'd suggest reading this first. My tips just build off of it.

Getting multiple Rails apps working in a shared environment is basically all up to Apache. Because your sites will be driven individually by Mongrels on separate ports, Apache proxies to them and vhosts become your friend.

mongrel.common

Like Coda, I kept my configuration DRY by creating a single file called mongrel.commmon. This contains all the configuration that every Rails app will need when being proxied from Apache. There's no need to repeat this stuff in every vhost.


RewriteEngine On

# 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]

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

First, we have Apache keep a look out for our maintenance.html file which is created by our nice disable_web Capistrano task. If its found, all traffic coming through Apache will be routed to it.

Next, we're using mod_rewrite to make sure Apache serves out our index files and cached pages. Having Apache serve our cached static files rather than our Mongrels, speeds everything up quite a bit.

The last thing we're telling Apache to do is to compress our static files to speed up and lower our outbound bandwidth. Everything gets decompressed on the clients browser. More on mod_deflate here.

site.vhost

The other file we need to get our new domain set up on our shared server is the domain's specific vhost file. Everything in this file will be contextual specifically to the domain.


<VirtualHost 62.44.22.11:80>
Include conf/mongrel.common

ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/dev/apps/domain.com/current/public


ErrorLog logs/domain.com_errors_log
CustomLog logs/domain.com_log combined

<Directory "/home/dev/apps/domain.com/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

<Proxy balancer://domain.com_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
</Proxy>

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


</VirtualHost>

First off, add the ip address that your vhost will sit on listening for traffic. This will usually be the ip address of your server, and most importantly the ip that your domain resolves to. People familiar with shared hosting will recognize this. In addition, make sure you add...


NameVirtualHost 62.44.22.11:80

This allows you to use name based virtual hosting with Apache.

The next thing we do is include our nice abstracted configuration, mongrel.common. Then we add the typical things an Apache vhost file would have, ServerName, alias, logs, etc. We make sure we point the DocumentRoot at the public folder of our sites so Apache can serve our static files correctly.

Then we define our Mongrel cluster. Define a name for our cluster, and tell Apache where our Mongrels are running (these are defined in your config/mongrel_cluster.yml). Next, tell Apache which cluster you want your traffic proxied to via mod_rewrite. See the named cluster?

And that's it. Include the vhost file via your httpd.conf Apache conf file, and your new domain is ready to go. All you have to do to set up another site on this same server is (after doing the normal Rails portion setup. You ARE using Capistrno right?) to create a new vhost file and restart Apache.

I've probably used bit more words than needed to go over this, but I just wanted to show that its easy to use this great deployment stack in a shared environment.

Popularity: 16% [?]

    Categories: Deployment, Rails     6 Comments »

Apache 2.2, mod_proxy_balancer, & Mongrel on Ubuntu 6.06

Ok after playing with Ubuntu, Apache 2.2, mod_proxy_balancer, and Mongrel, I've decided that this is the deployment stack I want to use for at least the next month. snicker

Since I was using a few different sources as references while setting this up, I thought I'd post exactly what I did to get everything installed correctly to suit my needs. This won't go into how to actually DEPLOY your rails app into this environment, but it'll show you what you need to do to get it all installed and set up.

UPDATE: This works on Edgy (6.10) as well...

UPDATE 2: Some things updated for Apache 2.2.4

UPDATE 3: Now with Subversion hotness

Deployment Stack

  • Ubuntu Linux 6.06 Dapper Drake
  • Apache 2.2
  • mod_proxy_balancer
  • Mongrel

Sources

Everything below was taken directly (and sometimes even copied and pasted) from the sources above. They are the real heros here. I"m just the aggregator. I'm pretty sure the "one customer" mentioned in the rimuhosting post is Ezra as well. But I could be mistaken.
Ok, lets start.

Its assumed that you are su'd to root.

Core Essentials

Make sure you remove any version of apache that already exists!

sudo dpkg --purge apache apache2

Install the GCC compilers and developer tools.

sudo apt-get install build-essential

Now we'll install Ruby and friends:

sudo apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8

Next we need to make some symlinks for basic Ruby commands.


sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb

Apache & Pals

Download and compile Zlib


wget http://www.zlib.net/zlib-1.2.3.tar.gz
tar -xvf zlib-1.2.3.tar.gz
./configure
make
sudo make install

Download and compile Apache 2.2


wget http://apache.rmplc.co.uk/httpd/httpd-2.2.4.tar.gz
tar -xvf httpd-2.2.4.tar.gz
./configure --prefix=/usr/local/apache --enable-proxy   --enable-proxy-http   --enable-proxy-balancer --enable-dav --enable-rewrite    --enable-so --enable-http   --enable-ssl    --enable-expires  --enable-headers  --enable-mods=deflate_module --with-php --with-mysql --with-susexec --disable-info  --without-berkeley-db --enable-dav=shared --enable-dav-lock=shared --with-included-apr
make
sudo make install

MySQL & Postfix

Lets install Postfix here really quick. We will configure it later but if we don't install it before we install MySQL, apt will pull in Exim as a dependency. We don't want this so lets pre-empt it by installing Postfix.
sudo apt-get install postfix

The Postfix installer will ask you a question about what type of server it is. I just chose "Internet Site".

Now let's install MySQL and friends.
sudo apt-get install mysql-server mysql-common mysql-client libmysqlclient15-dev libmysqlclient15off

Install the MySQL bindings.
sudo apt-get install libmysql-ruby1.8

Now test it:


irb
irb(main):001:0> require 'mysql'
=> true
irb(main):002:0> exit

Now we will install RubyGems so we can get Rails and all kinds of other Ruby treasures!


wget http://rubyforge.iasi.roedu.net/files/rubygems/rubygems-0.9.0.tgz
tar xvzf rubygems*
cd rubygems*
sudo ruby setup.rb

Now we'll install the latest version of Rails.
sudo gem install rails --include-dependencies

You should see (as of Rails 1.2.3):


Successfully installed rails-1.2.3
Successfully installed rake-0.7.2
Successfully installed activesupport-1.4.2
Successfully installed activerecord-1.15.3
Successfully installed actionpack-1.13.3
Successfully installed actionmailer-1.3.3
Successfully installed actionwebservice-1.2.3
Installing ri documentation for rake-0.7.2...
Installing ri documentation for activesupport-1.4.2...
Installing ri documentation for activerecord-1.15.3...
Installing ri documentation for actionpack-1.13.3...

lib/action_controller/routing.rb:1061:30: ':' not followed by identified or operator

lib/action_controller/routing.rb:1065:39: ':' not followed by identified or operator
Installing ri documentation for actionmailer-1.3.3...
Installing ri documentation for actionwebservice-1.2.3...
Installing RDoc documentation for rake-0.7.2...
Installing RDoc documentation for activesupport-1.4.2...
Installing RDoc documentation for activerecord-1.15.3...
Installing RDoc documentation for actionpack-1.13.3...

lib/action_controller/routing.rb:1061:30: ':' not followed by identified or operator

lib/action_controller/routing.rb:1065:39: ':' not followed by identified or operator
Installing RDoc documentation for actionmailer-1.3.3...
Installing RDoc documentation for actionwebservice-1.2.3...

Don't worry about those couple of errors. That just happens sometimes when building the RDoc documentation. If you look closely, you will see that it says "Successfully installed foobar..." a bunch of times.

Mongrel & Friends

Install mongrel and its supporting software. Make sure when prompted you select the most recent, non mswin32 option.
sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies

It was previously advised to install Sendfile in this stack, but apparently it doesn't serve the static content like it was once suggested. So the speed boost isn't actually there. This means its just extra software to blow up. So don't install it.

This comes strait from Coda, which came strait from Zed. And you listen to Zed.

Subversion

Ok, now lets throw subversion on the box. You might need to grab the libxml2 libs, so apt-get them first. Then download Subversion and compile it. You'll probably be warned that the Berkeley DB isn't installed. Just ignore it. I do.


sudo apt-get install libxml2-dev
wget http://subversion.tigris.org/downloads/subversion-1.3.2.tar.gz
tar xfz subversion-1.3.2.tar.gz
cd subversion-1.3.2
./configure --with-apr=/usr/local/apache/bin/apr-1-config --with-apr-util=/usr/local/apache/bin/apu-1-config --with-apxs=/usr/local/apache/bin/apxs --without-berkeley-db --with-ssl

Further Reading

Simply put, go read Coda's post on how to turn all the elements you just installed into a real working mongrel cluster.

I'm in the process of setting up a shared environment for my pre-production server. So I'll have this stack working for multiple sites on one computer using virtual hosts. I'll share the apache conf files when I've got them finalized.

Popularity: 33% [?]

    Categories: Deployment, Rails, Ruby     23 Comments »