Learning Objectives – after this chapter you will be able to:
- Explain what open source software is and why it matters for web development.
- Describe the history of the Open Source Initiative (OSI).
- Explain the role of Apache, MySQL/MariaDB and PHP – the AMP stack – in a dynamic web site.
- Trace how a browser request travels through Apache, PHP and the database and back.
Brief Introduction to PHP, Apache and MySQL
PHP, Apache and MySQL are all part of the open source group of software programs. The open source movement is a collaboration of some of the finest minds in computer programming. By allowing the open exchange of information, programmers from all over the world contribute to make a truly powerful and efficient piece of software available to everyone.
Through the contributions of many people to the publicly available source code, bugs get fixed, improvements are made, and a good software program becomes a great one over time.
| Component | Role in a web site |
|---|---|
| Apache | The web server – receives requests from browsers and sends back pages. |
| MySQL / MariaDB | The database server – stores data in tables and answers SQL queries. |
| PHP | The server-side scripting language – runs on the server, talks to the database and builds the HTML that is sent to the browser. |
A Brief History of Open Source Initiatives
The term open source was coined in 1998, after Netscape decided to publish the source code of its popular Navigator browser. This announcement prompted a small group of software developers, long-time supporters of the open source ideology, to formally create the Open Source Initiative (OSI) and the Open Source Definition.
- Although the OSI ideology was first promoted in the hacker community, after the release of Navigator's source code programmers from all walks of life began to offer suggestions and fixes to improve the browser's performance.
- Linux became the first operating system that could be considered open source (BSD, distributed from Berkeley in 1989, was a close runner-up), and many programs followed soon after.
- Large software corporations began to offer versions of their programs that worked on Linux machines.
- Any software that carries the OSI-approved licence can be considered open source, because it has passed the Open Source Definition test.
Why Open Source Rocks
Open source programs are popular with developers because:
- They are free. Open source software is available to the general public. Developers volunteer their time to improve existing software and to create new programs. By definition, open source software cannot require any licensing or sales fee.
- They are cross-platform and technology-neutral. Open source programs must not depend on "any individual technology or style of interface". As long as the software can run on more than one operating system, it meets the criterion – so it is usable by virtually everyone.
- They must not restrict other software. If an open source program is distributed along with other programs, those programs may be open source or commercial. This gives developers maximum control and flexibility.
- They embrace diversity. Open source programs cannot discriminate against any person, group or "field of endeavour". A program designed for medical use cannot be limited to that field if someone else wants to modify it for their own needs.
For the complete list of criteria a piece of software must meet to be called "open source", see the Open Source Definition on the OSI web site: opensource.org/osd.
How the Pieces of the AMP Module Work Together
Imagine that your dynamic web site is a fancy restaurant. Diners come to your place and each one wants something different and specific. They do not worry about how the food is prepared, as long as it looks and tastes delicious. Unlike a buffet, where everything is laid out and patrons simply choose from what is available, a good restaurant encourages patron–waiter interaction and complete customisation. Similarly, a web site should not be a static page with little interaction; it should be a dynamic site where the visitor chooses what he or she wants to see.
In this scenario the three components of the AMP module are:
| Component | Restaurant role |
|---|---|
| Apache | The chef – a highly trained master of culinary arts. Whatever people ask for, she prepares quickly and without complaint. Apache, the HTTP server, parses the requested files and passes on the results. |
| PHP | The waiter – takes the request from the patron and carries it to the kitchen with specific instructions about how the meal should be prepared. |
| MySQL | The stockroom of ingredients – in this case, information (data). |
When a patron (a web site visitor) sits down and orders a meal with specific requirements – a steak, well done – the waiter (PHP) takes that requirement back to the kitchen and passes it to the chef (Apache). The chef goes to the stockroom (MySQL) to fetch the ingredients (data), prepares the meal and presents the final dish to the patron exactly the way he or she ordered it.
http://localhost/shop/product.php?id=7 or submits a form..php extension and hands the file to the PHP engine.Figure 1.1 – The life of one request in the AMP stack
You can install one, two or all three components of the AMP package, depending on your needs. If you are responsible for a company intranet or host your own web site, you will usually install all three. If your site is hosted by a third-party hosting company, these components are already installed on the server for you.
Installing all three components on your own computer lets you develop and test your site in the comfort of your own workspace, without uploading to a server after every small change. Even after a lot of offline testing, always perform a complete test once your site is live, because the settings of your hosting company's server may differ from yours – and even a small difference can cause big headaches. The easiest way to install the complete AMP stack on one machine is XAMPP, described in Chapter 2.
Apache
Apache acts as your web server. Its main job is to parse any file requested by a browser and return the correct result according to the code in that file. The Apache HTTP Server is free, powerful and flexible, and can accomplish virtually any task that a webmaster requires. XAMPP 8.2.12 ships Apache 2.4.58.
Important features of the Apache HTTP Server include:
- Password-protected pages for a multitude of users.
- Customised error pages (for example, your own 404 Not Found page).
- Content negotiation – serving the version of a document the browser can best accept.
- Usage (access) and error logs in multiple, customisable formats.
- Virtual hosting – many web sites (domains or IP addresses) served from one server.
- Directory index directives (for example
index.php,index.html). - URL aliasing and rewriting (
mod_rewrite), with no fixed limit.
Apache can host a web site for the general public, a company-wide intranet, or simply a test copy of your pages before they are uploaded to a production server.
PHP
PHP is a server-side scripting language that allows your web site to be truly dynamic. PHP stands for PHP: Hypertext Preprocessor – a recursive acronym. Its flexibility and relatively small learning curve (especially for programmers who know C, Java or Perl) make it one of the most popular scripting languages on the web. Businesses and individuals everywhere use it, and its benefits clearly outweigh the costs – it is free and open source.
This book uses PHP 8 – the version shipped with current XAMPP releases. You can download PHP and read the complete function reference at www.php.net.
MySQL and MariaDB
Another open source favourite, MySQL is the database server that lets PHP and Apache work together to store, access and display data in a readable format in the browser. It is a Structured Query Language (SQL) server designed for heavy loads and complex queries. As a relational database system, it allows many tables to be joined together for maximum efficiency and speed.
MariaDB is a community-developed fork of MySQL, created by MySQL's original developers. It is a drop-in replacement – the same SQL, the same tools and the same PHP functions work with both. Since XAMPP 5.5.30/5.6.14 (2015) XAMPP ships MariaDB in place of MySQL, but the Control Panel still labels it "MySQL".
Popular features of MySQL/MariaDB:
- Multiple CPUs usable through kernel threads.
- Multi-platform operation (Windows, Linux, macOS …).
- Numerous column types that cover virtually every type of data.
- Group (aggregate) functions for calculations and sorting.
- Commands that show information about databases quickly and clearly to the administrator.
- Function names that do not clash with table or column names.
- A password and user-privilege system for security.
- Many indexes per table, supporting very large tables with millions of rows.
- Error messages available in many languages.
Key Takeaways
- Apache (web server), MySQL/MariaDB (database server) and PHP (server-side language) form the AMP stack.
- All three are open source: free, cross-platform, non-restrictive and open to everyone.
- The term "open source" was coined in 1998; the OSI maintains the Open Source Definition.
- In a request, Apache receives it, PHP runs the script and queries the database, and the browser receives only HTML.
- XAMPP ships MariaDB, a drop-in replacement for MySQL.
Question / Answer (15 questions)
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes on this page are the author’s own compilation from the e-book PHP: Basics to Advance. Every program was run on Apache with PHP 8 and MariaDB, and the output shown is the real result of that run. The works listed below were consulted and are cited for attribution and further reading. Product names and trademarks belong to their respective owners.
- Open Source Initiative. (n.d.). The Open Source Definition. https://opensource.org/osd
- The Apache Software Foundation. (n.d.). Apache HTTP Server Version 2.4 Documentation. https://httpd.apache.org/docs/2.4/
- The PHP Group. (n.d.). PHP Manual – What is PHP? https://www.php.net/manual/en/intro-whatis.php
- MariaDB Foundation. (n.d.). MariaDB Server Documentation. https://mariadb.com/kb/en/documentation/
- Wikipedia contributors. (n.d.). XAMPP. Wikipedia, The Free Encyclopedia. https://en.wikipedia.org/wiki/XAMPP