# Run CGI scripts in own webserver

**URL:** <https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188>\
**Category:** ruby-talk\
**Created:** [21 March 2011 20:11 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188 "2011-03-21T20:11:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_DeValck](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@Kevin\_DeValck](https://rubytalk.org/u/Kevin_DeValck)\
**Post date:** [21 March 2011 20:11 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/1 "2011-03-21T20:11:32Z")

</div>

Hello,

Does anybody know how I can run ruby cgi scripts in my web server. I  
coded the webserver myself in ruby. At this moment I use ERB to parse  
the .rhtml files. It all works fine but now i want to use the  
functionality of CGI to extend my webserver. So I would like to read the  
CGI file, parse it and send my html output to the user's webbrowser.

So does anybody knows how to run a CGI file?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Andrew\_Mcelroy](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Andrew\_Mcelroy](https://rubytalk.org/u/Andrew_Mcelroy)\
**Post date:** [21 March 2011 22:18 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/2 "2011-03-21T22:18:15Z")

</div>

> Hello,
> 
> Does anybody know how I can run ruby cgi scripts in my web server. I  
> coded the webserver myself in ruby. At this moment I use ERB to parse  
> the .rhtml files. It all works fine but now i want to use the  
> functionality of CGI to extend my webserver. So I would like to read the  
> CGI file, parse it and send my html output to the user's webbrowser.
> 
> So does anybody knows how to run a CGI file?

CGI is part of standard lib. try ruby uses it (until i can get a sessions  
issue worked out in rails).

Andrew McElroy  
[http://tryruby.org](http://tryruby.org)

> **···**
>
> On Mon, Mar 21, 2011 at 3:11 PM, Kevin Kevin \<kevin\_de\_valck@hotmail.com\>wrote:
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [21 March 2011 22:49 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/3 "2011-03-21T22:49:47Z")

</div>

That depends on your webserver. Apache has the fast\_cgi module, which  
should help you in getting up to speed, if you use Apache. I'm certain  
lighttp and NGinx have similar modules/plugins/whatever to facilitate  
CGI.

And I think Ruby's Webrick HTTP server-cum-library should be able to  
serve Ruby CGI scripts.

> **···**
>
> On Mon, Mar 21, 2011 at 9:11 PM, Kevin Kevin \<kevin\_de\_valck@hotmail.com\> wrote:
> 
> > Hello,
> > 
> > Does anybody know how I can run ruby cgi scripts in my web server. I  
> > coded the webserver myself in ruby. At this moment I use ERB to parse  
> > the .rhtml files. It all works fine but now i want to use the  
> > functionality of CGI to extend my webserver. So I would like to read the  
> > CGI file, parse it and send my html output to the user's webbrowser.
> > 
> > So does anybody knows how to run a CGI file?
> 
> --  
> Phillip Gawlowski
> 
> Though the folk I have met,  
> (Ah, how soon!) they forget  
> When I've moved on to some other place,  
> There may be one or two,  
> When I've played and passed through,  
> Who'll remember my song or my face.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [22 March 2011 09:03 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/4 "2011-03-22T09:03:44Z")

</div>

Kevin DeValck wrote in post #988602:

> Does anybody know how I can run ruby cgi scripts in my web server. I  
> coded the webserver myself in ruby.

You need to implement the (obsolete) CGI spec yourself in your server:  
[http://w3.org/CGI/](http://w3.org/CGI/)  
[http://graphcomp.com/info/specs/cgi11.html](http://graphcomp.com/info/specs/cgi11.html)  
See also RFC 3875

You could spawn the process yourself using IO.popen, but the trouble is  
setting the environment variables first in a thread-safe way. Probably  
better to use IO.pipe, fork and exec. Look at the source for open3.rb in  
the standard library.

I'm sure this will be an interesting learning exercise about how CGI  
works, but most people have abandoned CGI anyway because it's hugely  
inefficient. So if I were you, I'd be looking at Rack instead, or at  
least FastCGI or SCGI.

HTH,

Brian.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [22 March 2011 00:51 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/5 "2011-03-22T00:51:55Z")

</div>

Andrew Mcelroy wrote in post #988626:

> CGI is part of standard lib. try ruby uses it (until i can get a

He can use Sinatra or some other Rack-based framework. Rack supports the  
CGI interface. (Well, it's supposed to; I haven't actually tried it.)  
This will save him the work of converting the code in the future if he  
decides to move away from CGI.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [22 March 2011 01:06 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/6 "2011-03-22T01:06:16Z")

</div>

Phillip Gawlowski wrote in post #988633:

> **···**
>
> > On Mon, Mar 21, 2011 at 9:11 PM, Kevin Kevin \> \<kevin\_de\_valck@hotmail.com\> wrote:
> > 
> > > Hello,
> > > 
> > > Does anybody know how I can run ruby cgi scripts in my web server. I  
> > > coded the webserver myself in ruby. At this moment I use ERB to parse  
> > > the .rhtml files. It all works fine but now i want to use the  
> > > functionality of CGI to extend my webserver. So I would like to read the  
> > > CGI file, parse it and send my html output to the user's webbrowser.
> > > 
> > > So does anybody knows how to run a CGI file?
> > 
> > That depends on your webserver.
> 
> As the op stated, he wrote his own server.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Andrew\_Mcelroy](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Andrew\_Mcelroy](https://rubytalk.org/u/Andrew_Mcelroy)\
**Post date:** [22 March 2011 01:13 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/7 "2011-03-22T01:13:12Z")

</div>

> > Hello,
> > 
> > Does anybody know how I can run ruby cgi scripts in my web server. I  
> > coded the webserver myself in ruby. At this moment I use ERB to parse  
> > the .rhtml files. It all works fine but now i want to use the  
> > functionality of CGI to extend my webserver. So I would like to read the  
> > CGI file, parse it and send my html output to the user's webbrowser.
> > 
> > So does anybody knows how to run a CGI file?
> 
> CGI is part of standard lib. try ruby uses it (until i can get a sessions  
> issue worked out in rails).

I should clarify that try ruby is open source:

> **[GitHub - Sophrinix/TryRuby: Try Ruby is a interactive shell that quickly and...](https://github.com/sophrinix/TryRuby)**
>
> Try Ruby is a interactive shell that quickly and whimsically teaches the Ruby programming language. Originally \_why's idea, it has been recreated from the ground up by Rubyists who have a passi...

the /public directory is the ruby CGI 1.9.2 based version. it will show you  
how to write a ruby cgi website. I didn't mean that to come off as self  
promoting as it may of sounded.

Andrew McElroy  
[http://TryRuby.org](http://TryRuby.org)

> **···**
>
> On Mon, Mar 21, 2011 at 5:18 PM, andrew mcelroy \<sophrinix@gmail.com\> wrote:
> 
> > On Mon, Mar 21, 2011 at 3:11 PM, Kevin Kevin \<kevin\_de\_valck@hotmail.com\>wrote:
> 
> > Andrew McElroy  
> > [http://tryruby.org](http://tryruby.org)
> > 
> > > --
> > > 
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [22 March 2011 01:15 UTC](https://rubytalk.org/t/run-cgi-scripts-in-own-webserver/62188/8 "2011-03-22T01:15:58Z")

</div>

Which I failed to parse completely. \*d'oh\*

> **···**
>
> On Tue, Mar 22, 2011 at 2:06 AM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > As the op stated, he wrote his own server.
> 
> --  
> Phillip Gawlowski
> 
> Though the folk I have met,  
> (Ah, how soon!) they forget  
> When I've moved on to some other place,  
> There may be one or two,  
> When I've played and passed through,  
> Who'll remember my song or my face.
