php-fpm on Cpanel

After experimenting with mod_ruid2 and mod_fcgi in cpanel i found that the php speed just wasn’t as good as it should be.

I was really missing an opcode system. I prefer to use APC as it is actively developed by the php team themselves.

With mod_ruid2 APC works quite well but you are restricted to use apache prefork which uses lot of resources.

With mod_fcgi APC doesn’t work as well as it quickly eats up all your ram as the opcode isn’t shared. You can use mpm_worker though so this is an upside.

With php-fpm and mod_fastcgi being the ultimate solution but unfortunately cPanel still haven’t included php-fpm within their nice easyapache install system.

Someone on the cpanel forums has managed to get php-fpm up and running and you can find the instructions here.

I have modified it so that it uses a socket and fixed the users for the current apache 2.2.24

I got it working.





Then you recompile php and apache using easyapache with the mpm worker selected.

Then you have to set up your


script which you copy from


Then you have to fill in your variables at the top of the file:

#! /bin/sh

# Provides: php-fpm
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts php-fpm
# Description: starts the PHP FastCGI Process Manager daemon



you might have to locate or create each of these files. php-fpm was in the path for me.

type php-fpm

gave me that.

i copied




I had to edit it too for my user and group that apache runs under, this will vary server to server so you’ll have to see what permissions your apache server needs

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user’s group
; will be used.
user = nobody
group = nobody

and comment out the ip and change it to a socket.

;listen =
listen = /tmp/php-fpm.sock


You will have to create this file it won’t have permissions to create it itself even ran as root for some reason:

touch /var/run/

Having fun yet? K now you need to make apache use it for php.

vim /usr/local/apache/conf/includes/post_virtualhost_global.conf

you’re going to need to add this to the end of the file:


LoadModule fastcgi_module modules/

FastCGIExternalServer /usr/sbin/php-fpm -socket /tmp/php-fpm.sock
AddHandler php-fastcgi .php

# SetHandler php-fastcgi-virt
# Action php-fastcgi-virt /usr/sbin/php-fpm.fcgi virtual

Action php-fastcgi /usr/sbin/php-fpm.fcgi
ScriptAlias /usr/sbin/php-fpm.fcgi /usr/sbin/php-fpm

Options ExecCGI FollowSymLinks
SetHandler fastcgi-script
Order allow,deny
Allow from all

Don’t get ahead of yourself yet, we still haven’t compiled mod_fastcgi from source:
For which I followed this guide:
FastCGI for Apache2

Obviously ignore the bit about installing apache, as we already did that start with the part you download fastcgi.

You’ll have to change the source a bit differently than he lists tho here was my makefile:

# Makefile for Apache2

builddir = .

top_dir = /home/cpeasyapache/src/httpd-2.2.24

top_srcdir = ${top_dir}
top_builddir = ${top_dir}

include ${top_builddir}/build/

APXS = /usr/local/apache/bin/apxs
APACHECTL = /usr/local/apache/bin/apachectl

#LIBS=-Lmy/lib/dir -lmylib
INCLUDES=-I /home/cpeasyapache/src/httpd-2.2.24

all: local-shared-build

-rm -f *.o *.lo *.slo *.la

Then I ran into a problem with it not being able to create/read the log which would make nothing work:


touch /usr/var/log/php-fpm.log
chmod 777 /usr/var/log/php-fpm.log

chmod 777 is taboo and I’ll fix it soon but it got it able to read it when nothing else seemed to.

Lastly restart/start both of them:

/etc/init.d/php-fpm restart
/etc/init.d/httpd restart

Result: Well I’ve been optimizing our server for 2 weeks and this was the last thing. I’d already gotten the server response/page load time for our ecommerce php store to 500 to 600ms per product detail page according to firebug from times over 1.5 seconds. Now it’s down to no larger than 437ms. You can blink and miss it seriously. It’s well worth the effort I’ve never seen an ecommerce site this fast that runs off a php cart. It took me about 6 or 7 hours of researching around looking up info. Hopefully this will let someone else do it in far less time (my prior post was made a while after I’d started).

Inode problems

So i discovered my server had ran out of inodes yet had lots of space left.

This caused quite a few problems for me!

After a quick search i found

find . -xdev -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n

at root showed me where the inodes were being used.

A little deeper investigation i discovered this post on cpanel’s forums

so with the cpanel script


I could purge the old comet files and release all those inodes again!

I had previously been under attack from spam email.