HOW-TO Install PHP-FPM + MySQL 4 Nginx *TESTED PERFECT*: VPS BIBLE




new password?
login
X

This guide died.
Use Compile PHP-FPM for Nginx – Easy Install with a Script.

php-fpm logo

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Install MySQL for Databases

PHP and MySQL go pretty much hand-in-hand. Setting up, MySQL is the quick bit.


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. give MySQL an iron-clad password where prompted, then shore up with:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. 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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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 :P

So, explained below and as I say, for pretty much all of us we need this lot:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Compiling PHP with PHP-FPM using Autoconf & Libevent

Goto the download-from-source location:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

/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 :P 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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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.


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

“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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. sure, there are errors. Don’t worry about it. Just save the file for future reference:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. and hit Enter. Now this to move the file to a memorable place:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. 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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. and you’ll see confirmation:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

But just to prove the point, again run the ps command, querying the php-fpm processes using the cool grep tool:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

This time, we’ve got php-fpm’s master process and its 5 worker processes showing up (plus our irrelevant grep command again):-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. hurrah.

For reference, you can also use these commands to stop or restart php-fpm, should you need to sometime:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. 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


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

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:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. 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

22+ parts with video, here’s the index ..

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 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.