r/PHPhelp • • Jul 29 '22

Solved What is the relation between PHP and Apache

Apache need PHP to work, or could be any other language?

8 Upvotes

17 comments sorted by

14

u/gordonv Jul 29 '22

Apache is a program, or daemon, that has 1 job. To serve websites in the HTTP and HTTPS protocols. Commonly known as a web server.

  • Take static files and push them through the protocols

  • Take requests from clients and run small programs that produce output, and forward that output to the client.


PHP is an interpreted scripting language. The code is read from a plain text file and then interpreted as instructions. These instructions can do many things.

PHP can:

  • Run from the command line

  • Call other programs

  • Do network and process communication

  • Output text data so other programs (like Apache) can pick it up.


The relationship of Apache and PHP, is that Apache acts like a broadcaster. It gives out information. PHP acts like a brain. It can take data input, "think" about it, and output data as a response.

For example:

A user wants to know how many bananas are in the database.

  • User goes to a website.

  • Apache serves the website.

  • User clicks the banana count link.

  • The signal is sent/interpreted by Apache, and Apache points to the PHP code that pulls the number of bananas from a database.

  • PHP returns the answer to Apache

  • Apache serves the answer to the user

1

u/hungryaf2 Aug 01 '22

Damn, long and clear. Ty for your time, it's wasn't what I wanted to know but it make me want to build a server.

4

u/gordonv Jul 29 '22

To understand this better, I think you should set up a web server.

Pick an OS.
Install Apache.
Write a simple 1 page HTML page.

This would teach you the very basics of a web server. All Apache does is pick up the file and send it to the client. Nothing more, nothing less.

4

u/gordonv Jul 29 '22

After that, you can install PHP and connect it to Apache.

Then you will see that you can write PHP files and Apache will take the output of that script and sent it to the client.

1

u/imthebusman Jun 26 '25

but I don't see this in practice. The home page of website is a php endpoint.

in that case, what is Apache picking up?

1

u/gordonv Jun 26 '25

Apache is the thing that is serving out the page via HTTP/S

PHP is merely taking in text, processing, and outputting text.

It works like the CGI-BIN parts of web servers.

1

u/gordonv Jun 26 '25

Make a simple file that ends in .HTML or .HTM

That's a simple flat file that gets pushed out by Apache.

1

u/imthebusman Jun 27 '25

do u mean empty file?

actually, I guess it's more like say home page html is at "index.html", there's a index.php that somehow handles request and in the end returns html.

1

u/gordonv Jun 27 '25

Oh, yeah. That's normal behavior. Your install is using PHP as a processor.

So, I started making web pages where I had to make EXEs as my dynamic data processor and pass it through something called the CGI-BIN folder.

PHP was invented by a guy who wanted to make a shortcut for developing pages. It turned out very useful.

Apache is good at 2 things:

  • Serving static data

  • Linking to other programs and serving that data

You can attach Apache to a lot of stuff. Apache handles the HTTP/S part of things very well.

2

u/dabenu Jul 29 '22

It might be fair to say there's no relation at all. Not from a technical point of view anyway. They're both very different pieces of software serving very different purposes. Neither depends on the other.

The only real relationship is that it happens to be a popular combination to run on a webserver.

1

u/hungryaf2 Aug 01 '22

This is what I needed . Ty :)

5

u/ZLegacy Jul 29 '22

Apache is handling the http piping. It grabs the file, sends it off to php to process, then sends it back to the user.

3

u/ontelo Jul 29 '22

Apache is webserver, php is scripting language.

PHP does not need apache but can work "under" it

2

u/McUsrII Jul 29 '22

Php has a development server, it can work with serving one client which is very useful for having a private lightweight server locally.

A webserver can fork one instance of php for each client, and it can setup access privileges.

Comparing a webserver to php, is akin to comparing MySql and sqlite.

1

u/olefart75 Jul 31 '22

Apache is a server. It translates and generates web (HTTP) languages and displays the content the web code creates in a web browser. PHP needs Apache, Apache does not need PHP.

PHP is one of the many languages Apache translates into web content.