Using WEBrick to serve PHP web applications

巧了我就是萌 提交于 2019-12-05 09:50:47

To get php support in webrick you can use a handler for php files. To do this you have to extend HttpServlet::AbstractServlet and implement the do_GET and do_POST methods. These methods are called for GET and POST requests from a browser. There you just have to feed the incoming request to php-cgi.

To get the PHPHandler to handle php files you have to add it to the HandlerTable of a specific mount. You can do it like this:

s = HTTPServer.new(
    :Port => port,
    :DocumentRoot => dir,
    :PHPPath => phppath
)
s.mount("/", HTTPServlet::FileHandler, dir, 
    {:FancyIndexing => true, :HandlerTable => {"php" => HTTPServlet::PHPHandler}})

The first statement initializes the server. The second adds options to the DocumentRoot mount. Here it enables directory listings and handling php files with PHPHandler. After that the server can be started with s.start().

I have written a PHPHandler myself as I haven't found one somewhere else. It is based on webricks CGIHandler, but reworked to get it working with php-cgi. You can have a look at the PHPHandler on GitHub:

https://github.com/questmaster/WEBrickPHPHandler

You can use nginx or lighttpd

Here's a minimal lighttpd config.

  1. Install PHP with FastCGI support and adjust the "bin-path" option below for your system. You can install it with MacPorts using sudo port install php5 +fastcgi
  2. Name this file lighttpd.conf
  3. then simply run lighttpd -f lighttpd.conf from any directory you'd like to serve.
  4. Open your webbrowser to localhost:8000

lighttpd.conf:

server.bind = "0.0.0.0"
server.port = 8000
server.document-root = CWD
server.errorlog          = CWD + "/lighttpd.error.log"
accesslog.filename       = CWD + "/lighttpd.access.log"

index-file.names = ( "index.php", "index.html",
                    "index.htm", "default.htm" )

server.modules = ("mod_fastcgi", "mod_accesslog")

fastcgi.server = ( ".php" => (( 
  "bin-path" => "/opt/local/bin/php-cgi",
  "socket" => CWD + "/php5.socket",
)))

mimetype.assign = (  
  ".css"        =>  "text/css",
  ".gif"        =>  "image/gif",
  ".htm"        =>  "text/html",
  ".html"       =>  "text/html",
  ".jpeg"       =>  "image/jpeg",
  ".jpg"        =>  "image/jpeg",
  ".js"         =>  "text/javascript",
  ".png"        =>  "image/png",
  ".swf"        =>  "application/x-shockwave-flash",
  ".txt"        =>  "text/plain"
)

# Making sure file uploads above 64k always work when using IE or Safari
# For more information, see http://trac.lighttpd.net/trac/ticket/360
$HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
  server.max-keep-alive-requests = 0
}

If you'd like to use a custom php.ini file, change bin-path to this:

"bin-path" => "/opt/local/bin/php-fcgi -c" + CWD + "/php.ini",

If you'd like to configure nginx to do the same, here's a pointer.

I found this, but I really think it isn't worth the hassle. Is making a virtual host (which isn't even necessary) that difficult? In the time it would take you to set this up to work with PHP, if you can even get it working, you could have written a script that creates virtual host entries for you, making it as easy as webrick.

It looks like WEBrick has CGI support, which implies that you can get PHP running by invoking it as a CGI script. The #! line at the top of each executable file would just need to point towards the absolute path to php-cgi.exe.

It's worth noting that you'd need to remove the #! line when moving the file to any other server that doesn't think of PHP as a CGI script, which would be ... uh ... all of'em.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!