使用 WEBrick 为 PHP Web 应用程序提供服务

发布于 2024-10-27 03:26:05 字数 306 浏览 1 评论 0原文

我是一名 PHP 开发人员,已经开始学习 Ruby on Rails。我喜欢启动和运行 Rails 应用程序开发是多么容易。我最喜欢的东西之一是 WEBrick。它使您不必为您正在处理的每个小项目配置 Apache 和虚拟主机。 WEBrick 允许您轻松启动和关闭服务器,以便您可以在 Web 应用程序周围单击。

我并不总是有机会开发 Ruby on Rails 应用程序,因此我想知道如何配置(或修改)WEBrick 以便能够使用它来为我的 PHP 项目和 Zend Framework 应用程序提供服务。

你尝试过这个吗?为了实现这一目标需要采取哪些必要步骤?

I am a PHP developer who has started learning Ruby on Rails. I love how easy it is to get up and running developing Rails applications. One of the things I love most is WEBrick. It makes it so you don't have to configure Apache and Virtual Hosts for every little project you are working on. WEBrick allows you to easily start up and shut down a server so you can click around your web application.

I don't always have the luxury of working on a Ruby on Rails app, so I was wondering how I might configure (or modify) WEBrick to be able to use it to serve up my PHP projects and Zend Framework applications.

Have you attempted this? What would be the necessary steps in order to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那支青花 2024-11-03 03:26:05

要在 webrick 中获得 php 支持,您可以使用 php 文件的处理程序。为此,您必须扩展 HttpServlet::AbstractServlet 并实现 do_GETdo_POST 方法。这些方法是为来自浏览器的 GET 和 POST 请求而调用的。您只需将传入的请求提供给 php-cgi 即可。

要让 PHPHandler 处理 php 文件,您必须将其添加到特定安装的 HandlerTable 中。您可以这样做:

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

第一个语句初始化服务器。第二个将选项添加到 DocumentRoot 挂载。在这里,它启用目录列表并使用 PHPHandler 处理 php 文件。之后可以使用s.start()启动服务器。

我自己编写了一个 PHPHandler,因为我在其他地方没有找到它。它基于 webricks CGIHandler,但经过重新设计以使其与 php-cgi 一起使用。您可以在 GitHub 上查看 PHPHandler:

https://github.com/questmaster/WEBrickPHPHandler

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

稚然 2024-11-03 03:26:05

您可以使用 nginxlighttpd

这是一个最小的 lighttpd 配置。

  1. 安装支持 FastCGI 的 PHP,并根据您的系统调整下面的“bin-path”选项。您可以通过 MacPorts 使用 sudo port install php5 +fastcgi
  2. 将此文件命名为 lighttpd 来安装它.conf
  3. 然后只需从您想要提供服务的任何目录运行 lighttpd -f lighttpd.conf 即可。
  4. 打开您的网络浏览器到 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
}

如果您想使用自定义 php.ini 文件,请将 bin-path 更改为:

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

如果您想配置 nginx 执行相同操作,这里有一个指针

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.

送君千里 2024-11-03 03:26:05

我发现了这个,但我真的认为这不值得这么麻烦。制作虚拟主机(甚至没有必要)有那么困难吗?在您需要将其设置为与 PHP 一起使用时,如果您甚至可以让它工作,您可以编写一个脚本来为您创建虚拟主机条目,使其像 webrick 一样简单。

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.

剩一世无双 2024-11-03 03:26:05

看起来像 WEBrick 有 CGI 支持,意味着您可以通过将 PHP 作为 CGI 脚本调用来运行它。每个可执行文件顶部的 #! 行只需指向 php-cgi.exe 的绝对路径。

值得注意的是,在将文件移动到任何其他不将 PHP 视为 CGI 脚本的服务器时,您需要删除 #! 行,这会是……呃……所有人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文