Ask Your Doctor About mod_rails 25 comments

posted Tuesday, May 20, 2008 by topfunky

We need new inventions that reveal people’s true intentions, a portable pride protector, affordable lie detector…—Buck65

UPDATE: You can learn all about Phusion Passenger with the new PeepCode Screencast on the topic.

I was initially skeptical about mod_rails. The developers had the cojones to start promoting it weeks before it was ready, and they managed to generate a lot of hype around it before anyone had used it.

And we all know that hyped products never deliver!

Mostly, I was probably one of those developers described when they said

...we believe that experienced Ruby on Rails developers don’t consider deployment to be a problem; probably annoying at most…

Which is true. I was pretty happy with my current setup of Nginx and Mongrel. My only problem was that Nginx would often fail to create a pid file when the logs were rotated every night.

I still use Nginx and Mongrel at PeepCode, but I’ve switched a few smaller apps on a separate VPS to mod_rails. It turns out that there are many benefits to using mod_rails, both in development and production.

Great for development

It’s easy to ignore the pain of development. I didn’t realize how tedious it was to start and stop Mongrel while in development. It was also the reason I keep so many terminal tabs open at once.

With mod_rails, apps are always ready whenever I need them. If I need to watch the logs, it’s an easy tail -f log/development.log.

I have many pages that access dynamically generated graphs. A single Mongrel would process each serially, which would often take quite a while. With mod_rails, it’s nearly instant since new processes are spawned to handle the extra requests in parallel.

Configuration

I followed Manfred’s article to start using mod_rails in development. Two tweaks were to edit /etc/hosts and to point Apache to a local config file in my home directory.

First, edit /etc/hosts with fake names for the applications you’ll be developing against.

127.0.0.1    localhost peepcode.local podcast.local

Next, in /etc/apache2/httpd.conf, I set the RailsEnv to development and myself to the RailsDefaultUser. I also included a config file in my home directory (stored in source control). This config file includes all my virtual hosts and is the same on both my laptop and my desktop machine.

RailsEnv development
RailsDefaultUser topfunky
Include /Users/topfunky/repos/configs/mod_rails_vhosts.conf

The mod_rails_vhosts.conf file has several VirtualHost sections like this:

ServerName podcast.local
DocumentRoot /Users/topfunky/repos/podcast/public

Easy to debug visual plugins or graphs

Sometimes I need to visually debug code located in plugins or frozen gems. For example, I worked on some enhancements to my sparklines gem while viewing graphs on a reporting page. Normally, this would be impossible since Rails doesn’t reload itself for changes in the vendor directory.

Rstakeout comes to the rescue!

rstakeout "touch tmp/restart.txt" "vendor/**/*"

When I edit a plugin or gem in the vendor directory, mod_rails reloads the whole application and I can see the result immediately.

Rotate Logs

Other production tasks become very easy with mod_rails. Rotating the Rails logs is as easy as touching the restart.txt file.

Here’s /etc/logrotate.d/rails:

/var/www/apps/rubyonrailsworkshops.com/shared/log/production.log {
  daily
  rotate 7
  compress
  missingok
  sharedscripts
  postrotate
    touch /var/www/apps/rubyonrailsworkshops.com/current/tmp/restart.txt
  endscript
}

Coexist

Many people are sad that mod_rails doesn’t yet support rack. No problem. I just followed Coda Hale’s classic article and am running Merb under Mongrel through Apache, alongside a few mod_rails apps.

Problems

The only initial problem I’ve experienced is that Apache seems to use much more memory than Nginx did. A few days after switching to Apache, the memory usage skyrocketed and killed all my mod_rails apps. I had to restart Apache to get everything back.

Memory Usage
Figure A Memory Usage

I’m working on a god config file to keep tabs on everything. Does anyone have a good one yet?

At PeepCode

Scott Chacon recently published a Git Internals PDF that Jamis Buck called required reading for all Git users. He also launched GitCasts.

Mike Mondragon and Luke Francl finished a book on MMS2R. If you’ve ever tried to receive email into a web application, you know it can be quite a pain. MMS2R is a gem that makes it super simple to receive both plain text and multimedia attachments. It’s made me want to write an email-powered application just so I can use the concepts they show in the book!

25 comments

Leave a response

  • You don’t really have to set RailsDefaultUser because it defaults to the owner of config/environment.rb. For example, when config/enviroment.rb belongs to topfunky:staff, the Rails process wil start belonging to user topfunky.

    Regarding the memory issue, Passenger can sometimes spawn a lot of processes which might not fit in memory. You can limit them in the following way:

      RailsMaxPoolSize 3 # Never spawn more than three processes
      RailsPoolIdleTime 180 # Despawn a process when it has been idle for 180 seconds

  • Good to know that memory usage killing mod_rails isn’t an issue I am seeing in isolation. Generally I get 3-4 days between restarts of Apache, but sometimes a lot shorter.

    Impressed otherwise.

  • Gravatar icon Hongli Lai

    Actually, by the we announced the product, it was already near completion. Even before the announcement, we had been intensively testing Passenger. We spent the last few weeks mostly on polishing Passenger and fixing any bugs that we encounter. So don’t generate hype unless we know that we can deliver. :)

    As for memory usage, might switching to the ‘worker’ MPM be a good idea? The development version of Passenger is more stable and uses less memory, and supports Apache’s worker MPM, which in itself uses less memory than the default ‘prefork’ MPM. We recommend the following worker MPM setting for VPSes:
    StartServers             1
    ThreadsPerChild         10
    MaxClients              10
    MinSpareThreads          1
    MaxSpareThreads          1
    MaxRequestsPerChild  50000
    ThreadStackSize     500000

  • The permissions on my home directory were rwxr--r-- (744), which meant I was getting Permission Denied errors from Apache. I was trying to load some rails projects from /Users/evansj/rails/ into different vhosts.

    Changing the permissions of my home directory to rwxr-xr-x (755) made it work.

  • Gravatar icon Gustavo Delfino

    I would like to monitor memory usage in my server, just like in your figure. What tool are you using for that?

  • Gravatar icon Craig

    There has been some talk on the passenger group about the apache problems. People seem to to be experiencing it on the weekends, when traffic goes down to zero.

    Apparently the latest version in git solves a lot of the problems.

  • Gravatar icon Mark

    I fully recommend using monit for watching your apache process. It has some pretty advanced features for interacting with apache as well (aside from resource mgmt it can also interact with mod_status). Some of the ldap mods for apache lock up the processes and monit quickly restores order to the machine.

  • I’ve been using mod_rails for a few days now on my blog which is technically “production”. I’ve had some issues with a lot of ruby processes spawning and eating up a lot of memory. I’ve lowered the RailsMaxPoolSize to 2 and The RailsPoolIdleTime to 90. The memory usage seems high, but I’m on a 256MB Slice from SliceHost so that’s probably a contributing factor. There are also some improvements to be made in my actual blog, but those enhancements will come in time.

    Overall I’ve enjoyed using mod_rails especially since I come from a PHP background and it feels much more like home. I think that with continued development and improved performance it will big a big factor in making rails more attractive to people who are looking for an easy deployment process to go along with the easy development process.

  • Think: mod_rails.. git push.. automated migrations.. (almost) no more capistrano for most usage.

  • Gravatar icon Edward Ocampo-Gooding

    Woo Canadian music reference!

  • same issue with memory… i had to back to the mongrels =(

    anyway i like very much the mod_rails approach i’m still planing to use it in my small deployments.

    cheers!

  • Gravatar icon topfunky

    @Manfred: Thanks for the tip about not needing to set RailsDefaultUser. I have been setting RailsMaxPoolSize, but that hasn’t helped my memory problems.

    @Hongli: I’ve continued to have horrendous memory issues and unexpected silence from Apache. I restarted Apache with the settings you mentioned and initially, it seems to work better (the last 10 minutes).

    @Gustavo: I’m using Munin. It was horrendous to install on CentOS, but there are packages for other Linux distros.

    It’s good to hear that some of the memory issues are being solved. I would really like to be able to use mod_rails for my VPS (if it can be as reliable as Nginx and Mongrel are).

  • do you think that with mod_rails our framework will be so popular like php?

  • If you’re on CentOS setting up munin is a lot easier using the Dag Wieers repo. It has monit and munin.

    Not sure if that’s what you meant it being horrendous to install.

  • Hi Geoffrey,

    Barry here from IrishDev.com – we’re the organisers of the Irish Open Source Technology Conference. We’re working with various community groups to put on this show, one of them being the Ruby Ireland guys. We’ve got an additional track running and they have suggested they’d like you to fill one of the sessions.

    Are you available on June 18th – 20th?

  • I have not seen any of the memory issues … in fact my memory usage has gone down … but I limited the pool size to 3 rather than the default 20

  • Gravatar icon erwin

    excellent.. I can run easily all my https areas… just one trck I could not handle …. DEBUGGING

    when using ruby-debug and debugger command, how can I check it ? anyone already wrote about it ?

    btw , hourra for the Munster… they won.. but but.. not playing exciting game… pick and go pick and go… efficient but borring

  • I think you can use the remote debugging that ruby-debug provides [google ruby-debug remote]

  • any word on if this state of mod_rails is the same? Still slightly buggy?

  • I’ve been wanting this for quite some time. Coming from the PHP world years ago, mod_php has proven stable, fast and production ready.

    However, after your latest update Geoffrey I think we’ll stick with Pound/Apache/Mongrel, Pound/Nginx/Mongrel for now, but we’ll keep our fingers crossed.

  • I ended up having to add the _www user to my users group (mikehale). On leopard you’ll need to use dscl.

  • Gravatar icon Jamie Orchard-Hays

    Geoffrey, thanks for the helpful write-up. I have done this setup on my Mac (Leopard) and find that the first virtual host always wins—pointing at the second one always goes to the first. I’m wondering if anyone else has had this problem. My /etc/hosts file looks like:

    127.0.0.1 localhost app.local app.admin 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost

    but it doesn’t matter what I change the names to. First one always wins.

    Jamie

  • Gravatar icon Jamie Orchard-Hays

    oops, I was missing

    NameVirtualHost *:80

  • I also recommend using monit for watching your apache process. I use monit and was very satisfied.

  • Just as a follow-up I have a post about setting it up with the debugger http://duckpunching.com/?p=17.

    Good work Geoffrey.

Your Comment

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Manufactured with

Subscribe

Subscribe (RSS)