In this project a PHPHandler for the ruby WEBrick minimal webserver was created. With an PHP installation the webserver can process php files and serve php websites.
- docroot => example webserver root directory
- main.rb => example webserver start script
- phphandler.rb => PHPHandler for WEBrick
- Ruby with WEBrick
- The handler supports PHP 5 and needs a PHP 5 installation.
- The WEBrick configuration :DocumentRoot has to be set
- The new WEBrick configuration :PHPPath has to be set
It is assumed that all php files are accessible from the :DocumentRoot path.
With the mount() method the PHPHandler can be registered for files with the .php file extension in the :HandlerTable. Additionally :DocumentRoot has to be set to be able to resolve the local path of the files to be processed. Also the :PHPPath variable has to be set to the location of the PHP 5 'php-cgi.exe' location.
require 'webrick'
require_relative 'phphandler'
include WEBrick
# fill config vars
dir = File.join(File.dirname(__FILE__), "docroot")
port = 8080
phppath = File.join(File.dirname(__FILE__), "php-5.4.9-Win32-VC9-x86").gsub("/", "\\")
# create new server with config from vars
server = HTTPServer.new(
:Port => port,
:DocumentRoot => dir,
:PHPPath => phppath
)
# mount document root again to set new options (add PHPHandler for .php files)
server.mount("/", HTTPServlet::FileHandler, dir,
{:FancyIndexing => true, :HandlerTable => {"php" => HTTPServlet::PHPHandler}})
# start server / prepare shutdown
trap("INT") { server.shutdown }
server.start