Linux Learning Summary (44) LNMP PHP-FPM related configuration

Source: Internet
Author: User
Tags fpm

1 PHP-FPM Process Pool

Before we added an include vhost/*.conf in the Nginx configuration file; Then realize the isolation of each virtual host, php-fpm as a separate service, there is no similar operation? The answer is yes. Here we introduce a PHP-FPM process pool concept. Pool in the LNMP schema is a collection of process resources allocated to parse PHP. PHP-FPM can set multiple pool, in which one pool resource is exhausted, causing other sites to be unable to access resources, reported 502 error. It is necessary to separate the sites, using separate pool respectively. We can define an include in php-fpm.conf and then define a separate pool. The process is as follows:
vim/usr/local/php-fpm/etc/php-fpm.conf change content as follows

[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.loginclude = etc/php-fpm.d/*.conf

cd/usr/local/php-fpm/etc/
mkdir PHP-FPM.D
CD php-fpm.d/
Vim www.conf//write content as follows, add first pool

[www]listen = /tmp/www.socklisten.mode = 666user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024

Save and then edit the additional configuration file, then create a pool
Vim test.conf

[test]listen = /tmp/test.socklisten.mode = 666user = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024

This has two sub-profiles, that is, the creation of two pool, two pool listening to different sockets, we can in the virtual host configuration file for different virtual hosts to define a different pool, so as to achieve mutual isolation.

Configuration under test
/usr/local/php-fpm/sbin/php-fpm -t
Restart the service
/etc/init.d/php-fpm restart
View the sock file under/tmp/:
ls /tmp/*.sock

Check the next process again.
PS aux |grep php-fpm
Found a two pool of www and test

2PHP-FPM Slow Execution Log

If a PHP site can be accessed, that is, access is slow, how can we go further to find out why? Is there any way to trace the details that lead to a slow PHP parsing, which involves PHP-FPM's slow execution log. With PHP-FPM's slow execution log, we can clearly understand where PHP's script executes for a long time, and it can navigate to specific lines of code. How to turn on and view the log, with the following details:
Vim/usr/local/php-fpm/etc/php-fpm.d/www.conf
Add the following on the last side

request_slowlog_timeout = 1slowlog = /usr/local/php-fpm/var/log/www-slow.log

The first line defines the time-out, in seconds, when the PHP script executes more than 1 seconds to log
The second line defines the path and name of the log
We write a PHP script test

 vim /data/wwwroot/test.com/slow.php<?phpecho "just a slow running test";sleep (3);?>curl -x127.0.0.1:80 test.com/slow.php

The cursor paused for a few seconds before outputting the just a slow running test
Let's go check the slow execution log.
Cat/usr/local/php-fpm/var/log/www-slow.log

3open_basedir

The concept was previously exposed in lamp. The purpose of configuring it is to secure the site. HTTPD can be set for each virtual host a OPEN_BASEDIR,PHP-FPM can also be set differently for different pool Open_basedir

vim /usr/local/php-fpm/etc/php-fpm.d/test.conf  //在最后面加入php_admin_value[open_basedir] = /data/wwwroot/test.com:/tmp/
4PHP-FPM error Log

In lamp we have individually defined the error log for PHP, we closed the error message in/usr/local/php-fpm/etc/php.ini page display, define the error log path and name, define the log level. Since this configuration file is the same as the configuration file in lamp, it originates from the php.ini-production file in the PHP source package, so the configuration is the same as before. Don't repeat it here.

5PHP-FPM Process Management

Below we introduce the meaning of a configuration in the next php-fpm.conf

 pm = dynamic  //动态进程管理,也可以是static,静态一次性启动最大子进程数,不会变化。 pm.max_children = 50 //最大子进程数,ps aux可以查看 pm.start_servers = 20 //启动服务时会启动的进程数 pm.min_spare_servers = 5 //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。 pm.max_spare_servers = 35 //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。 pm.max_requests = 500  //定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。

Linux Learning Summary (44) LNMP PHP-FPM related configuration

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.