Project

General

Profile

Howto Rails » History » Revision 3

Revision 2 (stbuehler, 2009-11-12 21:49) → Revision 3/11 (stbuehler, 2009-11-12 22:36)

h1. Howto Rails 

 {{>toc}} 

 h2. lighttpd config 

 <pre> 
 setup { 
     module_load ( "mod_expire", "mod_fastcgi", "mod_vhost" ); 
 } 

 # looks for cached pages (with '.html' appended to the url), but only if the file for 
 # the original url doesn't exists and doesn't end with '.html' (in the first case we already have a file, 
 # in the second case we don't want to append another '.html' - if a file exists for the url we will still find it) 
 # 
 # remove it if your rails app doesn't cache files, or modify it to look somewhere else (depending on your rails application). 
 # see below for the lua script. 
 # you can also use "lua.handler" from "mod_lua" instead of "include_lua" if you need high performance with multiple workers. 
 cached_html { 
	 if !phys.is_file { 
		 if phys.path !$ ".html" { 
			 include_lua "/etc/lighttpd-sandbox/cached_html.lua"; 
		 } 
	 } 
 } 

 var.vhosts = []; 

 # ... 

 var.vhosts = var.vhosts + [ 
	 "www.lighttpd.net": ${ 
		 docroot "/var/www/servers/www.lighttpd.net/mephisto/public/"; 
		 index ("index.html"); 
		 cached_html; 

		 # deliver static files directly, forward everything else to the rails application 
		 if physical.is_file { 
			 header.add ("X-cleanurl", "hit"); 
		 } else { 
			 header.add ("X-cleanurl", "miss"); 
			 fastcgi "unix:/var/run/lighttpd/sockets/www.lighttpd.net.sock"; 
		 } 

		 if req.path =~ "\.(png|css|gif)$" { 
			 expire "access 1 week"; 
		 } 
	 } 
 ]; 

 # ... 

 vhost.map var.vhosts; 
 </pre> 

 h3. lua script 

 <pre> 
 -- /etc/lighttpd-sandbox/cached_html.lua 
 function try_cached_html(vr) 
	 local p = vr.phys.path .. '.html' 
	 st, res = vr:stat(p) 
	 if st and st.is_file then 
		 -- found the file 
		 vr.phys.path = p 
	 elseif res == HANDLER_WAIT_FOR_EVENT then 
		 return HANDLER_WAIT_FOR_EVENT 
	 end 
	 -- ignore other errors 
 end 

 actions = try_cached_html 
 </pre> 

 h2. spawn rails application 

 Simple ./run script to spawn rails applications with spawn-fcgi and runit/daemontools: 
 <pre> 
 #!/bin/sh 

 exec 2>&1 

 RAILS_ENV="production" \ 
 LANG=C LC_ALL=C \ 
 exec /usr/bin/spawn-fcgi -n -s /var/run/lighttpd/sockets/www.lighttpd.net.sock -u www-default -U www-data -- /var/www/servers/www.lighttpd.net/mephisto/public/dispatch.fcgi 
 </pre> 

 h3. dispatch.fcgi 

 Obviously you need a dispatch.fcgi FastCGI handler, if you don't have one, just try this one: 
 <pre> 
 #!/usr/bin/env ruby 

 require File.dirname(__FILE__) + "/../config/environment" 
 require 'fcgi_handler' 

 RailsFCGIHandler.process! 
 </pre>