1
2
3 require 'optparse'
4 require 'fileutils'
5 require 'tmpdir'
6
7 OPTIONS = {
8 :port => 3000,
9 :ip => "0.0.0.0",
10 :daemon => false,
11 :environment => "development",
12 :app_name => Process::pid.to_s,
13 :max_procs => 3,
14 :min_procs => 1,
15 :ssl => false,
16 :pemfile => "server.pem",
17 }
18
19 ARGV.options do |opts|
20 script_name = File.basename($0)
21 opts.banner = "Usage: ruby #{script_name} [options]"
22
23 opts.separator ""
24
25 opts.on("-p", "--port=port", Integer,
26 "Runs Rails on the specified port.",
27 "Default: 8000") { |OPTIONS[:port]| }
28 opts.on("-b", "--binding=ip", String,
29 "Binds Rails to the specified ip.",
30 "Default: 0.0.0.0") { |OPTIONS[:ip]| }
31 opts.on("-e", "--environment=name", String,
32 "Specifies the environment to run this server under (test/development/production).",
33 "Default: development") { |OPTIONS[:environment]| }
34 opts.on("-a", "--app-name=name", String,
35 "Specifies the application name.",
36 "Default: process_id") { |OPTIONS[:app_name]| }
37 opts.on("-d", "--daemon",
38 "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
39 ) { OPTIONS[:daemon] = true }
40 opts.on("-n", "--min-procs=number", Integer,
41 "Minimum number of FastCGI processes allowed.",
42 "Default: 1") { |OPTIONS[:min_procs]| }
43 opts.on("-m", "--max-procs=number", Integer,
44 "Maximum number of FastCGI processes allowed.",
45 "Default: 3") { |OPTIONS[:max_procs]| }
46 opts.on("-l", "--enable-ssl",
47 "Enable SSL."
48 ) { OPTIONS[:ssl] = true }
49 opts.on("-f", "--pemfile=pemfile", String,
50 "path to the PEM file for SSL support."
51 ) { |OPTIONS[:pemfile]| }
52
53 opts.separator ""
54
55 opts.on("-h", "--help",
56 "Show this help message.") { puts opts; exit }
57
58 opts.parse!
59
60 end
61
62 ENV["RAILS_ENV"] = OPTIONS[:environment]
63 RAILS_ROOT = Dir.pwd + "/./"
64 TMP_DIR = Dir.tmpdir
65 LIGHTTPD_CONF_FILE = TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.conf"
66
67 conf = DATA.read
68 conf.gsub!('__PORT__', OPTIONS[:port].to_s)
69 conf.gsub!('__BINDING__', OPTIONS[:ip])
70 conf.gsub!('__RAILS_ROOT__', File.expand_path(RAILS_ROOT))
71 conf.gsub!('__APP_NAME__', OPTIONS[:app_name])
72 conf.gsub!('__MIN_PROCS__', OPTIONS[:min_procs].to_s)
73 conf.gsub!('__MAX_PROCS__', OPTIONS[:max_procs].to_s)
74 conf.gsub!('__RAILS_ENV__', ENV['RAILS_ENV'])
75 conf.gsub!('__TMP_DIR__', TMP_DIR)
76 conf.gsub!('__SSL__', OPTIONS[:ssl] ? "enable" : "disable")
77 conf.gsub!('__PEMFILE__', OPTIONS[:pemfile])
78 File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
79
80 CMD = "/usr/sbin/lighttpd -f #{LIGHTTPD_CONF_FILE}"
81 CMD << " -D" if not OPTIONS[:daemon]
82
83 puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
84 puts "=> Ctrl-C to shutdown server; call with --help for options" if not OPTIONS[:daemon]
85
86 puts CMD
87 `#{CMD}`
88
89 FileUtils.rm Dir.glob(TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.*") if not OPTIONS[:daemon]
90
91 __END__
92 server.port = __PORT__
93 server.bind = "__BINDING__"
94 server.pid-file = "__TMP_DIR__/lighttpd.__APP_NAME__.pid"
95 server.max-keep-alive-requests = 4
96 server.max-keep-alive-idle = 4
97
98 ssl.engine = "__SSL__"
99 ssl.pemfile = "__PEMFILE__"
100
101 #server.event-handler = "freebsd-kqueue"
102
103 server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
104 server.document-root = "__RAILS_ROOT__/public/"
105 server.indexfiles = ( "index.html" ,"dispatch.fcgi")
106 accesslog.filename = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.access.log"
107 server.errorlog = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.error.log"
108 server.error-handler-404 = "/dispatch.fcgi"
109
110 #### fastcgi module
111
112 ## read fastcgi.txt for more info
113 fastcgi.server = (
114 ".fcgi" => (
115 "__APP_NAME__" => (
116 "socket" => "__TMP_DIR__/lighttpd.__APP_NAME__.fcgi.socket",
117 "bin-path" => "__RAILS_ROOT__/public/dispatch.fcgi",
118 "min-procs" => __MIN_PROCS__,
119 "max_procs" => __MAX_PROCS__
120 )
121 )
122 )
123
124
125 mimetype.assign = (
126 ".rpm" => "application/x-rpm",
127 ".pdf" => "application/pdf",
128 ".sig" => "application/pgp-signature",
129 ".spl" => "application/futuresplash",
130 ".class" => "application/octet-stream",
131 ".ps" => "application/postscript",
132 ".torrent" => "application/x-bittorrent",
133 ".dvi" => "application/x-dvi",
134 ".gz" => "application/x-gzip",
135 ".pac" => "application/x-ns-proxy-autoconfig",
136 ".swf" => "application/x-shockwave-flash",
137 ".tar.gz" => "application/x-tgz",
138 ".tgz" => "application/x-tgz",
139 ".tar" => "application/x-tar",
140 ".zip" => "application/zip",
141 ".mp3" => "audio/mpeg",
142 ".m3u" => "audio/x-mpegurl",
143 ".wma" => "audio/x-ms-wma",
144 ".wax" => "audio/x-ms-wax",
145 ".ogg" => "audio/x-wav",
146 ".wav" => "audio/x-wav",
147 ".gif" => "image/gif",
148 ".jpg" => "image/jpeg",
149 ".jpeg" => "image/jpeg",
150 ".png" => "image/png",
151 ".xbm" => "image/x-xbitmap",
152 ".xpm" => "image/x-xpixmap",
153 ".xwd" => "image/x-xwindowdump",
154 ".css" => "text/css",
155 ".html" => "text/html",
156 ".htm" => "text/html",
157 ".js" => "text/javascript",
158 ".asc" => "text/plain",
159 ".c" => "text/plain",
160 ".conf" => "text/plain",
161 ".text" => "text/plain",
162 ".txt" => "text/plain",
163 ".dtd" => "text/xml",
164 ".xml" => "text/xml",
165 ".mpeg" => "video/mpeg",
166 ".mpg" => "video/mpeg",
167 ".mov" => "video/quicktime",
168 ".qt" => "video/quicktime",
169 ".avi" => "video/x-msvideo",
170 ".asf" => "video/x-ms-asf",
171 ".asx" => "video/x-ms-asf",
172 ".wmv" => "video/x-ms-wmv",
173 ".bz2" => "application/x-bzip",
174 ".tbz" => "application/x-bzip-compressed-tar",
175 ".tar.bz2" => "application/x-bzip-compressed-tar"
176 )