How do Apache, PHP, and MySQL All Work Together?

This is something I am trying to grasp my head around.
Okay, so Apache simply servers files on a web server to clients. I fully understand that. But then if you install PHP, suddenly PHP code will be interpreted before those files are served by Apache. Then you install MySQL, and all of a sudden PHP can work directly with MySQL.

What is really done in the installation process that allows these three things to work so closely together? If Apache and PHP are separate programs, then how do they both work together on serving files?

Moreover, I understand that PHP is used to generate dynamic pages, but I've also heard people saying we're running a PHP based application.
Varun MalhotraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Apache is the HTTP server.  It runs on Linux and receives requests* from the internet.  It can be told what program to start when it receives a request.  A configuration file called .htaccess tells Apache what to do.  Here is a sample from my web server.  It tells apache to start the PHP5 application whenever a request is made for a file ending in .html, .htm. or .php

AddType application/x-httpd-php5 .html .htm .php

* Request is a term of art in client/server systems.  More on that here:

What allows these programs to work together?  They were designed to work together!

PHP is used to generate dynamic web pages.  It can generate these from a variety of data sources - its own files, databases (like MySQL), external APIs, etc.  PHP is also used to perform work that does not create web pages.  For example, it can be used to respond to a request with a JSON string or XML document.

If you want to learn more about PHP, install a copy and start using it.  This can help you get going.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
First, of all, they are all separate programs. They don't NEED to work together, and they can each be used independently of each other.

Installing PHP doesn't "suddenly" make Apache able to process PHP files. You have to configure Apache to tell it, "Whenever someone asks for a file that ends in .php, give the request to the PHP engine to process it."

Similarly, installing MySQL doesn't suddenly allow PHP to access MySQL. PHP has a MySQL library inside that has all the various instructions on how to access a MySQL server. From there, PHP code has to be written to use that library to connect it to the MySQL server.

I like to think of it all as a bakery. Apache is the person at the front that takes the order. For orders that require a baker to create a cake, Apache passes the information back to the baker in the back (PHP). The baker (PHP) then starts to follow a recipe to create the cake. In many cases, the cake will need special ingredients from the supermarket (MySQL / databases), so the baker will go take a trip to the supermarket, pick up the ingredients, come back to the bakery, and use them to bake the cake (the final response). The baker then hands the cake to the person at the front (Apache), who hands it to the customer.

It is very similar to the way it all works on the backend, and if you think about it - each component/person can be used by themselves. You can go to the supermarket by yourself if you want. A baker could bake a cake at home without needing an order. The person at the front could theoretically just sell items that were in the ready-to-buy case.
Varun MalhotraAuthor Commented:
Thanks everyone. I now understand. And thanks for providing the links @Ray.....:)
Bernard S.CTOCommented:
Just a final note here:
Because some licensing problems, php's default interface with mysql databases is no more the one named mysql, but a different one named mysqli
Bernard S.CTOCommented:

You should close this question now, allocating points to ansxer(s)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.