I've often described Well House Consultants as a "mom and pop" company - a small outfit where we provide a niche service (IT Training
and a business hotel in Melksham
). We make heavy use of PHP on our web sites, and it serves us very well, being quick and easy for us to code and running well on our servers. But is it scalable? What if we wanted to move on from our "mom and pop" system to a system that's good across a whole big international organisation?
Here's the archirecture of a straight forward system, using simplest design and coding principles:
The (orange) server box receives requests from the web site visitor's computer (the browser), and reads the PHP files of fthe disc as required. PHP is what I describe as an "HTML++ language" - in other words, the code is embedded within the HTML in the form of extra tags ... the server then takes those extra tags, runs the code that's in them, and sends the result of running that code back to the browsers, in the course of which data associated with the code that's running may be saved to / read back from the disc.
You may think that model would work fine for our little company, but actually we've moved on. Let me show you how.
For starters, the original model mixes up the look and feel of the web site with the business logic. In other words, the HTML directives on formatting the output and the calculations to extract data from its source were all combined into a single lump, meaning that both Lisa (on graphic art) and myself (on coding) had to get involved with the same files, and also meaning that a look and feel that could be shared across numerous pages had - in the simplest of models - to be duplicated. And that gave rise to maintainance issues, and an awful lot of work if things were to be changed. The first stage of improving this, by separating out common elements into shared files, is not
illustrated by diagram here - the second diagram goes on to show the second stage of separaration.
By splitting the web site / page elements into three or four distinct sections, each with a clean and clearly defined interface to the others, different elements can be maintained by people with different skill bases, and different applications can more easily share common templates, and ./ or different data sets and their access. Sometimes you'll hear this described in PHP terms as the "4 layer model", where you have the top level controlling the general flow of URLs to code, the business logic which looks after the data, the web helpers which provide data to web transposition, and the template which provides the standard look and feel into which results are "mailmerged". And that's refined further into being desribed as "MVC" - Model, view, controller - see [here]
The second illustration in this section (the one just above) shows our server split out into Model, View and Controller - together with "Router" which hasn't made it into the acronymn, but fulfills the vital role of converting the incoming request (the URL and its data) into a decision as to which piece of program to run, and what and how to pass the data in. In other words, the router will make decisions like converting a URL such as
into a request to run the script called
with an extra parameter string
But there's a great commonality of approach in what I've described above in just about every
web application. Most applications require sticky fields
- data cells where the data is validated when submitted, and echoed back into the same field when it's found to be invalid. Most applications require a continuity of data from one page to another - sessions. Most applications require the taking of data from the model to allow admin user edits, and then saving back, modified, into the data store. And rather than the programmer have to write these for each and every application, the common current approach is to use a Framework
. The most commonly used Framework in the PHP world in Zend
... the concept is also present if you use other languages on your web site - you many have head of / used Django
if you're programming in Python or Rails
if you're Ruby based.
Your data isn't typically going to reside totally within and be directly handled by your model code. Whilst you could
write data file handlers within your PHP or have them included within the Framework, it's more logical to use an already-written and well tested system which itself includes all sort of things like record locking, easy data changes, data intergrity validation and a good access system to make programmatic access esay. And the dominant such system for quite a while was / has been SQL - a relational database system where the data is stored in tables, tables are joined
to each other to avoid the need to repeatedly hold common data, and the internal format is such that simple edits can be done efficiently without big file rewrites. "SQL" - the Structured Query Language - has a number of flavours and implementations; you're most likely in the Open Source world to come across MySQL, but you may also come across PostGRESql, Oracle, Microsoft's SQL Server and others. All of these SQL engines run as a separate service that's typically accessed by the model element of your PHP MVC code, and the frameworks often include standard logic to help you map database records into PHP objects and vice versa.
As an aside (not on the diagram), you may also come across SQLite - which is a lightweight SQL database with a different structure. SQLite does not run as a separate server - it's a library that's built into your PHP (or other language) and you directly pass it SQL directives which it has implemented to run on local files, giving you many of the benefits of SQL over plain text files, but without a separate server which (frankly) can be overkill for many applications.
Looking wider than SQL, there are other ways that the model can access underlying data behind the model which may sometimes be more appropriate. A key-value store or a NoSQL database (such as MongoDb) may work better than an SQL system for some applications / users, and for more complex requirements a whole further service layer may be the better solution. You're looking at SOAP, at XML, at RESTful technology here, where a library within the model contacts a further server - usually via an http or https connection. So in effect your server becomes a browser to a second level server. Very often, these second level servers are running Java applications to look after the data ...
Many of the major web applications these days - Twitter, Facebook, Google Maps, and Flickr to name but a handful provide their own "API" - Application Programmer Interface. In essence, these APIs are server based interfaces, using http and/or https, to make requests and get results from back end servers so that the web applications can be used through your own applications as well as directly through your browser. Usually, authorisations / keys are required, and you're advised to cache results that are retrieved.
This is all very well in concept, but how does one design, experiment with, and test these more complex web based systems. We've moved a very long way indeed from a web site that's providing a static page of information into a full application in many cases, and we can't just go working on and chaning the live server in the middle of the night. So - in this diagram, we've added a second server. This is being used for testing and development - trying things out, making sure that no code works, with access (read only, one hopes!) to common back end servers.
Not shown in this diagram - you may have development and test servers, and with those servers you'll also have resource reporistories such as SVN (SubVersioN) to allow your developers to be able to check out and check in code changes, secure in the knowledge that their colleagues aren't amending it in parallel. Such a system also allows rollback to see the code at various release levels, so that your developers have an easy route to troubleshoot older versions if they're working on code that's common across a whole range of sites.
But where are your developers? London, Manchester or Glasgow? Or San Diego, Bangalore or Amsterdam? And will they have quick and easy access to your test and development servers, or will those servers be "firewalled" at your networking centre in London / Chicago / Fremont and hard for your developers to access?
Modern laptops are very powerful - absurdly powerful - and an excellent option these days is to run your server software - at least the first level - on your own machone. You can then research and develop locally in the USA, the Netherlands, India or up a Welsh mountain without the need for a fast VPN connection back to HQ.
Virtual Machines (using software such as Virtual Box from Oracle, or VMWare) allow you to reproduce the front end server's environment on your local machine - be it a PC or a Mac - and give you your own testing environment. And application configurations can be used to control whether you're going to access backend services from within your own organisation, Twitter and elsewhere ... or whwthe you'll run those on your local machine too.
This virtual machine approach is sometimes referred to as a "sandbox"; you have a separate part of your own computer runningan independent copy of the environment that's on your main company's server, yet with no need to be linked in to it while you're testing, and with no need to worry about the effect on other users while you develop, experiment, try things out, do timing trials and all of the rest. The main server carries on serving your company's customers, irrespective of infinite loops and gross coding errors that you might occasionally make, and you're no longer in an environment in which you need to time your work to happen at the times of day that resources are available.
In the final diagram, I show the system as it might be set up for a larger organisation. But here's an interesting question - "what do WE do at our expanded mom and pop outfit"
, where last week we had 22,000 different visitors to our two main web sites, between them accessing 62,000 pages. Other statistics include total log file size of around 350 Mbytes for the week, and over 1 million different individual requests of the server - these are the sorts of stats that salesmen will quote and they're impressive not not very useful!
It turns out that - even at our small size - it's worth our while to be using the same practical architecture. I have a sandbox copy of Virtual Box, running Centos Linux, Apache, PHP, Zend Framework and MySQL on my MacBook Air (yes, really) and I can even develop and test in the train as I travel - including in Standard class with the horrid seat pitch. And for our other site, which uses a much simpler model, we're running Apache httpd with PHP, and MySQL, directly on our own MacBook Pros.
Back end services? Yes - we use those ourselves too. You'll find me picking up services from Twitter
, Stop Forum Spam
, Journey Check
, The Highways Agency
, and the BBC
. For some I'm using their own APIs, for others MagpieRSS, and others are using the PHP file_get_contents
These subjects are covered (to some extent) in our Deploying LAMP
and PHP techniques
courses. However, they are very much "crossover" topics that may not fit neatly into any one course - please email me
with your specific requirements so that I can give you best training advise. (written 2012-12-16, updated 2013-01-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesA603 - Web Application Deployment - Further httpd Configuration 
Identifying and clearing denial of service attacks on your Apache server - (2014-09-27) 
Helping search engines with appropriate 400 error codes - (2013-02-11) 
Forwarding a whole domain, except for a few directories - Apache http server - (2012-09-17) 
Parse error: parse error, unexpected T_STRING on brand new web site - why? - (2012-03-03) 
Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19) 
An image from a website that occasionally comes out as hyroglyphics - (2011-01-14) 
Redirecting a page - silent, temporary or permanent? - (2010-08-03) 
How did I do THAT? - (2009-10-26) 
Monitoring and loading tools for testing Apache Tomcat - (2009-07-07) 
Database connection Pooling, SSL, and command line deployment - httpd and Tomcat - (2009-03-01) 
Moving a directory on your web site - (2009-01-03) 
How to avoid duplicating web page maintainance - (2008-12-20) 
mod_rewrite for newcomers - (2008-12-20) 
mod_proxy_ajp and mod_proxy_balancer examples - (2008-12-13) 
Pointing all the web pages in a directory at a database - (2008-08-30) 
mod_proxy and mod_proxy_ajp - httpd - (2008-08-22) 
WEB-INF (Tomcat) and .htaccess (httpd) - (2008-08-20) 
Configuring Apache httpd - (2008-07-12) 
What to do if the Home Page is missing - (2008-05-08) 
User and Group settings for Apache httpd web server - (2008-04-22) 
Strange behaviour of web directory requests without a trailing slash - (2008-03-06) 
Default file (MiMe types) for Apache httpd and Apache Tomcat - (2008-03-04) 
Online hotel reservations - Melksham, Wiltshire (near Bath) - (2008-02-24) 
Which modules are loaded in my Apache httpd - (2008-02-23) 
Using a MySQL database to control mod_rewrite via PHP - (2007-10-06) 
Load Balancing with Apache mod_jk (httpd/Tomcat) - (2007-10-02) 
.php or .html extension? Morally Static Pages - (2007-09-17) 
Compressing web pages sent out from server. Is it worth it? - (2007-09-14) 
Simple but effective use of mod_rewrite (Apache httpd) - (2007-05-27) 
Sharing the load with Apache httpd and perhaps Tomcat - (2007-03-29) 
httpd.conf or .htaccess? - (2007-02-14) 
Passing GET parameters through Apache mod_rewrite - (2006-12-27) 
Clustering, load balancing, mod_rewrite and mod_proxy - (2006-11-21) 
To list a directory under httpd on a web server, or not? - (2006-09-02) 
Using different URLs to navigate around a single script - (2006-06-11) 
An unhelpful error message from Apache httpd - (2006-03-30) 
Denial of Service ''attack'' - (2006-03-17) 
Apache httpd to Tomcat - jk v proxy - (2006-03-03) 
2006 - Making business a pleasure - (2006-01-01) 
Apache httpd - serving web documents from different directories - (2005-12-12) 
Separating 'per instance' data from binaries and web sites - (2005-10-16) 
Spotting a denial of service attack - (2005-06-12)H114 - Sourcing, installing and configuring PHP 
Adding a PHP build option, rotating an image based on camera data, and a new look at thumbnails in PHP - (2015-02-22) 
PHP - some quick fixes if scripts have issues, and how to use our support - (2014-11-19) 
Virtual Hosting with Apache http server - an overall scheme, and avoiding common pitfalls - (2011-01-14) 
Compile but do not run PHP - syntax check only - (2010-02-22) 
Your PHP code does not work? Here is where to start looking. - (2010-01-18) 
Controlling, supressing, enabling PHP error messages - (2009-10-02) 
How to make a Risotto (PHP build style) - (2009-08-02) 
Quick Summary - PHP installation - (2008-12-12) 
What is built in to this httpd and PHP? - (2008-08-23) 
Apache httpd, MySQL, PHP - installation procedure - (2008-08-01) 
cannot restore segment prot after reloc message - PHP / httpd - (2008-06-26) 
Checking server performance for PHP generated pages - (2008-06-06) 
PHP upgrade - traps to watch - (2005-10-31) 
Development Environments - (2005-05-19) 
Current MySQL and PHP paths and upgrades - (2005-01-28)H307 - PHP - Web2 and caching 
Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20) 
How do I post automatically from a PHP script to my Twitter account? - (2013-07-10) 
Web server efficiency - saving repetition through caches - (2013-05-30) 
Further recent PHP examples - (2013-04-28) 
Using web services to access you data - JSON and RESTful services - (2013-03-29) 
Handling failures / absences of your backend server nicely - (2013-02-08) 
On this day ... one PHP script with three uses - (2011-09-26) 
How to add a customised twitter feed to your site - (2011-02-27) 
Setting your user_agent in PHP - telling back servers who you are - (2010-12-18) 
PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04) 
Scraping content for your own page via PHP - (2009-12-21) 
Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04) 
New Example - cacheing results in PHP for faster loading - (2009-05-24) 
Automated server heartbeat and health check - (2009-01-16) 
Flash (client) to PHP (server) - example - (2008-12-06) 
Ajax - going Asyncronous and what it means - (2008-09-28) 
Starting Ajax - easy example of browser calling up server data - (2008-09-27) 
memcached - overview, installation, example of use in PHP - (2008-08-02) 
Exchange Rates - PHP with your prices in your users currency - (2008-05-19) 
Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
Some other Articles
Testing code coverage (have I tested everything?) in PHPTesting classes for your PHP website with PHPUnitThe week before ChristmasZend / layout of MVC and other files in an example application (PHP)Building up from a small PHP setup to an enterprise one Lesson 1 in programing - write clean, reuseable and maintainable tidy codeObjects in PHP - Revision PHP revision ... by example. Christmas Bus Schedule for Melksham - First Bus10 reasons why I travel the night before a course starts