Here’s my take on how to install a Ruby on Rails app with lighttpd. The app is configured to start on boot with an init.d script that runs the spinner as any system user.

Warning added 09 Jan. 2008. This info is a bit dated. I think fastcgi has fallen from grace in favor of http with mongrel or one of it’s derivatives. I currently use apache2 and mod-proxy with mongrel clusters.

This article assumes your Ruby on Rails app works great with script/server and that you have a working lighttpd installation. There are a growing number of tutorials on the web describing the initial installation of Ruby on Rails and lighttpd so we’re not going to cover those.

We will be configuring lighttpd to communicate with your app on some ports, then installing a process control script in /etc/init.d that can initialize and tear down your app. When you’re finished you will be able to sudo /etc/init.d/myapp {start|stop|restart}. Your app will also automatically start on boot and terminate gracefully on shutdown. Let’s assume an application called myapp is to be run by the system user crazyhorse for this example.

Here is a summary of the scripts you should already have as part of your rails installation.

script/process/spawner (starts instances of your app)

script/process/spinner (will try to run spawner periodically in case a process dies)

script/process/reaper (can restart and terminate the app)

You can use the -h option to learn more about these scripts but the defaults are very reasonable. Next we will create a script in /etc/init.d to run the rails process control scripts, then set up filesystem links in the runlevel directories so your app starts and stops automatically on boot and shutdown.

Paste the following code into a file named /etc/init.d/myapp, replacing myapp and crazyhorse as required.


#! /bin/sh

# Start and stop myapp's ruby on rails dispatchers

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

SCRIPTNAME="myapp" 
RAILS_ROOT="/home/crazyhorse/myapp" 
SPAWN_OPTS="-a127.0.0.1 -p8000 -i3 -eproduction" 
USER="crazyhorse" 

case "$1" in
  start)
        su $USER -c $RAILS_ROOT/script/process/spinner -c "$RAILS_ROOT/script/process/spawner $SPAWN_OPTS" 
        ;;
  stop)
        su $USER -c "$RAILS_ROOT/script/process/reaper -a graceful" 
        ;;
  restart)
        su $USER -c "$RAILS_ROOT/script/process/reaper -a restart" 
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

exit 0

Now permit your script to be executed with chmod and use update-rc.d to install the filesystem links.

# sudo chmod 755 /etc/init.d/myapp
# sudo update-rc.d myapp defaults
You should now be able to start and stop your app like any other installed daemon! Try it and make sure it works.

# sudo /etc/init.d/myapp start
# ps uax | grep myapp
# sudo /etc/init.d/myapp restart
# sudo /etc/init.d/myapp stop

Add something like the following to your lighttpd.conf. Notice that lighttpd is instructed to listen for fastcgi on localhost, ports 8000 through 8002. The accesslog directive tells lighttpd to keep a myapp specific accesslog.


$HTTP["host"] =~ "(^|\.)myapp\.com" {
        server.document-root = "/home/crazyhorse/current/public" 
        server.error-handler-404 = "/dispatch.fcgi" 
        server.indexfiles = ("dispatch.fcgi")
        url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
        fastcgi.server += (
                ".fcgi" => (
                        "localhost-8000" => ( "host" => "127.0.0.1", "port" => 8000 ) ,
                        "localhost-8001" => ( "host" => "127.0.0.1", "port" => 8001 ) ,
                        "localhost-8002" => ( "host" => "127.0.0.1", "port" => 8002 ) )
        )
        accesslog.filename = "/home/crazyhorse/lighttpd/access.log" 
}
Check the lighttpd config file for errors.

# lighttpd -t -f /etc/lighttpd/lighttpd.conf
Create the lighttpd log file directory and give ownership to the web daemon user so it has write access.

# mkdir /home/crazyhorse/lighttpd
# sudo chown www-data:www-data /home/crazyhorse/lighttpd
Add the web daemon user to crazyhorse’s group. This will allow read-only access to the web daemon so long as appropriate group permissions are set in the /home/crazyhorse/myapp/public directory. It is important that the web daemon (in this case www-data) be allowed to read the public dir for page caching, css, javascript, error pages, and other assets to work.

# sudo gpasswd -a www-data crazyhorse
# groups www-data
Restart lighttpd and then start the rails app.

# sudo /etc/init.d/lighttpd restart
# sudo /etc/init.d/myapp start
Make sure port 80 is still up.

# netstat -tulpn | grep :80

You should now be up and running.

* Update 5/1/2007 * Rails will start with an http, rather than fcgi, interface via mongrel if mongrel is installed. To use the fcgi interface with mongrel installed you can make the SPAWN_OPTS line in the init.d script look like this.

SPAWN_OPTS="fcgi -a127.0.0.1 -p8000 -i3 -eproduction" 

5 Responses Follows

  1. Scott Zetlan says

    Probably this is a version thing, but there’s no script/process/spinner in my app. I’m on rails 1.2.3. So I had to change: su $USER -c $RAILS_ROOT/script/process/spinner -c ”$RAILS_ROOT/script/process/spawner $SPAWN_OPTS”

    to

    su $USER -c "$RAILS_ROOT/script/process/spawner $SPAWN_OPTS"

    and then it worked fine. There’s also a script/process/inspector, so I suspect I could add:

    status)
        su $USER -c "$RAILS_ROOT/script/process/inspector" 
        ;;

    To add a status command.

  2. Sam says

    This is old so yes, it is a version thing. I’ll add a warning or two. Thank you.

  3. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.”Three ages beats by dr dre back, Jimmy Lovine and Dr. Dre ended up talking about the calendar new music chaos and what went wrong. Though technological know-how bogus audio additional obtainable, the complete had suffered. Bodies were not viewers it the way in which artists supposed. For two bodies so amative about audio, this was inexcusable. So they boarded on the opportunity to achieve bigger full and alarm calendar music ashamed within the adapted course. monster beats by dre ferrari commenced with Headphones. They captivated up with Monster artisan Noel Lee who accumulated the previously mentioned vision, and collaborated with him to reinvent headphones for that calendar age. Beats was born. Songs was reborn.” States Beats by Dr. Dre.

  4. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.Individuals you should not accept to anniversary with the audio. Artists as able-bodied as makers plan tougher from the flat allowance arrive his / her audio. Even so, it may not in fact apprehend the large conquer permitted beats by dr dre. Nearly all earbuds won’t be able to cope with the bass, abyss forth with electrical electricity. Tha acrid fact, the audio won’t advance you. Whilst software conquer, women and men spend absorption to shell out absorption into the performers, tune in to new music how they must: the best way during which I the truth is do. Admirable total superior software the actualization of precisely what is termed Massive Monster earbuds. You could biking to a admirable audio central audition headsets DRE. You’ll be take apprenticed that it’s admirable superior of audio. We’re able to accommodate you want the suitable Monster headphone . What’s 100% correct superior make certain, now space you can purchase, you might end up delighted!monster bound egold.

  5. beats by dr dre says

    Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.Despite weighting in at abutting to 270 grams they can be in reality enough to put on even for attached periods and relocating about beats by dr dre. The earcups are formulated gaining adequate and in fact enclose your aeriform to lock out ambient noise. Their alive emphasis abatement is in reality able in blocking out abhorrent appears if audience for your songs, and by flicking merely a little improve with regards to the headphones on their own you are able to all on the brusque purchase to what it annex on abutting to you. a allotment with the best noice cancelling beats by dr dre i purchase in fact tried. That is to your goal which they get frequent affluence of a affluence of abounding many years into this handle company. owning outlined the achievement that accustomed men such as you and me obtain to achieve our way up the ladder. You charge to accomplish a casting for by your cocky by ancestry lot’s of beats and alive with artists.Till afresh you truly will purchase to realize for bartering your arrangement beats headphones in the 20 greenback assortment. Hey you cost to appear out some the location.


Your Response