Web Development in Ruby : Write a Hello World Web App

Objective


To write a hello world web app that displays a static web page to a user.

Steps


Step 1

Create webrick.rb with the following code:

#!/usr/bin/env ruby
require 'webrick'
include WEBrick
s = HTTPServer.new(
    :Port => 3000,
    :DocumentRoot => File.join(Dir.pwd, "/html")
)
trap("INT") { s.shutdown }
s.start

Step 2

Check the permissions on webrick.rb.

$ls -l webrick.rb 
-rw-r--r--  1 zepho  staff  179 May 16 13:32 webrick.rb

Give execute permissions to the script by running:

$chmod a+x webrick.rb

Check the permissions:

-rwxr-xr-x  1 zepho  staff  179 May 16 13:32 webrick.rb

Step 3

Create html directory with index.html:

Hello Static Page

Step 4

Run the script:

$ruby webrick.rb

Step 5

Browse to http://localhost:3000. You will see the contents of index.html file: Hello Static Page in the browser.

Step 6

Copy the script to local cgi-bin folder on the server.

Step 7

Execute it from a browser by going to your domain www.example.com.

Step 8

Use Ctrl+C to stop the server.

Summary


In this article, we wrote a simple hello world program that displays a static page. In subsequent articles of this series, we will create dynamic pages that will be executed on the server and display on the browser as a webpage.

Reference


Ruby Webrick CGI Scripts


Related Articles


Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now