[HOW-TO] Enable and setup PHP-FPM in DirectAdmin (PHP 5.3)

By BXTra |

I've heard about PHP-FPM about a month ago from DirectAdmin webboard. Then, a few weeks ago, I had a couple websites that I need to make it run faster. After tried to do many things with it like ... to use mod_fcgid / XCache / Memcached, I feel like there was something in XCache since "Page execution time" is quite high. Then, I remember that I've read from many websites that XCache doesn't really work with mod_fcgid. From the technical point, all of them explained it clearly to understand. However, a number of people on many other websites also believe that it's working but that without any explanation. So, I chose to believe that XCache doesn't really work with mod_fcgid even though my own experience that XCache will sometime show that it's work for a very short period of time. Then, it will stop working if you keep monitoring status page. So, PHP-FPM should be the solution in my situation. All PHP Opcode Cache like XCache or APC should work as it should be since all childs will be control under one process manager which is PHP-FPM. Also, mod_fcgid can't be use together with PHP-FPM, only mod_fastcgi that works as of now. If you need more information on why, try google and you will see many people talk about it.

One requirement for this instruction to work is you need PHP 5.3. I have tried to patch PHP 5.2.17 according to PHP-FPM website but it didn't work. After tried to patch it so many times, I decided to upgrade to PHP 5.3.5 and it works correctly now.

Also, when compile PHP-FPM, you will get 2 PHP files. One is "php" which is for CLI mode and "php-fpm" which is PHP-FPM. "php-cgi" file won't be compiled until you removed "--enable-fpm" from configuration. That means... if you want to have the rest of Virtualhosts to run with mod_fcgid (php-cgi) with PHP 5.2, you can still do that. Just make sure to upgrade directly to PHP 5.3 and PHP-FPM at the same time. Always have "--enable-fpm" in configuration file for PHP to compile. That way, "php-cgi" will not compile in PHP version 5.3. So, all websites run with mod_fcgid that still use "php-cgi" file still run in PHP 5.2.

In case you need all of them to run in PHP 5.3. You need to compile PHP twice, one with "--enable-fpm" and one without it.

1. Enable PHP-FPM and compile PHP.

1.1 Copy "configure.php5" for custom compile

mkdir -p /usr/local/directadmin/custombuild/custom/suphp
cp /usr/local/directadmin/custombuild/configure/suphp/configure.php5 /usr/local/directadmin/custombuild/custom/suphp

1.2 Edit configure.php5

nano -w /usr/local/directadmin/custombuild/custom/suphp/configure.php5

add "--enable-fpm" into the file. After edit, the "configure.php5" will look like below :

#!/bin/sh
"./configure" \
"--prefix=/usr/local/php5" \
"--enable-force-cgi-redirect" \
"--enable-fastcgi" \
"--with-config-file-path=/usr/local/etc/php5/cgi" \
"--with-curl=/usr/local/lib" \
"--with-gd" \
"--with-gettext" \
"--with-jpeg-dir=/usr/local/lib" \
"--with-freetype-dir=/usr/local/lib" \
"--with-kerberos" \
"--with-mcrypt" \
"--with-mhash" \
"--with-mysql=/usr" \
"--with-mysqli=/usr/bin/mysql_config" \
"--with-pcre-regex=/usr/local" \
"--with-pdo-mysql=/usr" \
"--with-pear=/usr/local/lib/php" \
"--with-png-dir=/usr/local/lib" \
"--with-zlib" \
"--with-zlib-dir=/usr/local/lib" \
"--with-openssl" \
"--with-iconv=/usr/local" \
"--enable-bcmath" \
"--enable-calendar" \
"--enable-exif" \
"--enable-ftp" \
"--enable-gd-native-ttf" \
"--enable-magic-quotes" \
"--enable-soap" \
"--enable-sockets" \
"--enable-mbstring" \
"--enable-zip" \
"--enable-wddx" \
"--enable-fpm"

1.3 Compile PHP

cd /usr/local/directadmin/custombuild
./build php n

Note : If you get error while compile, you may need to run " .build clean " and then try to compile again.

1.4 After compiled, you will see where PHP-FPM file located as below :

.....
Make Complete
Copying php.ini..
/usr/local/etc/php5/cgi/php.ini already exists, skipping.
Installing php...
Installing PHP SAPI module:       fpm
Installing PHP CLI binary:        /usr/local/php5/bin/
Installing PHP CLI man page:      /usr/local/php5/man/man1/
Installing PHP FPM binary:        /usr/local/php5/sbin/
Installing PHP FPM config:        /usr/local/php5/etc/
Installing PHP FPM man page:      /usr/local/php5/man/man8/
Installing build environment:     /usr/local/php5/lib/php/build/
Installing header files:          /usr/local/php5/include/php/
Installing helper programs:       /usr/local/php5/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php5/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
.....

2. Now, Create Init script file

2.1 Copy file from source

cp /usr/local/directadmin/custombuild/php-5.3.5/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm

2.2 Edit php.fpm init script file as below:

nano -w /etc/init.d/php-fpm

Edit php.fpm file FROM :

.....
prefix=@prefix@
exec_prefix=@exec_prefix@

php_fpm_BIN=@sbindir@/php-fpm
php_fpm_CONF=@sysconfdir@/php-fpm.conf
php_fpm_PID=@localstatedir@/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF"
.....

TO :

.....
prefix=/usr/local/php5
exec_prefix=${prefix}

php_fpm_BIN=/usr/local/php5/sbin/php-fpm
php_fpm_CONF=/usr/local/php5/etc/php-fpm.conf
php_fpm_PID=/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF"
.....

You may need to run command below to have PHP-FPM run automatically when start up (After reboot or turn on the machine) :

/sbin/chkconfig php-fpm on

2.3 Edit PHP-FPM configuration file to match your need. My example below is just to make it work but you can adjust to whatever you like :

nano -w /usr/local/php5/etc/php-fpm.conf

Below is my "php-fpm.conf" configuration :

[global]
pid = /var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
[admin]
listen = /fcgi/admin/public_html/admin.sock
listen.owner = $pool
listen.group = $pool
listen.mode = 0666
user = $pool
group = $pool
pm = dynamic
pm.max_children = 50
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 10
php_admin_value[open_basedir] = /home/$pool/:/tmp:/var/tmp:/var/www/html

Note 1 : $pool is variable which will replaced by the data from the bracket. Here, that is "admin".
Note 2 : php_admin_value[open_basedir] is not require but suggested. Also, without "/var/www/html", you won't be access your webmail or anything through domain name. However, you can still access webmail or anything through IP Address.

2.4 Try to start / stop / restart PHP-FPM

service php-fpm start
service php-fpm stop
service php-fpm restart

Note :

1. To have PHP-FPM compiled and configured in this tutorial doesn't mean you already using it. It just means you already have PHP-FPM ready to use. You need to check my tutorial [HOW-TO] DirectAdmin - Run Apache with "mod_fcgid on PHP 5.2 / 5.3" and "mod_fastcgi + PHP-FPM on 5.3" on the same server that will be post soon.

Source :
- http://php.net/manual/en/install.fpm.php
- http://michaelshadle.com/2010/08/26/cleanest-configuration-for-the-new-php-fpm
- http://php-fpm.org

You can install it but you won't get any benefit of APC since php-cgi doesn't work with APC/XCache. APC/XCache should work great with CLI, mod_fastcgi or PHP-FPM. With mod_fcgid, it seems to work at first but if you check a while later. You will see that it won't work. Google to find more information about it.

I keep wondering: I have been using DA for years now and configured PHP in a variety of ways. I tried to enable PHP-FPM exactly the way shown here but it simply will not compile/install. I keep getting
Installing PHP SAPI module:       apache2handler
instead of
Installing PHP SAPI module:       fpm
Done this four times now and the result is still the same.

Compile PHP in suphp mode ( php5_cgi=yes in /usr/local/directadmin/custombuild/options.conf ). The top part in options.conf will look like below : default_php=5 php5_ver=5.3 php4_cli=no php4_cgi=no php5_cli=no php5_cgi=yes .. .. Then, follow step 1 in the guide and you will get it.

Thanks, now it compiled and has been installed. Now I can start the real testing phase :)

Thank you for the simple and working tutorial. I have been working on this for 2 days, can not make FPM+socket work. Follow your steps , it's done in minutes.

Add new comment

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.