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
How to install the APC to php cgi?
You can install it but you
How did you manage to do this?
Compile PHP in suphp mode (
Done!
2.1 Copy file from source ( There is a file-fpm.in init.d.php )
Finally working!
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.