Home » Php » "How the sausage is made" tour of apache/php/mysql interaction

"How the sausage is made" tour of apache/php/mysql interaction

Posted by: admin April 23, 2020 Leave a comment


I am having trouble understanding how apache/php/mysql stack works on a low level (including interaction with the browser). Is there a good description somewhere (a book, a website, etc) that will walk me through the whole path and explaining how starting with a browser reqesting a url, http requests is being sent, how apache talks to php, how php talks to mysql (persistant and non-persistant connections), etc, etc. I want to understand what waits for what in this chain, where timeouts are handled, how long sockets are opened and closed. A book, an article maybe? There is a lot of documentation on each individual component, but I can’t find a “walkthrough”.

The explanations I se so far are very high-level: look, here’s a happy cow, it goes to Bovine University, look – it’s all shrink wrapped on the supermarket shelf. What I need is the sausage farm/slaughterhouse/truck/factory tour, starting with cow insemenation 🙂

[update] To this day I have not found a better way to learn about these things other than reading the source.

How to&Answers:

PHP and MySQL by example has a pretty basic picture of the process, which I think you probably already understand.

Getting more in-depth than that picture though is a pretty long discussion. Ironically, you can read the book I just linked for a pretty good description. If you have more specific questions, I recommend opening new questions for them. Enjoy!


I have found a site that has, at least in part, contents from the book Advanced PHP Programming by George Schlossnagle.

The site is located at: http://php.find-info.ru/php/016/toc.html. Specifically, the section on The PHP Request Life Cycle contains a lot of the nitty-gritty details, including some source code and diagrams.

DISCLAIMER: IANAL, but considering that the book is still listed on Amazon, its possible the content linked to above breaks all sorts of codes, rules and/or laws. Its not my intention to proliferate or condone illegal or pirated materials, so if that be the case, please remove said links.


You are correct in the fact that there are entire books written on how this all fits togeather here is a link to a “walkthrough” it touches on the main parts.


Hope it helps


The best course of action would be to get a good book about the LAMP stack.

A quick response (ask for more if you feel you need it)
Browser contacts web server though HTTP protocol
Server generates (let’s leave how for the moment) an html result and posts it back.
Each browser understands only http protocol (for the sake of this analysis).

Now items such as icons, images, javascript etc, are just read from the apache server and “copied” to the browser. Same in plain html files.
The difference is in php files (I am oversimplifying here). These are passed to the php module and the response (of the module) will be sent back to the browser.

The php module is what understands php.
Are we together here? if yes then:
Php script may (or may not) require data from an MySQL server, it has to connect get them or manipulate them etc.

Summarising: Each of these operation is being done individually in a different process level. That’s what makes it “simple”.
Ask for more information if you want something more specific.


As far as I understand it apache receives the request, and works out what to do with it based on your .htaccess or config options. It then passes this request to PHP for parsing, if needed. PHP does two scans of the code, the first is the pre-parse, this picks up obvious flaws and runs functions on the top level(ignoring any in if statements, loops, includes, evals or lamda based functions), before parsing the page for real. Anything done with echo, I do believe, is returned as the standard out stream, and is returned to apache. If apache times the page out it sends the kill signal to PHP, which closes objects, prints the error messages if needed, before exiting. Once the page exits apache tends to headers and returns the page.

I would love to know more about this though, so if anyone can explain it better or has a correction/expansion on my answer, I’d love to hear it.