Recent Posts

Archives

Categories

PHP 5.3 FPM

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.

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
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/php-fpm.pid
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

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.