DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Lighttpd Rails Script With SSL Options

11.25.2005
| 6449 views |
  • submit to reddit
        
#!/usr/bin/env ruby

require 'optparse'
require 'fileutils'
require 'tmpdir'

OPTIONS = {
  :port        => 3000,
  :ip          => "0.0.0.0",
  :daemon      => false,
  :environment => "development",
  :app_name    => Process::pid.to_s,
  :max_procs   => 3,
  :min_procs   => 1,
  :ssl         => false,
  :pemfile     => "server.pem",
}

ARGV.options do |opts|
  script_name = File.basename($0)
  opts.banner = "Usage: ruby #{script_name} [options]"

  opts.separator ""

  opts.on("-p", "--port=port", Integer,
          "Runs Rails on the specified port.",
          "Default: 8000") { |OPTIONS[:port]| }
  opts.on("-b", "--binding=ip", String,
          "Binds Rails to the specified ip.",
          "Default: 0.0.0.0") { |OPTIONS[:ip]| }
  opts.on("-e", "--environment=name", String,
          "Specifies the environment to run this server under (test/development/production).",
          "Default: development") { |OPTIONS[:environment]| }
  opts.on("-a", "--app-name=name", String,
          "Specifies the application name.",
          "Default: process_id") { |OPTIONS[:app_name]| }
  opts.on("-d", "--daemon",
          "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
          ) { OPTIONS[:daemon] = true }
  opts.on("-n", "--min-procs=number", Integer,
          "Minimum number of FastCGI processes allowed.",
          "Default: 1") { |OPTIONS[:min_procs]| }
  opts.on("-m", "--max-procs=number", Integer,
          "Maximum number of FastCGI processes allowed.",
          "Default: 3") { |OPTIONS[:max_procs]| }
  opts.on("-l", "--enable-ssl",
          "Enable SSL."
          ) { OPTIONS[:ssl] = true }
  opts.on("-f", "--pemfile=pemfile", String,
          "path to the PEM file for SSL support."
          ) { |OPTIONS[:pemfile]| }
  
  opts.separator ""

  opts.on("-h", "--help",
          "Show this help message.") { puts opts; exit }

  opts.parse!

end

ENV["RAILS_ENV"] = OPTIONS[:environment]
RAILS_ROOT = Dir.pwd + "/./"
TMP_DIR = Dir.tmpdir
LIGHTTPD_CONF_FILE = TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.conf"

conf = DATA.read
conf.gsub!('__PORT__', OPTIONS[:port].to_s)
conf.gsub!('__BINDING__', OPTIONS[:ip])
conf.gsub!('__RAILS_ROOT__', File.expand_path(RAILS_ROOT))
conf.gsub!('__APP_NAME__', OPTIONS[:app_name])
conf.gsub!('__MIN_PROCS__', OPTIONS[:min_procs].to_s)
conf.gsub!('__MAX_PROCS__', OPTIONS[:max_procs].to_s)
conf.gsub!('__RAILS_ENV__', ENV['RAILS_ENV'])
conf.gsub!('__TMP_DIR__', TMP_DIR)
conf.gsub!('__SSL__', OPTIONS[:ssl] ? "enable" : "disable")
conf.gsub!('__PEMFILE__', OPTIONS[:pemfile])
File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }

CMD = "/usr/sbin/lighttpd -f #{LIGHTTPD_CONF_FILE}"
CMD << " -D" if not OPTIONS[:daemon]

puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
puts "=> Ctrl-C to shutdown server; call with --help for options" if not OPTIONS[:daemon]

puts CMD
`#{CMD}`

FileUtils.rm Dir.glob(TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.*") if not OPTIONS[:daemon]

__END__
server.port                = __PORT__
server.bind                = "__BINDING__"
server.pid-file             = "__TMP_DIR__/lighttpd.__APP_NAME__.pid"
server.max-keep-alive-requests = 4
server.max-keep-alive-idle = 4

ssl.engine = "__SSL__"
ssl.pemfile = "__PEMFILE__"

#server.event-handler = "freebsd-kqueue"

server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
server.document-root        = "__RAILS_ROOT__/public/"
server.indexfiles           = ( "index.html" ,"dispatch.fcgi")
accesslog.filename          = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.access.log"
server.errorlog             = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.error.log"
server.error-handler-404 = "/dispatch.fcgi"

#### fastcgi module

## read fastcgi.txt for more info
fastcgi.server =  (
                   ".fcgi" => (
                               "__APP_NAME__" => (
                                                  "socket" => "__TMP_DIR__/lighttpd.__APP_NAME__.fcgi.socket",
                                                  "bin-path" => "__RAILS_ROOT__/public/dispatch.fcgi",
                                                  "min-procs" => __MIN_PROCS__,
                                                  "max_procs" => __MAX_PROCS__
                                                  )
                               )
                   )


mimetype.assign             = (
                               ".rpm"          =>      "application/x-rpm",
                               ".pdf"          =>      "application/pdf",
                               ".sig"          =>      "application/pgp-signature",
                               ".spl"          =>      "application/futuresplash",
                               ".class"        =>      "application/octet-stream",
                               ".ps"           =>      "application/postscript",
                               ".torrent"      =>      "application/x-bittorrent",
                               ".dvi"          =>      "application/x-dvi",
                               ".gz"           =>      "application/x-gzip",
                               ".pac"          =>      "application/x-ns-proxy-autoconfig",
                               ".swf"          =>      "application/x-shockwave-flash",
                               ".tar.gz"       =>      "application/x-tgz",
                               ".tgz"          =>      "application/x-tgz",
                               ".tar"          =>      "application/x-tar",
                               ".zip"          =>      "application/zip",
                               ".mp3"          =>      "audio/mpeg",
                               ".m3u"          =>      "audio/x-mpegurl",
                               ".wma"          =>      "audio/x-ms-wma",
                               ".wax"          =>      "audio/x-ms-wax",
                               ".ogg"          =>      "audio/x-wav",
                               ".wav"          =>      "audio/x-wav",
                               ".gif"          =>      "image/gif",
                               ".jpg"          =>      "image/jpeg",
                               ".jpeg"         =>      "image/jpeg",
                               ".png"          =>      "image/png",
                               ".xbm"          =>      "image/x-xbitmap",
                               ".xpm"          =>      "image/x-xpixmap",
                               ".xwd"          =>      "image/x-xwindowdump",
                               ".css"          =>      "text/css",
                               ".html"         =>      "text/html",
                               ".htm"          =>      "text/html",
                               ".js"           =>      "text/javascript",
                               ".asc"          =>      "text/plain",
                               ".c"            =>      "text/plain",
                               ".conf"         =>      "text/plain",
                               ".text"         =>      "text/plain",
                               ".txt"          =>      "text/plain",
                               ".dtd"          =>      "text/xml",
                               ".xml"          =>      "text/xml",
                               ".mpeg"         =>      "video/mpeg",
                               ".mpg"          =>      "video/mpeg",
                               ".mov"          =>      "video/quicktime",
                               ".qt"           =>      "video/quicktime",
                               ".avi"          =>      "video/x-msvideo",
                               ".asf"          =>      "video/x-ms-asf",
                               ".asx"          =>      "video/x-ms-asf",
                               ".wmv"          =>      "video/x-ms-wmv",
                               ".bz2"          =>      "application/x-bzip",
                               ".tbz"          =>      "application/x-bzip-compressed-tar",
                               ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
                               )