Lamp combo PHP works in FPM mode

Source: Internet
Author: User

Tags: server application dynamic Interpreter

The version after fastcgi:php-5.3.3 comes with the FPM component,

The FPM component enables the PHP interpreter to work as a daemon and provides an application similar to the Prefork mode that can receive FASTCGI server from the front-end httpd,http-proxy-fcgi reverse proxy user to the backend for dynamic content requests

There are a few areas to note when building a split lamp:

1. Dynamic resources are deployed on the application server (PHP-FPM), and static resources are deployed on the Web server

2, when PHP is working in FPM, it will not be compiled into a httpd module, so compile without using--with-apxs2=/path

parameter, no more LoadModule in the httpd master configuration file Php5_module

Important parameters that need to be turned on when compiling PHP into FPM operation mode--ENABLE-FPM

3, when compiling PHP, you need to connect MySQL or other database management system, it is necessary to enable its related driver module.

4, PHP and MySQL is not the same node, the user account to connect to MySQL needs remote access permissions.

Compiling and installing PHP

#tar XF php-5.4.0.tar.bz2

#cd php-5.4.0

#./configure--prefix=/usr/local/php--with-config-file-path=/etc/php/php.ini--with-config-file-scan-dir=/etc/ php.d/--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib--with-libxml-dir-- Enable-xml--with-mcrypt--with-bz2--with-openssl--with-curl--with-xsl--with-iconv=/usr/local/libiconv-- Enable-sockets--enable-short-tags--enable-static--enable-gd-native-ttf--enable-ftp--enable-maintainer-zts-- With-mysql=mysqlnd--with-pdo-mysql=mysqlnd--with-mysqli=mysqlnd--enable-fpm

#make && make Install

Provide a configuration file for PHP


#cp Php.ini-production/etc/php/php.ini

Configure PHP-FPM

#cp sapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm (Service script)

#chmod +x/etc/rc.d/init.d/php-fpm (add execute permission)

#chkconfig –add php-fpm (added to the list of services)

#chkconfig PHP-FPM on

Providing configuration files for PHP-FPM


Edit the PHP-FPM configuration file, configure the FPM related options for the value you need, and enable the PID file (the last line below):

# vim/usr/local/php/etc/php-fpm.conf

Listen =

Pm.max_children = # # # #静态方式下开启的php number of-FPM processes

Pm.start_servers = 5 # # #动态方式下的起始php number of-FPM processes

Pm.min_spare_servers = 2 # # #动态方式下的最小php number of-FPM processes

Pm.max_spare_servers = 8 # # #动态方式下的最大php number of-FPM processes

PID =/usr/local/php/var/run/ # # #PID文件路径

Note: If the DM is set to static, then only pm.max_children this parameter takes effect. The system turns on the number of PHP-FPM processes that are set.

If the DM is set to dynamic, then the Pm.max_children parameter is invalidated and the next 3 parameters take effect.

The system starts the Pm.start_servers PHP-FPM process at the beginning of the PHP-FPM run, and then according to the system's

The demand dynamically adjusts the number of php-fpm processes between Pm.min_spare_servers and pm.max_spare_servers.

When the PHP-FPM is used as a standalone daemon, the default listener is on the port, which is the default to allow native

HTTPD process Access, the current environment is httpd and PHP are installed on different servers, so to modify the listening port, so that other hosts

able to access.

When each work in fast-cgi mode, the front-end Web server must communicate with the back-end host with the fcgi module.


#service php-fpm Start

Verify that startup is successful

#netstat –tnlp|grep PHP-FPM

TCP 0 0* LISTEN 32873/php-fpm

HTTPD related Configurations

1. Enable the relevant modules of httpd

After the Apache httpd2.4 has a module specifically for the implementation of FASTCGI, this module is,

It is actually an extension of the module, so the two modules are loaded

LoadModule Proxy_module modules/

LoadModule Proxy_fcgi_module modules/

Turn on the virtual host switch


2. Configure the virtual host to support the use of fcgi

Add two lines similar to the following in the corresponding virtual host:

Proxyrequests OFF

Proxypassmatch ^/(. *\.php) $fcgi://$1

Proxyrequests off: Turn off the forward proxy

Proxypassmatch: Send a file request ending in. php to the PHP-FPM process, php-fpm need to know at least the directory and URI that is running

So the two parameters are indicated here directly after the fcgi://

The delivery of other parameters has been encapsulated by and does not need to be specified manually.

Issues to be aware of:

The dynamic resource needs to be placed on the PHP server, when the HTTPD server receives the dynamic page content requested by the client browser,

will send the request to the specified PHP server for processing, but static pages such as HTML, JPG, etc. will still be on the httpd server

Processing, otherwise information such as images may not be displayed, so when deploying a website, you need to deploy the site's resources separately on both sides.

Create directories of the same path on both the httpd server and the PHP server, or you will get an error when starting the HTTPD service.

To edit a virtual host configuration file:

# vim/etc/httpd/extra/httpd-vhosts.conf

<virtualhost *:80>

ServerAdmin [email protected]

DocumentRoot "/data/web/www/"



Proxyrequests OFF

Proxypassmatch ^/(. *\.php) $ fcgi://$1

Errorlog "/usr/local/apache/logs/phpinfo-error_log"

Customlog "/usr/local/apache/logs/phpinfo-access_log" common


<directory "/data/web/www/" >

AllowOverride None

Options None

Require all granted


<ifmodule dir_module>

DirectoryIndex index.php index.html


Create a Web Host home page (on PHP server) to test whether httpd can support PHP

# vim/data/web/www/index.php

Edit the following content:


Phpinfo ();


Test on the client browser:

At this point, the Server API operating mode is: fpm/fastcgi

This article is from the "Boomshakalaka" blog, make sure to keep this source

Lamp combo PHP works in FPM mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: