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

04.10.2012
| 13900 views |
  • submit to reddit
        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:

#!/usr/local/bin/ruby

require 'optparse'

OPTIONS = {
  :port        => 8000,
  :ip          => "0.0.0.0",
  :daemon      => false,
  :environment => "development",
  :app_name    => "myapp",
}

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: 3000") { |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: rails_app") { |OPTIONS[:environment]| }
  opts.on("-d", "--daemon",
          "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
          ) { OPTIONS[:daemon] = true }

  opts.separator ""

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

  opts.parse!
end

ENV["RAILS_ENV"] = OPTIONS[:environment]
RAILS_ROOT = File.dirname(__FILE__) + "/../"
LIGHTTPD_CONF_FILE = "/tmp/#{OPTIONS[:app_name]}_lighttpd.conf"

File.open("#{RAILS_ROOT}config/lighttpd.conf", "r") do |file|
        conf = file.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])
  File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
end

CMD = "lighttpd -f #{LIGHTTPD_CONF_FILE}" 
CMD << " -D" unless OPTIONS[:daemon]
puts CMD
`#{CMD}`

And this is ./config/lighttpd.conf

server.port                = PORT
server.bind                = "BINDING"

server.pid-file             = "/tmp/APP_NAME_lighttpd.pid"
#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           = ( "dispatch.fcgi", "index.html" )
accesslog.filename          = "RAILS_ROOT/log/lighttpd_access.log"
server.errorlog             = "RAILS_ROOT/log/lighttpd_error.log"
server.error-handler-404 = "/dispatch.fcgi"


#### fastcgi module
## read fastcgi.txt for more info
fastcgi.server =  (
  ".fcgi" => (
    "APP_NAME" => ( 
      "socket" => "/tmp/APP_NAME1.socket",
      "bin-path" => "RAILS_ROOT/public/dispatch.fcgi",
      "min-procs" => 1,
      "max_procs" => 2
    )
  )
)

# mimetype mapping
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"
 )
    

Comments

Bodyguard Security replied on Wed, 2014/01/08 - 3:06pm

this code is really useful for me my semester project based on this thanks for sharing it really help me to make my project. security companies in london

Snippets Manager replied on Tue, 2008/04/29 - 2:33pm

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: sudo port install lighttpd 2. Set initial configs ( I changed the port and doument root): sudo vi /opt/local/etc/lighttpd/lighttpd.conf +"r /opt/local/etc/lighttpd/lighttpd.conf.default" 3. I needed to create two new directories: sudo mkdir /opt/local/var/run sudo mkdir /var/log/lighttpd 4. Installed into launchd sudo launchctl load -w /Library/LaunchDaemons/org.macports.lighttpd.plist 5. Started up script/server glinda:~ kent$ cd dev/rails/expenses/ glinda:expenses kent$ ruby -v ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0] glinda:expenses kent$ rails -v Rails 2.0.2 glinda:expenses kent$ ruby script/server lighttpd => Booting lighttpd (use 'script/server webrick' to force WEBrick) => /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 => Rails application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server (see config/lighttpd.conf for options)

Snippets Manager replied on Mon, 2012/05/07 - 2:11pm

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)?

Snippets Manager replied on Fri, 2008/04/04 - 1:41pm

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.

Snippets Manager replied on Thu, 2007/10/18 - 7:14pm

Worked for me when changed the first line From: #!/usr/local/bin/ruby To: #!/usr/bin/env ruby