PHP-FPM with Apache/Nginx

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

Installing PHP with FPM

​Download the latest PHP from http://www.php.net/ to /root/downloads/

​Extract it to downloads folder and run configure(with –enable-fpm), make and make install.

​tar xvjf php-5.5.23.tar.bz2

​cd php-5.5.23

​./configure –with-config-file-path=/etc/ –with-mysql=mysqlnd –with-jpeg-dir=/usr/ –with-zlib-dir=/usr/lib64/ –with-png-dir=/usr/lib64/ –with-freetype-dir=/usr/lib64 –enable-ftp –enable-calendar –with-libxml-dir=/usr/local –enable-sockets –enable-wddx –with-xsl=/usr/local –with-iconv-dir=/usr/local –with-gd –with-mysqli=mysqlnd –enable-sockets –with-curl –with-gettext –enable-mbstring –with-pdo-mysql=mysqlnd –with-tidy –enable-exif –enable-fpm –enable-opcache

​make

​make install

Configuring FPM

​Edit /usr/local/etc/php-fpm.conf and change the following settings as per the requirement.

error_log = /var/log/php-fpm.log

emergency_restart_threshold = 10

emergency_restart_interval = 1m

process_control_timeout = 10s

events.mechanism = epoll

[www]

user = daemon

group = daemon

listen = /tmp/php-fpm.sock

listen.owner = daemon

listen.group = daemon

listen.mode = 0660

pm = dynamic

pm.max_children = 700

pm.start_servers = 35

pm.min_spare_servers = 30

pm.max_requests = 5000

 

Add startup script and enable the service

​Copy the startup script from the php source folder to init.d folder and enable it on startup.

​cp /root/downloads/php-5.5.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

​chkconfig –add php-fpm

​chkconfig php-fpm on

​Change permission on php-fpm in init.d

/etc/init.d/php-fpm start

 

Configure Nginx and Apache to use PHP-FPM
Nginx

​Add the below configuration to nginx virtual host file, modify the paths accordingly and reload nginx.

location ~ \.php$ {

root   /var/www/html/www.domain.com/public_html;

try_files  $uri =404;

fastcgi_pass  unix:/tmp/php-fpm.sock;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME \ /var/www/html/www.domain.com/public_html/$fastcgi_script_name;

fastcgi_param PATH_INFO               $fastcgi_path_info;

fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_script_name;

include /etc/nginx/conf/fastcgi_params;

fastcgi_read_timeout 480;

fastcgi_send_timeout 480;

}

Apache

​Add the below configs to each VirtualHost configuration and reload apache

<Proxy unix:/tmp/php-fpm.sock|fcgi://127.0.0.1:9000>

ProxySet min=0

ProxySet timeout=300

</Proxy>

ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/tmp/php-fpm.sock|fcgi://127.0.0.1:9000/var/www/html/www.domain.com/public_html/

Options FollowSymLinks Includes ExecCGI

Comments are closed.