More Details About the Server Side – mogosselin.com

More Details About the Server Side

For beginners wanting to learn “web programming”, it’s a good idea to
know how the web actually works. Lately, I wrote the article “Client Side vs Server Side“.
It was a high-level summary of what is happening when you request a web
page. In this article, I’ll go a little bit deeper on what goes on when
the request reaches the “server side”.

Reminder

To refresh your memory, here’s what you should remember about the server side:

  • The “server side” usually means a server somewhere on the Internet
    that has the capacity to interpret code. In our case, PHP files.
  • When you work with XAMP, MAMP or a virtual machine for example, your computer plays the role of the client AND the server.
  • The server is also responsible for serving static files such as CSS, images and JavaScript.

How Does a Server Listen for Requests?

Computer that needs to listen for incoming connections does this
using a “port”. Basically, the computer “opens” the needed port and
start listening on it.

For web pages, the default port is “80″. When you enter
“http://www.google.com” in your browser’s URL bar, it’ll try to query
the port 80 by default.

That’s why you might have seen a URL like this:
http://www.some-site.com:8080. 8080 is often a port used for local web
servers when you develop on your own computer. But it could also be any
number between 1 and 65,535.

The number after the double dots “:” is, as you guessed, the port.
Since it’s not 80, we need to specify it, or else our browser will
simply query the server on the port 80, instead of the other specific
port. There’s a lot of different conventions as to what port serves what purpose, but a web server could listen on the port “1234″ for web pages if it’s configured like it.

And, for HTTPS (secured connection), the default port is 443.

What Listens to the Requests?

Ok, the server “opens” the port 80 to listen to HTTP requests, but what actually “opens” the port?

Well, that’s where Apache, Nginx and other “web server applications” come into play.

That’s their role. They open a port (or multiple ports), listen for a
request and do whatever the configuration tells them to do with the
request when they receive one.

How Does it All Work Together?

A typical Apache configuration for PHP just tells Apache to “give”
the request to the PHP interpreter when a file with the extension “.php”
is requested.

So, for example, if the page “http://www.site.com/home.php” is
requested on a server (port 80), Apache will give that request to the
PHP “program”.

Then, the PHP “program” will interpret the PHP (it will read/compile
it and then execute it). If a PHP page was requested from a browser, the
PHP code should output a complete HTML page.

That response is then sent to Apache, which in turn is sent back to the browser.

Now, keep in mind that this is still a high-level view of how it works on the server side.

For example, PHP doesn’t necessarily wait for the complete HTML
response before sending it to Apache, it can send it in “chunk”. Also,
web servers have different way of “sending” requests to PHP (CGI,
fastCGI, as a module). But all of this is for another article ;)

Graphic Time

Server side details of a request

  1. The application server opens the port 80 and listen for connections
  2. Your browser sends a request for the page /index.php to the application server
  3. The application server receives your request and, based on its
    configuration that says “send .php file to PHP”, push the request to PHP
  4. PHP compile and interpret the code and then send the response back to the application server
  5. The application server send the response back to your browser

Server Side Summary

This time, we went a little bit deeper on the server side. I hope
it’ll help some of you understand how XAMP and MAMP operate on your own
computer. That way, you should be able to better troubleshoot problems
at the server side!.

Here’s a summary of what happens on the server:

  • The application server (Apache, Nginx, etc.) listens on a port for HTTP connection
  • The default port for HTTP requests is 80 or 443 for HTTPS. If any other port is used, you’ll need to specify it in the URL.
  • The application server will “give” the requests to the right application based on his configuration
  • If it’s a static file, the server will usually serve it himself
  • If it’s a file with code (like a PHP file), the server will usually give it to the appropriate program. In this case, PHP.
  • The “program” will interpret the code and send an HTML response back to the application server
  • Finally, the application server will send the response back to the browser.

PS: This post explained in more details what is the “server side”. The free emails I send 2 times a month contains even more tips and tricks for people like you that want to improve their PHP skills! Interested? Click here!

Source:- http://www.mogosselin.com/