NSLU2-Linux
view · edit · print · history
  1. Install lighttpd with the cgi module
  2. Install ruby
  3. In the document root directory (in my case /www/pages) create a cgi-bin directory
  4. 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

  1. Make this script executable:
    chmod uag+rx erb.cgi
  2. Change /etc/lighttpd.conf - uncomment the line to load the CGI module
    ...
    server.modules = (
    ...
    "mod_cgi", ...
    )
  3. Add "index.rhtml" as one of the default documents
    index-file.names = ( "index.rhtml", ...
  4. Add ".rhtml" to the static file exclusions
    static-file.exclude-extensions = ( ".rhtml", ".php", ".pl", ".fcgi" )
  5. Add the cgi handler for .rhtml
    cgi.assign = ( ".rhtml" => "/www/pages/cgi-bin/erb.cgi" )
  6. Save the /etc/lighttpd.conf file and restart the server
    /etc/init.d/lighttpd restart
  7. 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>
  8. Load it in a web browser:
    wget http://localhost/

You can now write your web app in Ruby using Erb templates!

view · edit · print · history · Last edited by toby.
Originally by toby.
Page last modified on June 21, 2008, at 04:34 PM