PHP 5.3 FPM + Nginx Walkthrough


The new way to deploy PHP is using the FPM extension, that is available on PHP’s core since the branch 5.3. While as today there’re a few other posts detailing how to install and configure nginx to run it, I found all of them targeting to a more advanced user, and since I’m learning the basics of managing my own server, I wrote a “how to” for myself and decided to publish it here. Hopefully other people struggling to install will it find this simple tutorial useful.

Compiling PHP 5.3 with FPM

Since I’m not experienced with large php projects, I decided to search for the most common php extensions and compile with all of them.

sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libevent-dev
sudo apt-get install libxml2-dev libssl-dev
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libjpeg-dev libpng-dev
sudo apt-get install libmcrypt-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install snmpd

After that, download the latest PHP and unzip it:

tar -xvf php-5.3.3.tar.bz2
cd php-5.3.3

Now, you have to make sure that you buildconf is executable, remove the the existing configure file, and compile it:

chmod +x buildconf
rm configure
./buildconf --force
./configure --enable-fpm --sysconfdir=/etc/php5/conf.d --with-config-file-path=/etc/php5/conf.d/php.ini --with-zlib --with-openssl --enable-zip --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-soap --enable-sockets --disable-cgi --with-curl --with-curlwrappers --with-gd --with-mcrypt --enable-memcache --with-mhash --with-jpeg-dir=/usr/local/lib --with-mysql --with-mysqli --enable-pdo --with-pdo-mysql --with-pdo-sqlite --with-sqlite--with-xmlrpc --with-xsl

If you get any errors here, it could mean that you don’t have the required extension installed. Check the error message, google for the right package and install it using “sudo apt-get install …”. Or, you can decide that you don’t really need it and simply remove the extension from the “configure”.

Now is time to compile, run the tests and install it on your system:

make test
sudo make install

If you don’t fell like waiting a few extra minutes, you can skip the “make test”. If you get errors regarding the mysql extension, AFAIK, it’s normal and the problem is that the test code tries to connect to your localhost mysql server with a default password and if you don’t have it set up that way, it fails.

PHP-FPM and PHP config files

Now you have to copy the default configuration files to the right places. Please note that the paths I’m using here are these because the way I configured it(–sysconfdir=…).

sudo cp /etc/php5/conf.d/php-fpm.conf.default /etc/php5/conf.d/php-fpm.conf
sudo cp php.ini-production /etc/php5/conf.d/php.ini #run this from your extracted php folder

You have to change a few defaults and others required values in the php-fpm.conf file. You are advised to read it quickly, so you know what is possible to change. Here’re the stuff I changed:

pid = /var/run/
error_log = /var/log/php-fpm.log
group = www-data
pm.min_spare_servers = 5
pm.max_spare_servers = 35

PHP-FPM init script

Start by copying the default to the right place, changing to the right permissions and setting it up so the script will run when your system reboots.

sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod 755 /etc/init.d/php-fpm
sudo update-rc.d php-fpm
sudo /etc/init.d/php-fpm start

If it didn’t start, you can go and read the error log to figure out what’s going on. You can do that by issuing this command on another terminal:

sudo tail -f /var/log/php-fpm.log

You can read more about “update-rc.d”, run-levels and starting up services on this awesome tutorial How-To: Managing services with update-rc.d.

Testing with nginx

I’ll assume that you have nginx running on your server. If you don’t, there’re a lot of tutorials that goes from compiling the latest version to using a single “apt-get install nginx”. Here’s a good one on linode.

Since this is pretty straightforward, there’s no point in me repeating the same thing. Simple follow these instructions on how to set a virtual site on nginx and you should be fine. After that, create a PHP file on the right dir with , et voilà.

I’ll save for a future post the way I structure my sites, one for each linux user, with their own public and log dirs.

If you have any question, please leave in the comments. Thanks!