Compile PHP-FPM (PHP5) & MySQL for Nginx
This guide died.
Use Compile PHP-FPM for Nginx – Easy Install with a Script.
This heavily-tested, dare I say really-rather-good PHP-FPM guide details how to setup for Nginx on Debian/Ubuntu. Completely up-to-date with nothing missing & the best configuration options for your website server requirement.
I wanted to write this tutorial a year ago (in favor of the more basic PHP/FastCGI guide that I did write) but, at the time, wasn’t convinced PHP-FPM was being properly supported.
How wrong I was! PHP have recently taken php-fpm into their core and plan to integrate it into the source code for PHP 5.4. But as we’re currently on PHP 5.2, that is still a long way off. Here’s my solution for the next year or so.
It looks complex because it is. Lucky for you though, simply bulk-copy/paste my code and you’ll be set for Nginx on Debian or Ubuntu without errors and within minutes.
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Take your virtual private server from zero to hero
.. from blank box to cute-as server ..
with this easy-to-follow copy/paste guide.
22+ parts with video, here’s the index.
101 & Scenarios
PHP-FPM vs regular FastCGI
php-fpm is recognised as an efficient management tool for FastCGI. Basically, it’s a performance booster.
For now, until PHP 5.4 at least, php-fpm has to be compiled with php. In other words, you can’t simply download it as you can with the regular fastCGI option – using sudo aptitude install [packageName].
Compile PHP-FPM 4 Nginx – Integrated vs Separate
There are two compilation methods:-
- Integrated This is the easy option. You install everything but, mostly, unnecessarily. Nonetheless, this is a method that will suit some and I’ll be publishing my tutorial shortly. It’s written already. You may wonder why I don;t publish it first? Because I recommend ..
- Separate Highly configurable and therefore more efficient. But a pain to install. This guide absorbs the hassle.
PHP-FPM vs regular FastCGI – Benchmarking?
I’ve found no recent figures but for older PHP versions have seen fpm outshine fastCGI by 3-to-2. Watch this space .. I’ll be benchmarking the two shortly.
Upgrading from FastCGI to PHP-FPM
I realise some will want to do this. Let’s not jump the gun. I’ll benchmark the two methods first.
Subject to that, I’ll lay out a guide showing how to upgrade to php-fpm seamlessly, with no server downtime.
Caching PHP
Once everything else is set up, in terms of php, we need only to add caching to help serve sites faster. Again watch this space, I’ll be looking at various caching methods shortly. As an extension to this tutorial I’ll be setting out how to install and configure Xcache in a few days time. It’s written already.
Assume Super-User root privileges. At the command line type:-
Install MySQL for Databases
PHP and MySQL go pretty much hand-in-hand. Setting up, MySQL is the quick bit.
.. give MySQL an iron-clad password where prompted, then shore up with:-
.. giving your password as prompted, then answering the self-explanatory prompts “no”, “yes”, “yes”, “yes”, “yes”! (Well, I think so anyhow.)
Install PHP Package Compilers
PHP and php-fpm need various compilation tools to build successfully:-
Install PHP-Dependent Libraries
The way I’ve designed this guide, we’ll need the following packages for php to refer to .. and they cover the bases for practically all of us wanting to serve a bunch of websites as efficiently as possible.
Then again, don’t just take my word for it, do some research, particularly if you feel you may have some special requirement.
.. To help you decide if you are an exception – and you should do this now – take a look at my explanation for precisely what we are setting up in the section below called PHP Compile Options Explained.
Come to think of it, read that anyway .. you’ll get a better idea of what we’re doing and, besides, it took a lot of work to put together
So, explained below and as I say, for pretty much all of us we need this lot:-
Compiling PHP with PHP-FPM using Autoconf & Libevent
Goto the download-from-source location:-
PHP’s Autoconf Compilation Tool
Autoconf is yet another PHP compiler, this time compiled from a source tarball or zip. PHP sulks without, specifically, the 2.13 version (the awkward sod that it is!)
You can paste this lot in one go:-
Package Compilation Syntax
I shall say this only once!
- export AC_VER=2.13 Rather than repeating the version number over and over (and me having to change it over and over when eventually I have to update this tutorial) .. I’m setting a variable for the version number using the export command. If you happend to notice that a version number has been superceded (don’t try it with Autoconf though, trust me) then simply edit the version here, the one time only
- wget http://ftp… wget downloads a file, generally zipped up – a tarball
- tar xvf file.tar.gz unzipping into a new folder
- cd autoconf-$AC_VER goto the new folder
- ./configure checks your system and sets some options into a file called Makefile
- make this is where the Makefile is compiled into a binary (or execution) file
- && just a handy way of joining two commands together
- make install the installation process. Can only be run as root which is probably the number 1 reason why Linux is excellent and Windows virus-ridden, hmmn
- cd .. goto parent directory (here to repeat the process for another package)
Install Libevent
Required for php-fpm, Libevent is an event notification mechanism. Again I’ve found some versions playing up with the php-fpm compilation process. My tip, stick to this proven release. Paste the lot:-
PHP & PHP-FPM Install (with Options & Extensions)
Here’s the crux. PHP, the heavy-lifter of lingo parsers, coupled with its exciting new sidekick PHP-FPM aka FastCGI Process Manager. Paste this lot:-
/etc/php is the folder we’ll be adding our PHP configuration file php.ini, by the way.
OK, here’s my heavily-tested ./configure command .. you really owe me a beer for this one
But ..
.. before that, let’s look at the detail of what we want to achieve with this command ..
PHP Compile Options Explained
For the full list of PHP configuration options to choose from:-
Let’s go over what I’ve included and why.
There can be no perfect generic build because our needs may be slightly different but, IMHO, what I’m including will suit the vast majority of users requiring an efficient and secure solution to serve up php-driven websites.
That said, a recompile is always possible down the road if you need to change any configuration options, and I’ll explain how to do that another time.
Here are my compile time options. The asterisks highlight those absolutely required for my php-fpm install method to work with the Nginx web server. For each I specify any required dependency packages (as installed above using aptitude install [package]) that they rely on, what each does and, in case you’re really keen, a link to further reference.
EDIT: I’ve tweaked these options. For some reason, MySQLi wasn’t working without MySQL so I’m including both extensions. MySQLi, meanwhile, now uses PHP’s Native Driver (mysqlnd).
Also, instead of copying over a generic php.ini file we create a new one, adding additional rules.
If you feel you don’t need some option then just leave it out of the ./configure command, also omitting to install any relevant dependency package. Obviously, those items marked as required aren’t optional.
Note: Dependencies will automatically install with any further required packages.
Have a suggestion? Disagree? Please let me know.
Right. The uber-command! Paste:-
“Thank you for using PHP.”
.. If you didn’t get that you’ve done something wrong. Don’t blame me, I’ve tested this entire procedure about 6 times and it is flawless. In fact, it’s so damn hot it got me punching the air. Maybe you deviated from my suggested package versions? You are using Ubuntu or Debian, huh? This is untested on anything else, although only a few tweaks would be required.
Right, install the thing. This won’t be fast, about 5 to 10 minutes. Just the one sugar for me, thank you:-
And test your installation. Again, will take 5 to 10 minutes .. maybe open a second terminal to carry on with other stuff while this runs off its nearly 9000 tests:-
.. sure, there are errors. Don’t worry about it. Just save the file for future reference:-
.. and hit Enter. Now this to move the file to a memorable place:-
Tweak the PHP.ini File
Nearly there. Let’s create and tweak PHP’s configiration file, the php.ini.
Don’t confuse this with the automatically created php-fpm configuration file which, for the record, lives on the path /etc/php-fpm.conf and which we configured in that long php ./configure command.
Create the file:-
Most of the settings we want, we create with our compilation command. All we’re going to add is our timezone. To find your’s, check here http://nl3.php.net/manual/en/timezones.php :-
Amending my continent/city with yours and pasting something like:-
Save and close the file.
Start PHP-FPM
Kick up the FastCGI management processes to get this PHP solution running.
Optionally, using the ps – the “process” command – is a good way of testing that this works:-
.. so there’s nothing there yet, bar the grep php-fpm command we just started as a process, er, to find out about other processes (that aren’t running yet!)
So we’ll start up php-fpm while also adding initiation hooks so that in future, after a system reboot, fastCGI kicks up automatically:-
.. and you’ll see confirmation:-
But just to prove the point, again run the ps command, querying the php-fpm processes using the cool grep tool:-
This time, we’ve got php-fpm’s master process and its 5 worker processes showing up (plus our irrelevant grep command again):-
.. hurrah.
For reference, you can also use these commands to stop or restart php-fpm, should you need to sometime:-
.. and seeing as I’m spieling off about some of this dull stuff, the /etc/init.d directory is where all your initiation scripts live, for anything that you want started automatically on a server reboot, else to start/stop/restart on demand (using the same syntax /etc/init.d/nginx restart, for example. php-fpm’s init script was installed there when we issued the command make all install.
Enough already!
Shed Super-User
Analysing PHP with Phpinfo
When you’ve added Nginx and set up a site you’ll be able to peruse your php configuration, detailing all those PHP extensions we installed, by adding some-page.php and inserting within:-
.. before browsing to the new page in your browser to read the results.
For your security, when you’re finished with that information, delete the phpinfo page or, at least, delete the command to show this information (which is a goldmine of info for hackers.)
Caching In with Xcache, Security with Suhosin
PHP is done but we can and must improve it further with a couple of third party extensions which we install using the third party installer called phpize:-
- Caching In the next tutorial we’ll install Xcache tailored for this PHP configuration.
- PHP Protection Also coming up, we’ll add the respected Suhosin extension, shielding against PHP server vulnerabilities.
Cue index ..
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Take your virtual private server from zero to hero
with this easy-to-follow copy/paste guide.
“My local PC runs Windows” Show me for Linux/Mac
“My local machine is Linux/Mac” Show me for Windows
22+ parts with video, here’s the index ..
-
Setup Unmanaged VPS: The Ubuntu-Nginx Guide
Introducing vpsBible’s how-to guide for setting up a super-charged unmanaged VPS featuring Ubuntu and Nginx.
-
VPS (Virtual Private Server) vs Shared vs Dedicated
Before getting stuck in, let’s compare the web host types in detail and weigh up the differences between managed & unmanaged VPS.
-
Set Up a VPS Linux Distribution
Opening a VPS hosting account, we’ll choose, configure & launch a Linux server ‘distribution’.
-
Set Up Command Line Interface (CLI) using PuTTY
Now we’ve got a server to play with, we’ll connect to it, using the ’ssh’ protocol for a super-secure link.
-
Create a Linux User & Set Permissions
Initially you’ve logged into your remote Linux machine as ‘root’. We’ll add you as a user now, with root or SuperUser permissions.
-
Encrypt Data with OpenSSH & Auto-Login with PuTTY
Now we can use ‘authentication keys’ to shore up our connection while simplifying login to an automated, password-free yet secure process.
-
Harden the Secure Shell (SSH) & Create a Firewall
The final security step: galvanizing the OpenSSH protocol and setting up an ‘iptables’ firewall. Now the server is rock ruddy solid.
-
Edit bashrc for User-Friendly Linux, plus System Updates
We use the terminal a whole lot so let’s cut ourselves a break and create some command shortcuts, then update the server.
-
Add a Domain Zone to Your VPS
To make your server aware of yourDomain.com you’ll need a few ‘DNS’ records. Damn this is simple I’m practically asleep!
-
Prepare Linux Server for Email with Postfix
In order to send and receive email from our web applications we’ll install some software and tweak the ‘RDNS’ record.
-
Install PHP5 & MySQL – The One-Stop FastCGI Index Page
Add PHP5 & MySQL, but which FastCGI method is best? Here’s the benchmarking, 4 ways to install/compile, add caching modules & more.
-
Nginx (better than Apache) Web Server
Let’s install this supersonic web server, tweaking its file structure & adding default configuration files.
-
Setup FileZilla for Secure FTP (SFTP)
There’s FTP, and then there’s ‘Secure FTP’. We’ll set up FileZilla using SFTP – and those authentication keys – for secure file transfers.
-
Serve Multiple Sites & Blogs with Virtual Hosts
We want a website folder structure, some permissions, some site-specific configuration files and, hey, we’ll hook up ‘FastCGI’.
-
Adding Sub-Domains with Nginx
Need a sub-domain? If you do, here’s how to add subDomain.yourSite.com with an ‘A record’ and a configuration file.
-
Integrate A CMS Like WordPress, WP MU, Drupal etc
For those of you wanting to use a specific content framework, such as a CMS, blogging or forum software, pop in here and we’ll sort that out.
-
Google Apps for Domain-Specific Email
GA’s free Standard Edition for catchall webmail – POP-ping or IMAP-ped to your desktop client – saves precious server resources for your webs.
-
Moving Day! How to Move Your Blog or Site
Nearly there. Here’s the web site or blog migration guide, relocating to your VPS with minimal or no downtime.
Manage Unmanaged VPS: Ubuntu-Nginx Administration
Already set it up? We’d best maintain it then.
Toggle to the .. Ubuntu-Nginx Admin Index
Manage Unmanaged VPS: Ubuntu-Nginx Administration
Maintain your virtual private server with ..
.. cheatsheets, tutorials, tips & guides.
Head back to the .. Ubuntu-Nginx Installation Index
-
Nginx Control Panel: Here’s the Deal
You’re migrating shared-to-VPS and want a GUI like cPanel? No you don’t, not after you read this! What’s more, you don’t need one either.
-
Nginx Control Panel Workarounds
For every control panel module there’s an equivalent (and faster) terminal command. Let’s compare these.
-
Upgrade to Nginx Latest Version with ZERO Downtime
Keep Nginx up-to-date – and optimized for security. This guide shows precisely how *with no missed server requests*.
-
Compile an Advanced Nginx Module Configuration
Need special web server functions? Here’s how to install or upgrade Nginx with bespoke required modules & parameters.
-
Add Web Sites Nginx Cheatsheet
Once you’ve set up your first site, adding more gets easier. Especially when you refer to this. (Let’s face it, I’m just too good to you.)
-
Adding Sub-Domains with Nginx
Rather than add somesite.tld, maybe you want some.subsite.tld? Here’s how to set your A record using a DNS manager and the vhost file for Nginx.
-
Add/Delete MySQL Database from Command Line
Need a database? Want to bin an old db? This tutorial shows you how using the terminal command line console whatnot.
-
Configure phpMyAdmin for Nginx to Maintain MySQL
Rather than have to crunch databases via the command line, we can make life easier by adding this splendid tool to manage them.
-
Backup/Export MySQL DB with CLI & mysqldump
Here’s how to backup a database from the command line using the handy mysqldump utility.
-
Import MySQL Database using Command Line
Import data to a new database with this simple mysqldump guide .. great for reinstating an old backed-up db, for instance.
-
Batch Replace/Delete Data in MySQL Database
Change repetitive db data with a single command. Rather than manually find & alter each instance, isolate the lot & update multiple content.
-
LOST a MySQL Password? How to RESET it
Lost your MySQL password? That’s a bit of a bummer .. or it would be if you didn’t have this new password setup guide, huh.
-
Backup or Upload with Secure Copy SCP (Linux to Linux)
Never mind FTP m8! Save time and download/upload your files using encrypted SCP (secure copy), the #1 Linux-Linux file transfer solution.
-
Backup or Upload with PuTTY PSFTP (Windows/Linux)
Equivalent to the above, how to transfer your files if you’re unlucky enough to be running Windows for your local PC. Not that I’m biased.
-
*USEFULLY* Park A Spare Domain with Nginx
Got a spare domain name sat around doing nothing? Put it to some use, have it point its traffic at your primary domain. Here’s the deal ..
-
Password Protect Nginx Web Files/Directories
Maybe you want a hidden website directory, say, for projects, or are developing a site and want it off-limits ’till launch day. Top tip ..
-
Redirect Web Pages with Nginx Rewrite Rule
Here’s the syntax to point a site or blog to another domain. That way, for instance, traffic from a passive site benefits an active one.
-
Nginx Error & Access Logs
Need to troubleshoot some error, or want to find out who’s trying to hack your site? Here’s a start .. those tell-tale logs.
-
Subversion for Easy CMS Updates ie WordPress/Drupal/etc
A brief introduction to install and know about the power of Subversion, the version control system to keep scripts and platforms up-to-date.
-
You Tell Me …
Something missing? Probably. Let me know what you’d like included. And if it’s listed below, shout out anyhow and I’ll try to bump it.
Nginx Admin: In the Works ..
This lot’s marked for addition already:-
- Setup or Edit DNS using Bind
- Network Tools Troubleshooting Guide
- The Comprehensive Permissions Guide
- Configuring Nginx Rewrites
- Custom Website Error Page
- Setting up Cron Jobs
- Rsync for Incremental Remote-to-Local Backup
- Cron & Rsync for Automatic Backup
- Cron & mysqldump for Auto DB Backup
- Safeguard Bandwidth with Hotlink Protection
- Block Access with Nginx’ IP Deny
Got a Question? Want to Comment?
This site has no comments system. We've got a better system!
Please use the link at the top of the post and goto this topic's forum thread:-
- to help build community
- for a superior knowledge base
- to keep info in one place, not diluted between forum & comments.
For those posts ported from Guvnr.com (pre-Feb/2010), archive comments remain.
Handy info from these comments is in the process of being ported to the forum and attributed to the original author.
If you think this idea sucks, let me know.
Thank you.






















SIMPLE! Need to Recompile PHP from Source Installation? VPS BIBLE - vpsBible April 15th, 2010 at 8:05 pm
[...] now give the configure command with your options. This is the options set I use in the tutorial PHP-FPM & MySQL on Nginx, for [...]
Compare FastCGI, Spawn-FCGI & PHP-FPM for PHP5/Nginx: VPS BIBLE - vpsBible September 20th, 2010 at 6:25 pm
[...] PHP-FPM (separate) [...]