lighttpd forum lighty > Rails + lighttpd 1.5 + FastCGI + Spawn-fcgi Setup

Posted by Helmut Buchschmid
on 09.03.2007 17:35
Hi!
How to setup Ruby on Rails 1.2.2 with Lightttpd 1.5, FastCGI and
spawn-fcgi to get the fastest response with static and processed files?
Posted by veto (Guest)
on 10.03.2007 09:57
I found a way by googeling around.
To make that post not a dead end i will explain my steps here.

I use:
Xubuntu/Debian 2.6.17-11-generic i686 GNU/Linux
Lighttpd 1.5
Ruby 1.8.5
Rails 1.2.2
libfcgi0 Shared library of FastCGI
libfcgi-dev Header files of FastCGI
gem fcgi



1. copy the lighttpd.conf file to  /etc/lighttpd/lighttpd.conf
    (to have nicely all our configuration files under  etc)

2. activate the server modules in that file lighttpd.conf by deleting 
the # in front
#---------------------------------------------------------------------------------------------
#example:
.server.modules              = (
                                "mod_rewrite",
#                               "mod_redirect",
#                               "mod_alias",
                                "mod_access",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
                               "mod_proxy_core",
                               "mod_proxy_backend_http",
                               "mod_proxy_backend_fastcgi",
#                               "mod_proxy_backend_scgi",
#-------------------------------------------------------------------------------------------------



3. paste the rewrite rules to the lighttpd.conf file.
    (by rewriting the url request to dispatch.fcgi we are "marking the 
request" as a dynamic contents)

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#example:
   url.rewrite = (
          "^/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9%]+)\??([\-_a-zA-Z0-9=&%]*)$"
 =>
            "/dispatch.fcgi?controller=$1&action=$2&id=$3&$4",
               "^/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9]+)/?\??([\-_a-zA-Z0-9=&%]*)$"
 =>
                 "/dispatch.fcgi?controller=$1&action=$2&$3",
                   "^/([\-_a-zA-Z0-9]+)/?\??([\-_a-zA-Z0-9=&%]*)$" =>
 "/dispatch.fcgi?controller=$1&action=index&$2"
         )
#---------------------------------------------------------------------------------------------------------------------------------------------------------





4. Paste a  $HTTP["host"] "Rule"
    (the lightthtd server will than know where to look for a request 
with a specific domainname, in this case he looks to
     /home/veto/Awww/test1/public)
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#example:
  $HTTP["host"] == "test1.com" {
  server.document-root           = "/home/veto/Awww/test1/public"
  }
#---------------------------------------------------------------------------------------------------------------------------------------------------------





 5. Paste a $HTTP["url"] "Rule"
     (the lighttpd server will forward than any  request with a 
extensions of .fcgi to  a  socket("our Information exchange meeting 
place". And in this case it will be in the  tmp folder of our linux 
distro. )

#---------------------------------------------------------------------------------------------------------------------------------------------------------
#example:
$HTTP["url"] =~ "\.fcgi$" {
        proxy-core.balancer             = "static"
        proxy-core.allow-x-sendfile     = "enable"
        proxy-core.check-local          = "enable"
        proxy-core.protocol             = "fastcgi"
        proxy-core.backends             = ( "unix:/tmp/ap1-0.socket"
)
        proxy-core.max-pool-size        = 16
}

#---------------------------------------------------------------------------------------------------------------------------------------------------------



 ( you see after rewrite/marking the request now we  forward it to the 
socket. On this socket another Process  the fcgi will    retrieve the 
request)




  6. To start the lighttpd server we can simple execute the folllowing 
command in the terminal:
     veto@beast15:~$:        sudo lighttpd -f 
/etc/lighttpd/lighttpd.conf

      we can also add a init script to /etc/init.d/ and
      witht the command:

     veto@beast15:~$:        sudo update-rc.d lighttpd defaults

     the init script start up when the pc boots

     also  can than controll the lighttpd server process by executing
     /etc/init.d/lighttpd start
     /etc/init.d/lighttpd start
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#example init script copy to  /etc/init.d/lighttpd:

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/lighttpd
NAME=lighttpd
DESC="web server"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

DAEMON_OPTS="-f /etc/lighttpd/lighttpd.conf"

test -x $DAEMON || exit 0

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" $NAME
    if ! start-stop-daemon --start --quiet\
    --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
            log_end_msg 1
    else
            log_end_msg 0
    fi
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" $NAME
    if start-stop-daemon --quiet --stop --oknodo --retry 30\
    --pidfile $PIDFILE --exec $DAEMON; then
        rm -f $PIDFILE
        log_end_msg 0
    else
        log_end_msg 1
    fi
    ;;
  reload)
    log_daemon_msg "Reloading $DESC configuration" $NAME
    if start-stop-daemon --stop --signal 2 --oknodo --retry 30\
    --quiet --pidfile $PIDFILE --exec $DAEMON; then
        if start-stop-daemon --start --quiet  \
        --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
        log_end_msg 0
        else
        log_end_msg 1
        fi
    else
        log_end_msg 1
    fi
  ;;
  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" 
>&2
    exit 1
    ;;
esac

exit 0
#---------------------------------------------------------------------------------------------------------------------------------------------------------




7. we have to install the fcgi
    in debian you can simple use
   veto@beast15:~$:      sudo  atp-get install libfcgi0
   veto@beast15:~$:      sudo  atp-get install libfcgi-dev

8. Now we have to install the ruby binding to the fcgi
    veto@beast15:~$:  sudo gem install fcgi -y



9. Now we must start the fcgi externally with the spawn-fcgi what will 
be installed with lighttpd 1.5

    veto@beast15:~$:  sudo RAILS_ENV=production spawn-fcgi -f 
/home/veto/Awww/test1/public/dispatch.fcgi    -s /tmp/ap1-0.socket -P 
/tmp/ap1-0.socket

  ( we can also create a init script and put it into the /etc/init.d/ 
folder, so we can control the process with start and stop and  the 
creation and removing of the sockets. mr robin from japan made a nice 
script below what creates more processes and stockets which our 
proxy-core.balancer in lighttpd can use:)

#---------------------------------------------------------------------------------------------------------------------------------------------------------
#example init script copy to  /etc/init.d/lighttpd:

#!/bin/sh

    DISPATCH_PATH=/home/veto/Awww/test1/public/dispatch.fcgi
    SOCKET_PATH=/tmp/socket/
    RAILS_ENV=production
    export RAILS_ENV

    case "$1" in

     start)
       rm -rf $SOCKET_PATH/javaeye.socket-*
       for num in 0 1 2 3 4 5 6 7 8 9
       do
        /usr/local/bin/spawn-fcgi -f $DISPATCH_PATH -s 
$SOCKET_PATH/javaeye.socket-$num
       done
       ;;

    stop)
         killall ruby
       # killall -9 dispatch.fcgi
       rm -rf $SOCKET_PATH/javaeye.socket-*
       ;;

     restart)
       $0 stop
       $0 start
       ;;

     *)
       echo "Usage: dispatch.sh {start|stop|restart}"
       ;;

   esac

   exit 0

#---------------------------------------------------------------------------------------------------------------------------------------------------------


10. the spawn-fcgi  command will create a fcgi dispatch process with its 
socket (exchange place) in /tmp/.
      and our lighttpd server can  now forward those dynamic web request 
to that stocket and retrieve their result.



Posted by ixland (Guest)
on 11.04.2007 20:14
This also might be of some help:

(Multiple Rails with Lighttpd 1.5, Apache (mod_proxy) and Plesk 8.1)
http://osx.ixland.com/blog/2007/04/11/multiple-rails-with-lighttpd-15-apache-mod_proxy-and-plesk-81/

Helmut Buchschmid wrote:
> Hi!
> How to setup Ruby on Rails 1.2.2 with Lightttpd 1.5, FastCGI and
> spawn-fcgi to get the fastest response with static and processed files?