3. LAMP STACK
INTRO
‣ Simply exploding the acronym on a letter by letter
basis gives us the following elements:
‣ Linux
‣ Apache Web server
‣ MySQL database
‣ PHP
3
4. LAMP STACK
APACHE WEB SERVER
‣ The Apache HTTP Server Project is an effort to develop
and maintain an open-source HTTP server for modern
operating systems including UNIX and Windows.
‣ The goal of this project is to provide a secure, efficient
and extensible server that provides HTTP services in
sync with the current HTTP standards.
4
5. LAMP STACK
HOW DOES PHP WORK WITH THE WEB SERVER AND BROWSER?
‣ PHP is an interpreted language.
‣ This means that you will write code statements (lines of
code) and when a page is requested, the PHP
interpreter will load your PHP code, parse it and then
execute it.
‣ This is useful for web development in the fact that you
do not have to re-compile your source code for trivial
code changes and the changes have immediate effect
on all subsequent requests.
5
6. LAMP STACK
HOW DOES PHP WORK WITH THE WEB SERVER AND BROWSER?
‣ PHP is written as standard text files with the .php
extension.
‣ PHP files are often saved within a folder in a web server’s
public directory (or a web root directory).
‣ On most systems this will either be named public or
public_html.
‣ For example, if a file was saved as index.php in a web root
directory, a user could access it by typing http://
www.example.org or http://www.example.org/
index.php.
6
7. LAMP STACK
HOW DOES PHP WORK WITH THE WEB SERVER AND BROWSER?
‣ When Apache determines that the request is for a PHP
script rather than a static file, PHP sends the request to
PHP-FPM.
‣ PHP-FPM is the modern PHP FastCGI process manager
that is included with PHP. Each of your apps has its own
PHP process pool. Each process in the app's process
pool runs as the system user your app belongs to.
‣ Once PHP finishes processing the request, the
response goes back through Apache and then to the
browser.
7
8. LAMP STACK
APACHE VIRTUAL HOSTS
‣ Virtual hosts are the bread and butter of Apache.
‣ They allow you to run multiple websites off of one web
server as well as customize settings for each site.
8
10. LAMP STACK
PHP-FPM
‣ PHP-FPM (FastCGI Process Manager) is an alternative
PHP FastCGI implementation with some additional
features useful for sites of any size, especially busier
sites.
10
11. LAMP STACK
SCALABILITY
‣ Basically there are two approaches to scalability:
‣ Vertical: basically buying the biggest machine you
can so you don't need more than one;
‣ Horizontal: doing things in a way that lends itself to
parallelism. Only needed on the most intensive of
applications.
11
12. LAMP STACK
SCALABILITY BOTLENECKS
‣ If your database is slowing things down either give it more resources
or use some form of in-memory caching to take the load off;
‣ If disk I/O is your problem then the same applies;
‣ If you're running out of memory to the point where it's causing too
many page faults and thus causing a disk I/O problem, add more
memory;
‣ Does your application and it's data lend itself to partitiioning across
servers? If so, that's one way of scaling horizontally;
‣ If bandwidth is an issue and you're delivering large files then
perhaps a CDN is the answer;
‣ And so on…
12
13. LAMP STACK
SHARED HOSTING
‣ One web server and one database on same machine
‣ Application designed for one machine
13
14. LAMP STACK
TWO SERVERS
‣ Web server and database on different machines
‣ Minimal changes to code
14
16. LAMP STACK
SCALING THE DATABASE 2
‣ DB more suited for equal read/write applications
‣ Minimal changes to code
16
17. LAMP STACK
SCALING THE WEB SERVER
‣ Web server is made of “worker threads” that process
work as they come in
17
18. LAMP STACK
LOAD BALANCING
‣ Load balancing refers to efficiently distributing
incoming network traffic across a group of backend
servers, also known as a server farm or server pool.
18
20. LAMP STACK
WHAT MORE CAN WE DO?
‣ Use MariaDB
‣ One of the most popular database servers. Made by the
original developers of MySQL. Guaranteed to stay open
source.
‣ Without the query cache enabled, MariaDB had 10% better
performance up to 20 concurrent connections, while MySQL
was 25% better at 220 connections at the same time.
‣ When query cache was enabled, MariaDB was a clean winner
on all connection numbers, having more than 50% better
performance than MySQL on 220 concurrent connections.
20
21. LAMP STACK
WHAT MORE CAN WE DO?
‣ Use Varnish Cache
‣ Varnish Cache is a web application accelerator also
known as a caching HTTP reverse proxy. You install it
in front of any server that speaks HTTP and configure
it to cache the contents. Varnish Cache is really, really
fast. It typically speeds up delivery with a factor of
300 - 1000x, depending on your architecture.
21
22. LAMP STACK
WHAT MORE CAN WE DO?
‣ Use nginx for serving static content
‣ Nginx is a web server, which can also be used as a
reverse proxy, load balancer and HTTP cache.
22
23. LAMP STACK
WHAT MORE CAN WE DO?
‣ Use Redis for object & session caching
‣ Redis is an open source, in-memory data structure store, used as a
database, cache and message broker. It supports data structures
such as strings, hashes, lists, sets, sorted sets with range queries,
bitmaps, hyperloglogs and geospatial indexes with radius queries.
‣ The session handler is responsible for storing and retrieving data
saved into sessions - by default, PHP uses files for that.
‣ An external session handler can be used for creating faster and
scalable PHP environments behind a load balancer, where all
application nodes will connect to a central server to share session
information.
23
24. LAMP STACK
WHAT MORE CAN WE DO?
‣ Use CDN
‣ A content delivery network or content distribution network
(CDN) is a geographically distributed network of proxy
servers and their data centers.
‣ The goal is to distribute service spatially relative to end-users
to provide high availability and high performance.
‣ CDNs serve a large fraction of the Internet content today,
including web objects (text, graphics and scripts),
downloadable objects (media files, software, documents),
applications (e-commerce, portals), live streaming media, on-
demand streaming media, and social networks.
24
25. LAMP STACK
RESOURCE MANAGEMENT
‣ Balance how you use hardware
‣ Use memory to save CPU and/or IO
‣ Balance your resource use (CPU vs RAM vs IO)
‣ Check configurations and optimize them periodically
‣ Test and measure performance
25