Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP interface with Apache?

Tags:

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 knowledge 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!

like image 958
Waleed Amjad Avatar asked May 05 '10 10:05

Waleed Amjad


People also ask

What is the relation between Apache and PHP?

Apache is the web server that processes requests and serves web assets and content via HTTP. MySQL is the database that stores all your information in an easily queried format. PHP is the programming language that works with apache to help create dynamic web content.

Does Apache handle PHP?

PHP handlers, a type of Apache module, contain libraries that the Apache web server uses to interpret and run PHP code. The /etc/apache2/conf. d/php.

How does PHP interact with a web server?

Step 1 – Client send a page request to the web server. Step 2 – Web server forwards that request to the PHP interpreter. Step 3 – Now PHP interpreter will take the Date from Database and responce it back to the Web server.

Why does PHP need Apache?

PHP isn't a web server, it's a scripting language. Since you do need a web server, as you say, Apache is rather necessary, yes. (Caveat, asterisk: PHP ships with a development web server, and you can write a web server in PHP, but both are bad ideas in production.)


1 Answers

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.

like image 90
symcbean Avatar answered Sep 21 '22 14:09

symcbean