Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Lighttpd Rails Script (See related posts)

Adding these two files to your rails project will allow you to start a lighttpd server like you would start webrick. e.g.: './script/lighttpd' or './script/lighttpd -d -e production'

This is ./script/lighttpd:

   1  
   2  #!/usr/local/bin/ruby
   3  
   4  require 'optparse'
   5  
   6  OPTIONS = {
   7    :port        => 8000,
   8    :ip          => "0.0.0.0",
   9    :daemon      => false,
  10    :environment => "development",
  11    :app_name    => "myapp",
  12  }
  13  
  14  ARGV.options do |opts|
  15    script_name = File.basename($0)
  16    opts.banner = "Usage: ruby #{script_name} [options]"
  17  
  18    opts.separator ""
  19  
  20    opts.on("-p", "--port=port", Integer,
  21            "Runs Rails on the specified port.",
  22            "Default: 3000") { |OPTIONS[:port]| }
  23    opts.on("-b", "--binding=ip", String,
  24            "Binds Rails to the specified ip.",
  25            "Default: 0.0.0.0") { |OPTIONS[:ip]| }
  26    opts.on("-e", "--environment=name", String,
  27            "Specifies the environment to run this server under (test/development/production).",
  28            "Default: development") { |OPTIONS[:environment]| }
  29    opts.on("-a", "--app-name=name", String,
  30            "Specifies the application name.",
  31            "Default: rails_app") { |OPTIONS[:environment]| }
  32    opts.on("-d", "--daemon",
  33            "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
  34            ) { OPTIONS[:daemon] = true }
  35  
  36    opts.separator ""
  37  
  38    opts.on("-h", "--help",
  39            "Show this help message.") { puts opts; exit }
  40  
  41    opts.parse!
  42  end
  43  
  44  ENV["RAILS_ENV"] = OPTIONS[:environment]
  45  RAILS_ROOT = File.dirname(__FILE__) + "/../"
  46  LIGHTTPD_CONF_FILE = "/tmp/#{OPTIONS[:app_name]}_lighttpd.conf"
  47  
  48  File.open("#{RAILS_ROOT}config/lighttpd.conf", "r") do |file|
  49          conf = file.read
  50    conf.gsub!(/PORT/, OPTIONS[:port].to_s)
  51    conf.gsub!(/BINDING/, OPTIONS[:ip])
  52    conf.gsub!(/RAILS_ROOT/, File.expand_path(RAILS_ROOT))
  53    conf.gsub!(/APP_NAME/, OPTIONS[:app_name])
  54    File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
  55  end
  56  
  57  CMD = "lighttpd -f #{LIGHTTPD_CONF_FILE}" 
  58  CMD << " -D" unless OPTIONS[:daemon]
  59  puts CMD
  60  `#{CMD}`


And this is ./config/lighttpd.conf

   1  
   2  server.port                = PORT
   3  server.bind                = "BINDING"
   4  
   5  server.pid-file             = "/tmp/APP_NAME_lighttpd.pid"
   6  #server.event-handler = "freebsd-kqueue" 
   7  
   8  server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
   9  
  10  server.document-root        = "RAILS_ROOT/public/"
  11  server.indexfiles           = ( "dispatch.fcgi", "index.html" )
  12  accesslog.filename          = "RAILS_ROOT/log/lighttpd_access.log"
  13  server.errorlog             = "RAILS_ROOT/log/lighttpd_error.log"
  14  server.error-handler-404 = "/dispatch.fcgi"
  15  
  16  
  17  #### fastcgi module
  18  ## read fastcgi.txt for more info
  19  fastcgi.server =  (
  20    ".fcgi" => (
  21      "APP_NAME" => ( 
  22        "socket" => "/tmp/APP_NAME1.socket",
  23        "bin-path" => "RAILS_ROOT/public/dispatch.fcgi",
  24        "min-procs" => 1,
  25        "max_procs" => 2
  26      )
  27    )
  28  )
  29  
  30  # mimetype mapping
  31  mimetype.assign             = (
  32    ".rpm"          =>      "application/x-rpm",
  33    ".pdf"          =>      "application/pdf",
  34    ".sig"          =>      "application/pgp-signature",
  35    ".spl"          =>      "application/futuresplash",
  36    ".class"        =>      "application/octet-stream",
  37    ".ps"           =>      "application/postscript",
  38    ".torrent"      =>      "application/x-bittorrent",
  39    ".dvi"          =>      "application/x-dvi",
  40    ".gz"           =>      "application/x-gzip",
  41    ".pac"          =>      "application/x-ns-proxy-autoconfig",
  42    ".swf"          =>      "application/x-shockwave-flash",
  43    ".tar.gz"       =>      "application/x-tgz",
  44    ".tgz"          =>      "application/x-tgz",
  45    ".tar"          =>      "application/x-tar",
  46    ".zip"          =>      "application/zip",
  47    ".mp3"          =>      "audio/mpeg",
  48    ".m3u"          =>      "audio/x-mpegurl",
  49    ".wma"          =>      "audio/x-ms-wma",
  50    ".wax"          =>      "audio/x-ms-wax",
  51    ".ogg"          =>      "audio/x-wav",
  52    ".wav"          =>      "audio/x-wav",
  53    ".gif"          =>      "image/gif",
  54    ".jpg"          =>      "image/jpeg",
  55    ".jpeg"         =>      "image/jpeg",
  56    ".png"          =>      "image/png",
  57    ".xbm"          =>      "image/x-xbitmap",
  58    ".xpm"          =>      "image/x-xpixmap",
  59    ".xwd"          =>      "image/x-xwindowdump",
  60    ".css"          =>      "text/css",
  61    ".html"         =>      "text/html",
  62    ".htm"          =>      "text/html",
  63    ".js"           =>      "text/javascript",
  64    ".asc"          =>      "text/plain",
  65    ".c"            =>      "text/plain",
  66    ".conf"         =>      "text/plain",
  67    ".text"         =>      "text/plain",
  68    ".txt"          =>      "text/plain",
  69    ".dtd"          =>      "text/xml",
  70    ".xml"          =>      "text/xml",
  71    ".mpeg"         =>      "video/mpeg",
  72    ".mpg"          =>      "video/mpeg",
  73    ".mov"          =>      "video/quicktime",
  74    ".qt"           =>      "video/quicktime",
  75    ".avi"          =>      "video/x-msvideo",
  76    ".asf"          =>      "video/x-ms-asf",
  77    ".asx"          =>      "video/x-ms-asf",
  78    ".wmv"          =>      "video/x-ms-wmv",
  79    ".bz2"          =>      "application/x-bzip",
  80    ".tbz"          =>      "application/x-bzip-compressed-tar",
  81    ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
  82   )

Comments on this post

caiomoritz posts on Oct 18, 2007 at 19:16
Worked for me when changed the first line

From:
#!/usr/local/bin/ruby

To:
#!/usr/bin/env ruby
adevstar posts on Apr 04, 2008 at 13:46
to figure out what the shebangs line is for your site like the comment above just enter "which ruby" into a terminal, that will give you the location that you need.
timmorgan posts on Apr 04, 2008 at 22:37
Maybe it's just on my Mac, but I think I can start a lighttpd server with the standard script/server. Can someone verify it's not something funky I've installed (other than Lighttpd)?
bci posts on Apr 29, 2008 at 15:10
After install and config of lighttpd on my Macintosh, the lighttpd.conf was auto-created. Here is what I did today.:

1. Used MacPorts to install lighttpd:
   1  sudo port install lighttpd


2. Set initial configs ( I changed the port and doument root):
   1  sudo vi /opt/local/etc/lighttpd/lighttpd.conf +"r /opt/local/etc/lighttpd/lighttpd.conf.default"


3. I needed to create two new directories:
   1  
   2  sudo mkdir /opt/local/var/run
   3  sudo mkdir /var/log/lighttpd


4. Installed into launchd
   1  sudo launchctl load -w /Library/LaunchDaemons/org.macports.lighttpd.plist


5. Started up script/server
   1  
   2  glinda:~ kent$ cd dev/rails/expenses/
   3  glinda:expenses kent$ ruby -v
   4  ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
   5  glinda:expenses kent$ rails -v
   6  Rails 2.0.2
   7  glinda:expenses kent$ ruby script/server lighttpd
   8  => Booting lighttpd (use 'script/server webrick' to force WEBrick)
   9  => /Users/kent/dev/rails/expenses/config/lighttpd.conf not found, copying from /Library/Ruby/Gems/1.8/gems/rails-2.0.2/configs/lighttpd.conf
  10  => Rails application starting on http://0.0.0.0:3000
  11  => Call with -d to detach
  12  => Ctrl-C to shutdown server (see config/lighttpd.conf for options)



You need to create an account or log in to post comments to this site.


Click here to browse all 5556 code snippets

Related Posts