# Super simple serving of ruby pages

**URL:** <https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376>\
**Category:** Ruby\
**Created:** [September 20, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376 "2006-09-20T18:01:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/1 "2006-09-20T18:01:07Z")

</div>

Hi. I apologize in advance if this is a dumb question, but though I’ve  
searched online I haven’t found an answer.

I’m starting to teach my 8yr daughter Ruby. We’ve covered HTML and CSS  
already and she handcoded her own static website. I’d like to  
incorporate her Ruby learning experience into her website, which is  
much more interesting for her than using Ruby to write scripts or  
desktop apps. She’s just starting out, so Rails is much too complicated  
for her. She’s not ready for the whole MVC concept yet. What I’d like  
to do is to run very simple Ruby scripts from the site and incorporate  
Ruby code into rhtml files, but without Rails. I don’t really want a  
“framework”, just the ability to run a ruby file that will serve up an  
rhtml file. In other words, something very simple like PHP (I don’t  
want to teach her PHP). [I’ll get into Rails later once she’s more advanced.]

ERb looks like the ticket, but there’s a missing link (for me). I can  
write a ruby script that generates an rhtml file, but then how do I  
serve that rhtml file without a framework like  
Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,  
but I’m missing the connection. I can’t find a simple tutorial anywhere  
online, and I’m no expert on web applications.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/2 "2006-09-20T18:17:09Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby code into rhtml files, but without Rails. I don’t really want a  
> online, and I’m no expert on web applications.
> 
> Thanks for the help.

Webrick has WEBrick::HTTPServlet::ERBHandler, which (I believe) serves  
ERb pages. Mongrel probably has something similar.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/3 "2006-09-20T18:27:10Z")

</div>

zerohalo wrote:

/ …

> ERb looks like the ticket, but there’s a missing link (for me). I can  
> write a ruby script that generates an rhtml file, but then how do I  
> serve that rhtml file without a framework like  
> Rails/Nitro/Camping/etc.? Probably the answer is with Webrick somehow,  
> but I’m missing the connection. I can’t find a simple tutorial anywhere  
> online, and I’m no expert on web applications.

Just one question. Do you want the existing Web server to serve your  
Ruby  
code, or do you require the server to be Ruby-based also?

If the former, you can tell Apache to serve Ruby scripts that contain  
the  
usual CGI conventions. That seems simple enough. And it might not be  
what  
you are asking for.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/4 "2006-09-20T18:41:12Z")

</div>

Paul L. wrote:

> Just one question. Do you want the existing Web server to serve your Ruby  
> code, or do you require the server to be Ruby-based also?

No, the server doesn’t need to be Ruby-based. I could use lighttpd or  
apache to serve the code.

> If the former, you can tell Apache to serve Ruby scripts that contain the  
> usual CGI conventions. That seems simple enough. And it might not be what  
> you are asking for.

That’s the part I’m missing. I find Apache extremely difficult to  
configure (beyond the basics). How would I tell Apache to serve Ruby  
scripts?

Thanks.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/5 "2006-09-20T18:27:10Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ruby code into rhtml files, but without Rails. I don’t really want a  
> online, and I’m no expert on web applications.
> 
> Thanks for the help.

Webrick seems to have support for ERB - see httpservlet/erbhandler.rb,  
that is used for .rhtml files by FileHandler. Just find in the  
tutorials how to start webrick for a given document root  
(Webrick::HttpServer.new({:DocumentRoot = ‘/var/www/’}) and  
something?)

You can use meta\_vars and query variables in your rhtml files.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/6 "2006-09-20T18:53:13Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > usual CGI conventions. That seems simple enough. And it might not be what  
> > you are asking for.
> 
> That’s the part I’m missing. I find Apache extremely difficult to  
> configure (beyond the basics). How would I tell Apache to serve Ruby  
> scripts?

In a recent thread (eruby?) someone mentioned adding

> AddType application/x-httpd-eruby .rhtml  
> Action application/x-httpd-eruby /cgi-bin/eruby  
> to httpd.conf

In my win installation there is c:/ruby/bin/erb.bat so I suppose there  
is something similar in unix. Now either add #!/usr/bin/erb to the top  
of any .cgi file or, preferably, do something similar to the above,  
somehow telling apache to run erb.

(I’m not an apache guru, so these are just hints to get you started…)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/7 "2006-09-20T18:59:14Z")

</div>

Jan S. wrote:

> Webrick seems to have support for ERB - see httpservlet/erbhandler.rb,  
> that is used for .rhtml files by FileHandler. Just find in the  
> tutorials how to start webrick for a given document root  
> (Webrick::HttpServer.new({:DocumentRoot = ‘/var/www/’}) and  
> something?)

Yes, I tried that. Unfortunately there is no documentation on the  
webrick site on how to use ERBHandler. I have this in a ruby script  
which does start up the Webrick server. However, while it serves HTML  
files fine, it treats RHTML files as binaries and RB files as just  
plain text. Here’s my code:

```

require 'webrick'
include WEBrick

def start_webrick(config = {})
  config.update(:Port => 8080)
  server = HTTPServer.new(config)
  yield server if block_given?
  ['INT', 'TERM'].each {|signal|
    trap(signal) {server.shutdown}
  }
  ruby_dir = File.expand_path('/data/sandbox/ruby')
  server.mount("/data/sandbox/ruby", HTTPServlet::ERBHandler, ruby_dir)
  server.start
end

start_webrick(:DocumentRoot => '/data/sandbox/ruby')

```

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/8 "2006-09-20T19:19:19Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes, I tried that. Unfortunately there is no documentation on the  
> webrick site on how to use ERBHandler. I have this in a ruby script  
> which does start up the Webrick server. However, while it serves HTML  
> files fine, it treats RHTML files as binaries and RB files as just  
> plain text. Here’s my code:

require ‘webrick’  
include WEBrick

def start\_webrick(config = {})  
config.update(:Port =\> 8080)

- config.update(:MimeTypes =\> {‘rhtml’ =\> ‘text/html’})  
server = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}

- ruby\_dir = File.expand\_path(‘/data/sandbox/ruby’)
- server.mount(“/data/sandbox/ruby”, HTTPServlet::ERBHandler, ruby\_dir)  
server.start  
end

start\_webrick(:DocumentRoot =\> ‘/data/sandbox/ruby’)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/9 "2006-09-20T19:37:22Z")

</div>

Thanks, Jan. That was easy. Now .rhtml files render correctly.

Is there a way to tell it to execute .rb files rather than treat them  
as simple text files?

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 7:43pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/10 "2006-09-20T19:43:23Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [snip]
> 
> > If the former, you can tell Apache to serve Ruby scripts that contain the  
> > usual CGI conventions. That seems simple enough. And it might not be what  
> > you are asking for.
> 
> That’s the part I’m missing. I find Apache extremely difficult to  
> configure (beyond the basics). How would I tell Apache to serve Ruby  
> scripts?

On Debian and Ubuntu, it just works. After you apt-get install  
apache2, you just copy some foo.rb file (maybe like the one shown  
below) to /usr/lib/cgi-bin/ and then point your browser at  
[http://localhost/cgi-bin/foo.rb](http://localhost/cgi-bin/foo.rb) and boom. 🙂

==== snip /usr/lib/cgi-bin/foo.rb ====  
#!/opt/ruby/bin/ruby -w

puts “Content-type: text/html”  
puts

puts "

The time is now #{Time.now}, so you'd better get crackin'!

" ==== /snip ====

—John

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/11 "2006-09-20T20:01:24Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > files fine, it treats RHTML files as binaries and RB files as just  
> > > plain text. Here’s my code:
> > 
> > require ‘webrick’  
> > include WEBrick

- module WEBrick
- module HTTPServlet
- 

```
FileHandler.add_handler("rb", CGIHandler)

```

- end
- end

> > - server.mount(“/data/sandbox/ruby”, HTTPServlet::ERBHandler, ruby\_dir)  
> > server.start  
> > end
> > 
> > start\_webrick(:DocumentRoot =\> ‘/data/sandbox/ruby’)

Now it starts .rb for me, although I’m not able to write any good cgi  
right now 😉 maybe it’s because I’m on windows…

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/12 "2006-09-20T20:41:38Z")

</div>

Jan S. wrote:

> Now it starts .rb for me, although I’m not able to write any good cgi  
> right now 😉 maybe it’s because I’m on windows…

Doesn’t work for me. With even the simplest .rb file it gives me the  
following error:

ERROR Premature end of script headers:

but maybe I need to put a particular include in particular at the top  
of each .rb file? Not used to working with .rb files outside Rails.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/13 "2006-09-20T20:11:26Z")

</div>

On 9/20/06, zerohalo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t really want a  
> “framework”, just the ability to run a ruby file that will serve up an  
> rhtml file. In other words, something very simple like PHP (I don’t  
> want to teach her PHP). [I’ll get into Rails later once she’s more advanced.]

Does this help?  
[http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html](http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html)

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/14 "2006-09-20T21:01:40Z")

</div>

Wilson B. wrote:

> Does this help?  
> [http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html](http://cyll.org/blog/tech/2006-08-26-makingrubyintophp.html)

Indeed it did. Thanks. That works nicely.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/15 "2006-09-20T21:51:51Z")

</div>

zerohalo wrote:

> Jan S. wrote:
> 
> > Now it starts .rb for me, although I’m not able to write any good cgi  
> > right now 😉 maybe it’s because I’m on windows…
> 
> Doesn’t work for me. With even the simplest .rb file it gives me the  
> following error:
> 
> ERROR Premature end of script headers:

This is a classic CGI header error, caused by the absence of a  
recognizable  
emitted header like:

print “Content-type: text/html\r\n\r\n”

As the first text emitted by the script.

Remember, if you can get the server to read the filetype and treat it as  
a  
well-behaved source, and if you can get the Ruby scripts to emit the  
expected header information, your server requirements become much  
simpler  
to meet.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/16 "2006-09-20T22:31:59Z")

</div>

Paul L. wrote:

> This is a classic CGI header error, caused by the absence of a recognizable  
> emitted header like:
> 
> print “Content-type: text/html\r\n\r\n”
> 
> As the first text emitted by the script.

Got it. That works, thanks, Paul, and Jan, for your patience with my  
ignorance.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 20, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/17 "2006-09-20T22:01:53Z")

</div>

zerohalo wrote:

> > usual CGI conventions. That seems simple enough. And it might not be what  
> > you are asking for.
> 
> That’s the part I’m missing. I find Apache extremely difficult to  
> configure (beyond the basics). How would I tell Apache to serve Ruby  
> scripts?

Others have offered good advice. The simplest way is to give the server  
what  
it expects to see:

* * *

#!/usr/bin/ruby -w

print “Content-type: text/html\r\n\r\n”

print “

# Hello world!
”
* * *

Put this file in a directory where executable content is expected to be,  
like /serverpath/cgi-bin, and chances are Apache will treat it as you  
would  
expect, without any special configuration.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 21, 2006, 2:20am UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/18 "2006-09-21T02:20:18Z")

</div>

zerohalo wrote:

> Hi.

Hi.

> She’s just starting out, so Rails is much too complicated  
> for her. She’s not ready for the whole MVC concept yet.

FWIW, I have the brain of an 8yr old in when it comes  
to web-aps, and I managed to write a /couple/ Camping aps  
the other evening.

Regards,

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 21, 2006, 3:40am UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/19 "2006-09-21T03:40:39Z")

</div>

FranÃ§ois Montel wrote:

> Got it. That works, thanks, Paul, and Jan, for your patience with my  
> ignorance.

It seems like I am at the same level as your daughter [although a whole  
9 years older] and am trying to do the same thing you are. I could not  
get anything working from what I gathered in this thread, but it seems  
you have. Can you tell me what you ended up doing so I can do that too?

thanks

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [September 21, 2006, 3:56am UTC](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376/20 "2006-09-21T03:56:23Z")

</div>

Jonathan D. wrote:

> FranÃ§ois Montel wrote:
> 
> > Got it. That works, thanks, Paul, and Jan, for your patience with my  
> > ignorance.
> 
> It seems like I am at the same level as your daughter [although a whole  
> 9 years older] and am trying to do the same thing you are. I could not  
> get anything working from what I gathered in this thread, but it seems  
> you have. Can you tell me what you ended up doing so I can do that too?

It’s very simple, really. Apache requires that executable pages (pages  
that  
have to be processed by an interpreter like Ruby) by placed in a special  
directory. The default for this is usually /sitedirectry/cgi-bin.

Write an ordinary Ruby script, but make sure the first thing it prints  
is a  
CGI header, like this:

print “Content-type: text/html\r\n\r\n”

After this first line, just output HTML, as in my prior example, and the  
page will appear to all intents and purposes as though it contains the  
text  
it is printing. Therefore your Ruby code decides what the page content  
is,  
and it is obviously dynamic.

A Ruby interpreter must be installed on the server machine. Apart from  
that,  
it should work without any fuss.

[Next page](https://www.ruby-forum.com/t/super-simple-serving-of-ruby-pages/69376.md?page=2)
