开发者

How does PHP interface with Apache?

开发者 https://www.devze.com 2022-12-29 10:22 出处:网络
I\'ve almost finished writing a HTTP/1.0 compliant web server under Java (no commercial usage as such, this is just for fun) and basically I want to include PHP support. I realize that this is no easy

I've almost finished writing a HTTP/1.0 compliant web server under Java (no commercial usage as such, this is just for fun) and basically I want to include PHP support. I realize that this is no easy task at all, but I think it'll be a nice accomplishment.

So I want to know how PHP exactly interfaces with the Apache web server (or any other web server really), so I can learn from it and write my own PHP wrapper. It doesn't necessarily have to be mod_php, I don't mind writing a FastCGI wrapper - which to my knowle开发者_StackOverflow社区dge is capable of running PHP as well.

I would've thought that all that PHP needs is the output that goes to client (so it can interpret the PHP parts), the full HTTP request from client (so it can extract POST variables and such) and the client's host name. And then you simply take the parsed PHP code and write that to the output stream. There will probably be more things, but in essence that's how I would have thought it works.

From what I've gathered so far, apache2handler provides an API which PHP makes use of to 'connect' to Apache. I guess it's an idea to look at the source code for apache2handler and php5apache2.dll or so, but before I do that I thought I'd ask SO first.

If anyone has more information, experience, or some sort of specification that is relevant to this then please let me know.

Thanks in advance!


There are 3 ways PHP can be invoked from Apache:

1) as a module - this involves linking the php interpreter against a library of hooks published by the webserver

2) CGI - the webserver starts up an instance of the interpreter for each request and passes parameters to the interpreter via stdin, the command line and environment variables, stdout is sent to the client and stderr should be written to the error_log

3) fastCGI - this eliminates the overhead of starting a new process for each request - the interpreter runs as a daemon

CGI is the simplest to implement but does not scale/perform well, the module would be the hardest by far. FastCGI is nearly as fast as the module approach. CGI and fastCGI are open, well documented APIs.

There are other ways of achieving your goal - e.g. Quercus

C.


To put it simply, this is how it works:

Apache normally serves files by fetching the file and sending the stream down the HTTP connection. With PHP, however, Apache fetches the file, pipes it into the PHP binary, and sends the output stream from the command down the HTTP connection.


In addition to the php file, HTTP request, and the client host name, there are certain other items of information typically passed to PHP to set certain other elements of the $_SERVER superglobal. The linked documentation page has a list of what is typically set.


The key word is CGI.
It is dramatically simple protocol, which serving web-servers for ages.
It is not the only way for PHP to interact with a web-server, but most common and easy to implement one.

In short, your server must set up some environment variables, and then call a cgi-script which is just a php script itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消