php 5.3.x / php-fpm / nginx

I have started moving my dev environments from apache to ngnix for various reasons, mostly because ngnix has a smaller footprint and lets me get more out of low memory vms. I would probably write another post later about the reasons for switching from apache to ngnix for now, here are my notes for getting the php5.3.x/php-fpm and nginix working together. I should probably automate this process using hudson/puppet/chef configuration but that’s again an exercise for later.

Notes:
Install the required basic development tools (especially if you are using a new vm/install)

$ sudo apt-get install make bison flex gcc patch autoconf subversion locate
$ sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev --with-mysql --with-pdo-mysql

Download and patch php 5.3

$ cd /usr/local/src
$ sudo wget http://uk.php.net/get/php-5.3.2.tar.gz/from/this/mirror
$ sudo tar zxvf  php-5.3.2.tar
$ cd /usr/local/src/php-5.3.2
$ sudo svn co http://svn.php.net/repository/php/php-src/branches/PHP_5_3/sapi/fpm sapi/fpm
$ sudo ./buildconf --force
$ sudo ./configure --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --enable-pdo --with-curl --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-mbregex --with-mhash --enable-xslt  --enable-zip --with-pcre-regex --with-mysql --with-pdo-mysql

Now do the regular configure, make, make test and make install

If you are a purist and want to remove all the symbols in php, run the following command, personally I leave it as it is:

$sudo strip /usr/local/bin/php-cgi

Also if you want the mecache and apc goodness then run the following commands

$ sudo pecl install memcache
$ sudo pecl install apc

Now prepare for the configuration values:

$ sudo cp /usr/local/src/php-5.3.2/php.ini-development /usr/local/lib/php.ini #if you for dev, else choose recc
$ sudo mkdir /etc/php
$ sudo ln -s /usr/local/lib/php.ini /etc/php/php.ini
$ sudo ln -s /usr/local/etc/php-fpm.conf /etc/php/php-fpm.conf

The other important thing that needs doing is changing the user permissions in the php-fpm.conf file. It should basically be the same user with which ngnix user runs. It was www-data in my case.

Next step is to install Ngnix. There are tons of tutorials on the internet so not going to cover it. If you are on a debian based system, you can install it quickly using the apt-get command.

$ sudo apt-get install nginx

Now its time to start the php-fpm process, which you can do by running this as root.

$ sudo php-fpm -y /etc/php/php-fpm.conf

There might be a few more tweaks that need doing in the php-fpm.conf but leave that an exercise for the reader.

Now, tweak the ngnix host files under the /etc/nginx/sites-available to your hearts content and enable the fastcgi module in it. I shall cover its configuration in detail in the next posts.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.