PHP-FPM configuration-related

Source: Internet
Author: User
Tags deprecated fpm php website

[TOC]

PHP-FPM configuration related one, PHP-FPM configuration

Unlike lamp, in the LNMP architecture, PHP-FPM as a standalone service exists, and since it is a standalone service, it must have its own configuration file. The PHP-FPM configuration file is/usr/local/php-fpm/etc/php-fpm.conf, which also supports the include statement, similar to the include in nginx.conf.

1. The pool of PHP-FPM

Nginx can configure more than one virtual host, PHP-FPM configuration file php-fpm.conf can set up multiple pool, each pool can listen to a port, in which one pool resource exhausted, will cause other sites inaccessible resources, reported 502 error. It is necessary to separate the sites, using separate pool respectively.

1. Open php-fpm.conf
[[email protected] ~]# vim /usr/local/php/etc/php-fpm.conf[[email protected] ~]# cd /usr/local/php-fpm/etc/[[email protected] etc]# lspear.conf  php-fpm.conf  php-fpm.conf.default  php.ini[[email protected] etc]# cat php-fpm.conf[global]pid = /usr/local/php-fpm/var/run/php-fpm.piderror_log = /usr/local/php-fpm/var/log/php-fpm.log[www]listen = /tmp/php-fcgi.sock#listen =127.0.0.1:9000listen.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
Php-fpm.conf Part Content Annotation
#[www]#listen = /tmp/php-fcgi.sock#监听地址,也可以写:listen = 127.0.0.1:9000,本地监听,也可以监听其他IP:port#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径#listen.mode = 666#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限#user = php-fpm#group = php-fpm#定义php-fpm服务的用户
On the original basis on the copy to modify a
[xavi.com]listen = /tmp/xavier.sock#listen =127.0.0.1:9000listen.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
Check and restart the PHP-FPM service
[19-Mar-2018 23:02:28] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[[email protected] etc]# /etc/init.d/php-fpm reloadReload service php-fpm  done
View the next PHP-FPM service
[[email protected] etc]# PS aux |grep php-fpmroot 4684 0.0 0.2 227268 4984?        Ss 23:03 0:00 php-fpm:master process (/usr/local/php-fpm/etc/php-fpm.conf) php-fpm 4685 0.0 0.2 229292 5080?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4686 0.0 0.2 229292 5080?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4687 0.0 0.2 229292 5080?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4688 0.0 0.2 229292 5084?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4689 0.0 0.2 229292 5088?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4690 0.0 0.2 229292 5088?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4691 0.0 0.2 229292 5088?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4692 0.0 0.2 229292 5088?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4693 0.0 0.2 229292 5088?        S 23:03 0:00 php-fpm:pool wwwphp-fpm 4694 0.0 0.2 229292 5088? S 23:03 0:00 Php-fpm:pOol wwwphp-fpm 4705 0.0 0.2 229292 5088?        PHP-FPM 4809 0.0 0.2 229292 5084?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4810 0.0 0.2 229292 5084?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4811 0.0 0.2 229292 5084?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4812 0.0 0.2 229292 5084?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4813 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4814 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4815 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4816 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4817 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4818 0.0 0.2 229292 5088?        S 23:08 0:00 php-fpm:pool xavi.comphp-fpm 4819 0.0 0.2 229292 5088? S 23:08 0:00 phP-fpm:pool xavi.com 
2. Already see WWW and xavi.com two pool, next how to apply
[[email protected] etc]# cd /usr/local/nginx/conf/vhost/[[email protected] vhost]# lsbcd.com.conf  load.conf  proxy.conf  ssl.conf  test.com.conf[[email protected] vhost]# vim test.com.conf[[email protected] vhost]# vim bcd.com.confserver    {   listen 80 default_server;   server_name abcd.com;   index index.html index.htm index.php;   root /data/nginx/default;location ~ \.php$      {        include fastcgi_params;        fastcgi_pass unix:/tmp/xavier.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /data/nginx/default$fastcgi_script_name;      }
Another way to create multiple pool is to have multiple conf files under/usr/local/nginx/conf/vhost/so that we can configure more than one virtual machine, so does PHP-FPM also support multiple conf files, yes

[[email protected] php-fpm.d]# vi www.conf[www]listen = /tmp/php-fcgi.sock#listen =127.0.0.1:9000listen.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[[email protected] php-fpm.d]# vi xavi.conf[xavi.com]listen = /tmp/xavier.sock#listen =127.0.0.1:9000listen.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

So that the original php-fpm.conf is only three lines of content.

[[email protected] php-fpm.d]# cat ../php-fpm.conf[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
Check syntax and restart
[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t[20-Mar-2018 20:39:01] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful[[email protected] php-fpm.d]# /etc/init.d/php-fpm restartGracefully shutting down php-fpm . doneStarting php-fpm  done
PS aux |grep php-fpm

Second, php-fpm slow execution log

PHP website, one of the benefits of using the LNMP architecture is to look at "slow logs", why do you say so? When our customer or manager found that the site access speed point is slow, if all the load is not very high, everything is normal or slow, how to do? The only way to do that is to check the "slow log", the slow log will show us in detail how many lines of a page of code in the execution process exceeded the threshold we set!

2.1 Configure www.conf Bar

On the original basis, add two lines:

request_slowlog_timeout = 2slowlog = /usr/local/php-fpm/var/log/www_slow.log命令的意思是:只要执行某条语句超过2秒就会记录到咱们规定的日志文件中去。

Turn on slow execution log

[[email protected] php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf[www]listen = /tmp/php-fcgi.sock#listen =127.0.0.1:9000listen.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 = 1024request_slowlog_timeout = 2slowlog = /usr/local/php-fpm/var/log/www_slow.log[[email protected] php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t[20-Mar-2018 21:50:45] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
2. View Slow Execution Log
[[email protected] php-fpm.d]# ls /usr/local/php-fpm/var/log/php-fpm.log  www-slow.log[[email protected] php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
    • We found that there are already configuration files, but no errors have been recorded;
Cat/usr/local/nginx/conf/vhost/test.com.conf

Php-fcgi.sock is occupied by test.com.conf, creating sleep.php file under/data/nginx/test.com/
[[email protected] nginx]# vim /data/nginx/test.com/sleep.<?php echo "slowlog test!";  sleep(2);echo "done";?>

Error Reporting:

[[email protected] nginx]# vim /data/nginx/test.com/sleep.php[[email protected] nginx]# curl -x127.0.0.1:80 test.com/sleep.phpcurl: (7) Failed connect to 127.0.0.1:80; 拒绝连接

Repeat multiple default 80 ports, and forget the} symbol to delete the test file in time

Test results
[[email protected] vhost]# curl -x127.0.0.1:80 test.com/sleep.php slowlog test!done[[email protected] vhost]# [[email protected] vhost]#  cat /usr/local/php-fpm/var/log/www-slow.log[20-Mar-2018 22:58:57]  [pool www] pid 4662script_filename = /data/nginx/test.com/sleep.php[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1[20-Mar-2018 22:59:49]  [pool www] pid 4664script_filename = /data/nginx/test.com/sleep.php[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1 //第一行[[email protected] vhost]# cat /data/nginx/test.com/sleep.php<?php echo "slowlog test!"; sleep(2);echo "done"; ?>

Error Analysis 2

The simplest way is to open the configuration file and view the error log: Vim/usr/local/php-fpm/etc/php.ini

Identification is a grammatical error.

Third, Open_basedir

If a server has a number of Web services, it is not recommended to directly configure Open_basedir directly in the php.ini. Appache you can set a OPEN_BASEDIR,PHP-FPM for each virtual host you can also set different open_basedir for different pool.

[[email protected] vhost]#  vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

Purpose: This completely limits the Xavi.com directory can not access other site resources, physical isolation of multiple sites The above troubleshooting is open php.ini display_error in production is not safe. Establish a log error-checking system
vim /usr/local/php-fpm/etc/php.ini增加如下配置:搜索/error_log errot_log = /usr/local/php-fpm/var/log/php_error.log

[[email protected] vhost]# grep error_log /usr/local/php-fpm/etc/php.ini; server-specific log, STDERR, or a location specified by the error_log; Set maximum length of log_errors. In error_log information about the source is;error_log = php_errors.log;error_log = syslogerror_log = /usr/local/php-fpm/var/log/php_error.log; OPcache error_log file name. Empty string assumes "stderr".;opcache.error_log=
To ensure normal output of the log, set up the log file under this path and change its properties
[[email protected] vhost]# touch /usr/local/php-fpm/var/log/php_error.log[[email protected] vhost]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
Test error log:

Modify Ask Conf file be sure to restart the PHP-FPM to take effect
  [[email protected] vhost]#!curlcurl-x127.0.0.1:80 test.com/sleep.php slowlog test!done[[email  protected] vhost]#/etc/init.d/php-fpm restartgracefully shutting down php-fpm. Donestarting php-fpm done[[email protected] vhost]# curl-x127.0.0.1:80 test.com/sleep.php No input file specified. [[email protected] vhost]#!catcat/usr/local/php-fpm/var/log/php_error.log[20-mar-2018 15:38:25 UTC] php  Deprecated:comments starting with ' # ' is Deprecated in Unknown on line 1 in Unknown on line 0[20-mar-2018 15:38:25 UTC]  PHP deprecated:comments starting with ' # ' is Deprecated in Unknown on line 1 in Unknown on line 0[20-mar-2018 15:38:31 UTC] PHP Warning:Unknown:open_basedir restriction in effect.  File (/data/nginx/test.com/sleep.php) is not within the allowed path (s): (/data/nginx/wwtest.com:/tmp/) in Unknown on line 0[20-mar-2018 15:38:31 UTC] PHP Warning:Unknown:failed to open stream:operation not permitted in Unknown on line 0 

Iv. PHP-FPM Process Management
    • [] pm = dynamic;
      Dynamic process management, or static, can only be set to dynamic, the following configuration will take effect

    • [] Pm.max_children = 50;
      Maximum number of sub-processes, PS aux can be viewed

    • [] Pm.start_serve = 20; The number of processes that will start when the service is started

    • [] pm.min_spare_servers = 5;
      Define the idle period, the minimum number of child processes, if this value is reached, PHP-FPM
      The service automatically derives new child processes.

    • [] pm.max_spare_servers = 35;
      Defines the maximum value of the child process during the idle period, and starts cleaning up the idle child process if it is above this value.

    • [] pm.max_requests = 500; Defines the maximum number of requests processed by a child process, that is, a PHP-FPM child process can handle this request at most, and when this value is reached, he automatically exits.

PHP-FPM configuration-related

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: 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.