Mongrel init script

27/06/2008
#!/usr/bin/env ruby
 
app_dir = '/srv/www'
 
apps = {
  'ladybird' => 8001
}
 
if ['stop', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Stopping #{path}..."
    `mongrel_rails stop -c #{path}/current -P log/mongrel.pid`
  end
end
 
if ['start', 'restart'].include? ARGV.first
  apps.each do |path, port|
    path = File.join app_dir, path
    puts "Starting #{path} on #{port}..."
    `mongrel_rails start -d -p #{port} -e production -c #{path}/current -P log/mongrel.pid`
  end
end
 
unless ['start', 'stop', 'restart'].include? ARGV.first
    puts "Usage: mongrel {start|stop|restart}"
    exit
end
No Comments

Rails with Mongrel on Plesk

18/12/2006

I set up Mongrel on a server using Plesk 7. Each Rails app has a single Mongrel webserver1 running on the local interface (port 8001 and up). The Mongrel webservers are not accessible to the outside world. They are proxied through Apache using mod_proxy. Just add this snippet to the vhost.conf for the appropriate domain:

ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://0.0.0.0:8001/
ProxyPassReverse / http://0.0.0.0:8001/

Replace 8001 with the chosen port for your app. (The block does not need to be inside a VirtualHost or Directory container.) Remember to rebuild the Plesk configs afterwards with:

/usr/local/psa/admin/sbin/websrvmng -a -v

You’ll now need to add a Mongrel webserver for the app in /etc/init.d/mongrel:

apps = {
  'firstapp'=> 8001,
  'secondapp' => 8002
}

Enter the app name and your chosen port number into the ‘apps’ hash, and save the file.

Deployment with Capistrano

You’ll now need to get your Rails app ready to deploy using Capistrano. Open a shell and navigate to a working copy of your app. Then run:

capify .

This will generate some Capistrano files in your working copy folder, including a generic deployment recipe (deploy.rb). We need to customise deploy.rb to work with our setup. If the SVN repository isn’t accessible from the live server, you’ll also need to install a Capistrano module to enable use of a local Subversion repository – download from here:

Capistrano module for local SVN access

The zip file contains two .rb files that you’ll need to put into your app’s lib/tasks directory.

Make sure you add and check in all of the Capistrano files to Subversion when you’re done.

Now we’re ready to deploy the app for the first time. In your working copy directory, do:

cap deploy:setup
cap deploy

This will export the latest copy of your project from Subversion, upload it to the server, and start Mongrel.

To update the version of your app on the server, use:

cap deploy

If you have any problems with your release, you can easily roll back to the previous version with:

cap deploy:rollback

Importing and updating your database

To import your schema for the first time, upload your db/schema.rb if you haven’t already. Then, on the live server, do:

RAILS_ENV=production rake db_schema_import

The RAILS_ENV=production part ensures that the production database details are used by rake.

If your database is already on the live server and you need to run migrations against it, you can do:

RAILS_ENV=production rake db:migrate

Or from kermit:

cap deploy:migrations

How apps are stored

It is not essential to know this, because this is all done for you by Capistrano!

All Rails applications live in the /u/apps/ directory.

Every time you do a ‘cap deploy’, a new dated folder is created in the /u/apps/APPNAME/releases folder. For example:

# ls -l /u/apps/firstapp/releases/
drwxr-xr-x  13 root root 4096 Dec 18 14:48 20061218144553
drwxr-xr-x  13 root root 4096 Dec 18 15:23 20061218152030
drwxr-xr-x  13 root root 4096 Dec 18 15:54 20061218155131

The most recent release is symlinked to /u/apps/APPNAME/current. This is where Mongrel serves the application from.

Some files (including log files) are shared between releases, so you don’t lose them when you deploy a new version. These live in the /u/apps/APPNAME/shared folder.

If you’re concerned about the releases folder getting too full, you can delete all but the most recent 5 releases with:

cap deploy:cleanup

Footnotes

1. I had intended to set up Rails using a Mongrel cluster but you need Apache 2.1 or higher to use mod_proxy_balancer.

2. Plesk password protection will not work. All requests to the domain are proxied to Mongrel, so Apache auth is ignored.

Links

No Comments