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

« Newer Snippets
Older Snippets »
Showing 21-22 of 22 total

Lighttpd Rails Script v2

Small adjustments to the original:

- respect the app-name argument
- support taking min / max processes on the command line

Script:

   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     :max_procs   => 3,
  13     :min_procs   => 1,
  14  }
  15  
  16  ARGV.options do |opts|
  17     script_name = File.basename($0)
  18     opts.banner = "Usage: ruby #{script_name} [options]"
  19  
  20     opts.separator ""
  21  
  22     opts.on("-p", "--port=port", Integer,
  23             "Runs Rails on the specified port.",
  24             "Default: 3000") { |OPTIONS[:port]| }
  25     opts.on("-b", "--binding=ip", String,
  26             "Binds Rails to the specified ip.",
  27             "Default: 0.0.0.0") { |OPTIONS[:ip]| }
  28     opts.on("-e", "--environment=name", String,
  29             "Specifies the environment to run this server under (test/development/production).",
  30             "Default: development") { |OPTIONS[:environment]| }
  31     opts.on("-a", "--app-name=name", String,
  32             "Specifies the application name.",
  33             "Default: my_app") { |OPTIONS[:app_name]| }
  34     opts.on("-d", "--daemon",
  35             "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
  36             ) { OPTIONS[:daemon] = true }
  37     opts.on("-n", "--min-procs=number", Integer,
  38             "Minimum number of FastCGI processes allowed.",
  39             "Default: 1") { |OPTIONS[:min_procs]| }
  40     opts.on("-m", "--max-procs=number", Integer,
  41             "Maximum number of FastCGI processes allowed.",
  42             "Default: 3") { |OPTIONS[:max_procs]| }
  43  
  44     opts.separator ""
  45  
  46     opts.on("-h", "--help",
  47             "Show this help message.") { puts opts; exit }
  48  
  49     opts.parse!
  50  
  51  end
  52  
  53  ENV["RAILS_ENV"] = OPTIONS[:environment]
  54  RAILS_ROOT = File.dirname(__FILE__) + "/../"
  55  LIGHTTPD_CONF_FILE = "/tmp/#{OPTIONS[:app_name]}_lighttpd.conf"
  56  
  57  File.open("#{RAILS_ROOT}config/lighttpd.conf", "r") do |file|
  58           conf = file.read
  59     conf.gsub!(/PORT/, OPTIONS[:port].to_s)
  60     conf.gsub!(/BINDING/, OPTIONS[:ip])
  61     conf.gsub!(/RAILS_ROOT/, File.expand_path(RAILS_ROOT))
  62     conf.gsub!(/APP_NAME/, OPTIONS[:app_name])
  63     conf.gsub!(/MIN_PROCS/, OPTIONS[:min_procs].to_s)
  64     conf.gsub!(/MAX_PROCS/, OPTIONS[:max_procs].to_s)
  65     File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
  66  
  67  end
  68  
  69  CMD = "lighttpd -f #{LIGHTTPD_CONF_FILE}"
  70  CMD << " -D" unless OPTIONS[:daemon]
  71  
  72  puts CMD
  73  `#{CMD}`


Conf:

   1  
   2  server.port                = PORT
   3  server.bind                = "BINDING"
   4  server.pid-file             = "/tmp/APP_NAME_lighttpd.pid"
   5  
   6  #server.event-handler = "freebsd-kqueue"
   7  
   8  server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
   9  server.document-root        = "RAILS_ROOT/public/"
  10  server.indexfiles           = ( "dispatch.fcgi", "index.html" )
  11  accesslog.filename          = "RAILS_ROOT/log/lighttpd_access.log"
  12  server.errorlog             = "RAILS_ROOT/log/lighttpd_error.log"
  13  server.error-handler-404 = "/dispatch.fcgi"
  14  
  15  #### fastcgi module
  16  
  17  ## read fastcgi.txt for more info
  18  
  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" => MIN_PROCS,
  25         "max_procs" => MAX_PROCS
  26       )
  27     )
  28  )
  29  
  30  
  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    )

Lighttpd Rails Script

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   )
« Newer Snippets
Older Snippets »
Showing 21-22 of 22 total