Speedy Sites: Nginx and PHP

Posted by Unknown on Thursday, February 20, 2014

In the previous article in this series, we looked at using Apache with mod_pagespeed to perform on-the-fly enhancements to decrease page load times. Getting an optimised page is only half the battle however; we need to ensure that our backend is doing as little work as possible in order to be highly scalable. In this article, we look at how we can achieve this while improving performance – all with nginx.


What is nginx?


For those unfamiliar with nginx, it describes itself as “a free, open-source, high-performance HTTP server and reverse proxy”, first released publicly in 2004, and becoming one of the most successful webservers in the past few years – their wiki has an impressive list of sites powered by it. The biggest advantage it has over the tried-and-tested Apache webserver is that it uses an event-driven, asynchronous architecture – so it doesn’t rely on threads to handle requests. What does this mean in real terms? Well, apart from predictable increases in memory usage under load, the key point for us at the moment is that it can serve static content fast. Really fast.


Many PHP installations use Apache, which is a well known and robust solution for serving PHP, but it can be a little heavy for very high volumes of traffic. Most tutorials on configuring Apache for PHP rely on mod_php, a tried and trusted implementation. But there are other options: nginx uses PHP Fast Process Manager (PHP-FPM), an alternative fastcgi engine which has numerous advantages in both logging and adaptive process spawning. Effectively, PHP-FPM gives the developer far more control and performance over PHP than mod_php does, while nginx means this speed increase applies not just to dynamic responses but to static assets too.


Sailing up the river


It’s common practice – certainly within nginx – to refer to your servers further back as “upstream”, but when thinking of your application, I like to expand the metaphor.


Picture your application as a river. Along the banks, there are docks, and each dock serves a different purpose. Manpower – your dock workers, or application resources – are finite, and each dock can only load and unload so many boats per hour.


Imagine a stream of boats coming up from the sea; the majority just want water, some need fresh food, and the very worst need repairs. Moving further upstream than necessary wastes fuel, and upsets the crew as the river gets narrower and more congested.


If you could design your docks, you would have the largest dock at the mouth of the river, provisioning the majority of the ships with water so they didn’t have to sail upstream. You would have your food (the next most required resource, and taking longer to provide) further upstream, with less workers because you had already turned around a lot of traffic by handling the most simple requests. Only the most needy ships, badly in need of repair, would venture deep into the interior to be fixed far upstream, where your most valuable workers would sit awaiting damaged vessels – the lengthiest operation.


Using this metaphor, we can see there is value in dealing with as many requests at the edge as possible; if you extend these principles to apply to your web application, then nginx can help a great deal.


Install and Configure Nginx


Our users, sailing through our application, are an odd bunch of naval folk. If we think of the average dynamic Web page, we need only one dynamic bit of content: the html itself. The rest – the CSS files, the images, the JavaScript, fonts and so on – all of these are static.

Sure, we may pull in the odd bit of data via AJAX, but in the main, our site is dealing with dozens of requests for static content for each request that requires your application to run. Why put the onus of handling those simple requests on the same server that’s dealing

with dynamic content? Why pollute our caches (or use up bandwidth allowances) fetching a file that can be fetched easily from the file system?


First we will set up an instance of nginx to help us provide a lightweight front end. First, let’s get hold of a version of nginx that supports all the things we want to make it responsible for. The default distros in CentOS and Ubuntu are fine for normal usage (and you will see benefit), but nginx doesn’t support the pluggable module system of Apache; you will need to c


Truncated by Planet PHP, read more at the original (another 18693 bytes)




more

{ 0 comments... » Speedy Sites: Nginx and PHP read them below or add one }

Post a Comment

Popular Posts