PHP vs
Rails
Which originally was called:
LAMP
    vs
The World
and is probably a far more
     appropriate title.
But first,
Some housekeeping
Giving not just one talk,
not two, but three talks
     in December!
Already gave:
     Git & Github at Web414
blog.mattgauger.com/git-github-a-beginners-guide
Just go to: blog.mattgauger.com
MKE Ruby User’s Group:
What’s new & great in Rails 3
       Dec 20th 7PM
Slides from this talk will be
online at blog.mattgauger.com
     tomorrow (probably)
Simple agenda for tonight:
      Webservers:
  where we came from,
 where we are now, and
   where we’re going
All web sites (web apps)
     have a need.
A need for speed.
How do we get there?
Let’s start with some basics.
HTTP
Hypertext Transfer Protocol
Request, Response
Client, Server
A Request or Response is
   made of 4 parts:
• Request line, such as GET /index.html
• Headers, such as Accept-Language: en
• An empty line
• An optional message body
REST
A system of Verbs and
       Nouns
for interacting with a web
         resource.
GET /index.html
POST /form.html
GET, POST, PUT, DELETE
Roy Fielding first defined REST
10 years ago in his dissertation
 Architectural Styles and the
  Design of Network-based
    Software Architectures. 
Since then, REST is often held as
  the standard for usable, well-
designed, easy-to-integrate APIs. 
A little history of
  web servers
Mosiac,
NCSA
Where we are today
Apache
Created in February 1995
from the NCSA httpd 1.3
     + some patches
First official public release
   of the Apache server
    came in April 1995.
Apache has the legacy of
 being one of the most
 popular web servers.
You might also say that it is
  limited by that legacy.
How does it work?
Serving filesystem paths
/var/www/index.html
mysite.com/index.html
Where is that
behavior defined?
/etc/apache2/sites-available/
Example /etc/apache2/sites-available/default
<VirtualHost *:80>
   ServerAdmin webmaster@mattgauger.com
   DocumentRoot /var/www/
   <Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride None
      Order allow,deny
      allow from all
   </Directory>
  ErrorLog /var/log/apache2/access.log combined
</VirtualHost>
Request comes in,
spin up a thread,
form a response,
  send it back.
Apache’s method of
     scaling:
Making lots of
  threads
Threads wait for
incoming connection
They sit in a “pool”
OS-level threads
incur a penalty
Each one has an
  execution stack
that uses memory.
Context switching
between threads isn't
       free.
The result is:
Memory climbs and
climbs and the server
       runs out
Swapping to disk
 incurs a huge
    penalty
This isn’t an ideal
  way to scale
For massive
concurrency, we cannot
 use an OS thread for
   each connection.
You can throw more
  CPUs, more RAM,
and more servers at it,
but it doesn’t solve it.
Sidenote: How does
PHP work on Apache?
PHP is a module to
     Apache
It executes PHP
passed into it and
 Apache sends a
  response back
Execution stays within
       Apache
The original idea for
PHP was to fill in some
    spots in HTML.
It was a HTML
“Preprocessor”
For example,
<?php $name = “Matt”; ?>
<p>My name is
<?php print $name; ?>
.</p>
becomes
<p>My name is Matt.</p>
Before it is sent back to the
          browser.
So how do we get past
Apache’s scaling issues?
Enter
NGINX
Or just nginx
nginx can serve
HTML pages back to
     a browser
but that isn’t why
you’d want to use it.
nginx uses an
 event loop
the event loop waits
   and dispatches
events, or messages
the event causes an
event handler to be
       called
event loops can be
 highly concurrent
Its memory footprint
 stays constant as #
 of clients increases
nginx is intended to
be used as more of a
    reverse proxy
What’s a ʎxoɹd
  ǝsɹǝʌǝɹ?
Typically, reverse
proxies are used in front
    of web servers.
Connections coming in
go to the reverse proxy
          first.
The proxy may deal with
the request itself, or pass
   it off to some other
           service.
What does that all
     mean?
nginx doesn’t handle
your PHP scripts for you
You have to tell it to give
PHP scripts to something
  that can parse PHP
Like FastCGI
Here’s a default nginx
    configuration
Example /etc/nginx/sites-available/default
 server {
    listen 80 default;
    server_name mattgauger.com;

     index index.html;
     root /var/www;

     access_log /var/log/nginx/localhost.access.log;
 }
FastCGI version of the same config
server {
   listen 80;
   server_name mattgauger.com;
   index index.php;
   root /var/www;

    location ~* .php$ {
      if (!-f $request_filename) {
         return 404;
      }

        fastcgi_pass 127.0.0.1:9000;
    }
}
Anything matching the
   pattern ~* .php$
gets passed to FastCGI
FastCGI is actually a
 service running on the
webserver on port 9000
In this sense, we can say
 that FastCGI is a lot like
 an application server in
     other languages.
Like Ruby, Python, etc.
What does the app
   server do?
Request comes in from
  the reverse proxy
Or from some other means
   (another server, data
transfers, APIs, RPCs, etc)
App servers run scripts,
 but not in response to
  incoming requests
The code is always
 running, and when it
sees a request come in
It decides what it has to
   do with that request
And it puts together an
   HTTP Response
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Etc.
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
                   [blank line here!]
Response body.Neque porro quisquam est qui dolorem ipsum
quia dolor sit amet, consectetur, adipisci velit..."
Which is sent back as a
response to a browser,
or some JavaScript, or
      whatever
Notice what’s going on
        here.
We aren’t just serving
the contents of a file.
We aren’t just serving
the results of running a
PHP to fill in some parts
    of HTML either.
We’re writing an
  application
But instead of having a
commandline interface
         or GUI
This application uses
  HTTP and REST.
Web applications are
typically structured with
something called MVC.
MVC = Model, View,
    Controller
A full discussion on MVC
    will have to wait.
But the important part is
   that we’re not just
   writing index.php
And index.php isn’t just
   some preprocessor
code, filling out values in
  an HTML template.
At this point in the talk,
  I talked a lot about
where we’re going here,
    which I argue is
node.js
node.js is written on top
of the Google V8 engine
Which is cool, because
you’re writing JavaScript
   on the server-side.
node uses JavaScript’s
 callback concept to
  define applications
not just webapps;
other things are possible,
    like DNS servers.
See this great talk by
  node.js’s creator:
http://yhoo.it/e4Jx7i
I plan to write more blog
posts about webapps, web
  servers, and application
    servers in the future.
Thank you!

Matt Gauger - Lamp vs. the world - MKE PHP Users Group - December 14, 2010