- Install lighttpd with the cgi module
- Install ruby
- In the document root directory (in my case /www/pages) create a cgi-bin directory
- Create the following file in this cgi-bin directory called erb.cgi:
#!/usr/bin/ruby
# erb.cgi
# Apache script handler for .rhtml files
# based on work by Brian Bugh and Paul McArdle?
# see http://dekstop.de/weblog/2006/01/rhtml_on_osx_with_apache_and_erb/(approve sites)
require 'time'
require 'erb'
time = Time.now.httpdate
HEADERS = <<EOF
Date: #{ time }
Server: #{ ENV['SERVER_SOFTWARE'] }
Last-Modified: #{ time }
Content-Type: text/html
EOF
begin
erb = File.open(ENV["SCRIPT_FILENAME"]) { |f| ERB.new(f.read) }
print HEADERS + erb.result(binding)
rescue Exception
print "Content-Type: text/html\n\n"
# error message
print "<h1>Script Error</h1>"
print "<pre>#{ $! }</pre>"
# debug info
print "<h2>Backtrace</h2>"
print "<pre>#{$!.backtrace.join("\n")}</pre>"
print "<h2>Environment</h2>"
print "<pre>#{ENV.keys.map { |key| key + ' = ' + ENV[key] + "\n"} }</pre>"
print "<hr>"
print "<i>#{__FILE__} -- #{time}</i>"
end
- Make this script executable:
chmod uag+rx erb.cgi
- Change /etc/lighttpd.conf - uncomment the line to load the CGI module
...
server.modules = (
...
"mod_cgi", ...
)
- Add "index.rhtml" as one of the default documents
index-file.names = ( "index.rhtml", ...
- Add ".rhtml" to the static file exclusions
static-file.exclude-extensions = ( ".rhtml", ".php", ".pl", ".fcgi" )
- Add the cgi handler for .rhtml
cgi.assign = ( ".rhtml" => "/www/pages/cgi-bin/erb.cgi" )
- Save the /etc/lighttpd.conf file and restart the server
/etc/init.d/lighttpd restart
- Create a test web page in /www/pages/index.rhtml
<html>
<body>
Hello World! The time is now <%=Time.now%>!
Contents of directory:
<pre>
<%=`ls -la`%>
</pre>
</body>
</html>
- Load it in a web browser:
wget http://localhost/
You can now write your web app in Ruby using Erb templates!