# Wish list: webrick server with cgi test file

**URL:** <https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910>\
**Category:** Ruby\
**Created:** [June 20, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910 "2006-06-20T14:56:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [June 20, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910/1 "2006-06-20T14:56:06Z")

</div>

Anyone has a webrick server which works with a cgi test file, to share  
with me?  
Hopefully, not all webrick users are at the rails conference!

I have tried various combinations with various errors. Here is the  
latest attempt  
using the info on this page for server and test file  
[http://microjet.ath.cx/webrickguide/html/CGIHandler.html](http://microjet.ath.cx/webrickguide/html/CGIHandler.html)  
-----------------\>  
require ‘webrick’

include WEBrick # let’s import the namespace so  
# I don’t have to keep typing  
# WEBrick:: in this documentation.

def start\_webrick(config = {})

# always listen on port 8080

config.update(:Port =\> 8080)  
server = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}  
server.start

end

start\_webrick {|server|  
cgi\_dir = ‘/library/webserver/documents/ruby/cgi-bin’  
server.mount(‘/cgi-bin’, HTTPServlet::FileHandler, cgi\_dir,  
{:FancyIndexing=\>true})  
}

with the test file in cgi-bin, test2.cgi, executable  
#!/usr/bin/env ruby  
print “Content-type: text/plain\r\n\r\n”  
ENV.keys.sort.each{|k| puts “#{k} ==\> #{ENV[k]}”}

I get an error message.  
env: ruby: No such file or directory  
With other attempts I get other errors.

I started with the webserver on this page  
[http://microjet.ath.cx/webrickguide/html/What\_is\_WEBrick.html](http://microjet.ath.cx/webrickguide/html/What_is_WEBrick.html)  
----------------\>  
require ‘webrick’

include WEBrick # let’s import the namespace so  
# I don’t have to keep typing  
# WEBrick:: in this documentation.

def start\_webrick(config = {})

# always listen on port 8080

config.update(:Port =\> 8080)  
server = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}  
server.start

end

## start\_webrick(:DocumentRoot =\> ‘/library/webserver/documents/ruby/cgi-bin’)

it serves a hello.html file in cgi-bin directory fine.  
-----------------\>

 A Small Hello 
# Hi

This is very minimal "hello world" HTML document.

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [June 20, 2006, 3:20pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910/2 "2006-06-20T15:20:28Z")

</div>

anne001 wrote:

> I get an error message.  
> env: ruby: No such file or directory  
> With other attempts I get other errors.

This particular error has nothing to do with webrick. What is happening  
is that the /usr/bin/env command cannot find ruby in it’s path. The  
first thing to try would be to replace

#!/usr/bin/env ruby

With

#!/usr/bin/ruby

Or where ever your ruby image is (try ‘which ruby’ to find out).

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [June 20, 2006, 4:48pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910/3 "2006-06-20T16:48:24Z")

</div>

Thank you, changing that line did fix this example.  
And with this line my other test cgi script works too. Thank you!!!

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [June 20, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910/4 "2006-06-20T17:28:22Z")

</div>

start\_webrick(:DocumentRoot =\> ‘/var/www’)

is functionally similar to:

start\_webrick {|server|  
doc\_root = =’/var/www’  
server.mount("/" HTTPServlet::FileHandler doc\_root,  
{:FancyIndexing=\>true})  
}

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [June 20, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/wish-list-webrick-server-with-cgi-test-file/63910/5 "2006-06-20T17:03:56Z")

</div>

What is a webrick servlet?  
[http://microjet.ath.cx/webrickguide/html/CGIHandler.html](http://microjet.ath.cx/webrickguide/html/CGIHandler.html)

I can serve my cgi test file with a webrick server with or without  
servermount…  
why does this page seem to say cgi needs a cgi handler?  
Are the two servers different?

first server  
---------------\>  
require ‘webrick’

include WEBrick # let’s import the namespace so  
# I don’t have to keep typing  
# WEBrick:: in this documentation.

def start\_webrick(config = {})

# always listen on port 8080

config.update(:Port =\> 8080)  
server = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}  
server.start

end

start\_webrick(:DocumentRoot =\>  
‘/library/webserver/documents/ruby/cgi-bin’)

second server  
------------------\>  
require ‘webrick’

include WEBrick # let’s import the namespace so  
# I don’t have to keep typing  
# WEBrick:: in this documentation.

def start\_webrick(config = {})

# always listen on port 8080

config.update(:Port =\> 8080)  
server = HTTPServer.new(config)  
yield server if block\_given?  
[‘INT’, ‘TERM’].each {|signal|  
trap(signal) {server.shutdown}  
}  
server.start

end

start\_webrick {|server|  
cgi\_dir = ‘/library/webserver/documents/ruby/cgi-bin’  
server.mount(‘/cgi-bin’, HTTPServlet::FileHandler, cgi\_dir,  
{:FancyIndexing=\>true})  
}
