Back on Typo 6.0.3 and Rails 3.0!

Posted by Huy Dinh Tue, 15 Feb 2011 01:32:00 GMT

Feb 15

When I found out the hard way that mongrel's daemonized mode doesn't really work with Rails 3, I had to find some other feasible way to deploy Rails applications. I did end up trying mod_rails and Apache 2.2, but its process-based nature wasn't working well on the virtual server it was supposed to run on, because of the restricted number of processes at the same time. Since there are other services running on the virtual server, this setup ended up getting very close to the limit. Which obviously is bad.

So there was my excuse to just keep lighttpd running. The more difficult part now was to find a way to actually run Rails. I ended up sticking with Thin. Both lighttpd and Thin are horribly easy to set up on a user without root privileges. (Getting the Debian package of Apache 2.2 to run on non-root takes quite some time, compared to this.)

The easiest way to install Thin probably is gem install thin. Depending on the operating system you are using, installing lighttpd can be either a plain aptitude install lighttpd or some more tedious compiling.

#!/bin/sh

case "$1" in
    devel.huydinh.eu)
        THINPROJECT=devel.huydinh.eu
        THINPORT=3000
        THINENV=development
        ;;
    huydinh.eu)
        THINPROJECT=huydinh.eu
        THINPORT=8081
        THINENV=production
        ;;
    *)
        echo "Please choose an existing project."
        exit 2
        ;;
esac

THINDIR=$HOME/doc/$THINPROJECT
THINPID=$HOME/var/run/thin.$THINPORT.pid
THINLOG=$HOME/var/log/thin/$THINPROJECT.log

DAEMON="$HOME/lib/ruby/gems/1.8/bin/thin"
DAEMON_ARGS="-a 127.0.0.1 -p $THINPORT -c $THINDIR -e $THINENV -d -l $THINLOG -P $THINPID --tag $THINPROJECT"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

case "$2" in
    start)
        $DAEMON $DAEMON_ARGS $2 && echo "Thin started and daemonized."
        ;;
    force-reload|status|stop)
        $DAEMON $DAEMON_ARGS $2
        ;;
    restart)
        $0 $1 stop
        $0 $1 start
        ;;
    *)
        echo "Usage: thinctl PROJECT {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac

This wraps starting and stopping Thin instances (I should rewrite this to use configuration files for Thin) and all there is left to do is to set up lighttpd as a reverse HTTP proxy. Which is about just as easy:

$HTTP["host"] =~ "^devel\.huydinh\.eu$" {
    proxy.server = ( "" =>
        (
            ( "host" => "127.0.0.1", "port" => "3000" ),
        ),
    )
}

$HTTP["host"] =~ "^www\.huydinh\.eu$" {
    url.redirect = (
        "^/(.*)" => "https://huydinh.eu/$1"
    )
}

$HTTP["host"] =~ "^huydinh\.eu$" {
    proxy.server  = ( "" =>
        (
            ( "host" => "127.0.0.1", "port" => "8081" ),
        ),
    )
}

There is some lighttpd spell that would let lighttpd handle static files in /javascripts, /images etc., but it seems to break Typo. That's why it's not included.

Setting up both lighttpd and Thin from scratch probably took me less time than getting Apache 2.2 run on the non-root user. That's before setting up mod_rails...

I wasn't all too certain about this setup when running in development mode (on devel.huydinh.eu), because it felt pretty slow, even for RAILS_ENV=development. The second I tested the production version however, I was happy.

So after a looong time: Typo 6.x and Rails 3.x!

As for the old content, I do not really intend to bring them back as a whole. But I think I will revamp some of the more interesting posts and put them back up here as time permits.

Posted in  | Tags , , , , , , , ,  | no comments | no trackbacks