Linux php-fpm pool, slow execution log, process management, and Open_basedir introduction

Source: Internet
Author: User
Tags fpm parse error writing test scripts

The pool of PHP-FPM

NIGNX can configure multiple virtual hosts, PHP-FPM also support the configuration of multiple pool, each pool can listen to a port, can also listen to a socket.

PHP-FPM Configuration Instructions:

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. Edit the configuration file

[[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.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[[email protected] etc]# vim php-fpm.conf  //增加一个以下配置:include = etc/php-fpm.d/*.conf//include的这一行比较特殊,请注意等号后面的路径,必须写上etc目录,然后需要创建配置文件目录和子配置文件。

After editing the php-fpm.conf configuration file in the www below the pool deleted, leaving only the [Global] section

2. Create php-fpm.d directory and directory files

[[email protected] etc]# mkdir php-fpm.d[[email protected] etc]# cd php-fpm.d[[email protected] php-fpm.d]# vim www.conf[[email protected] php-fpm.d]# vim aming.conf[[email protected] php-fpm.d]# lsaming.conf  www.conf//两个文件分别增加内容如下:[www]listen = /tmp/php-fcgi.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[aming.com]listen = /tmp/aming.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
3. How to separate two sites

    • The original site test.com.conf is configured as follows:

    • To add a aaa.com.conf site information, operate as follows:

[[email protected] php-fpm.d]# cd /usr/local/nginx/conf/vhost/[[email protected] vhost]# lsaaa.com.conf  ld.conf  proxy.conf  ssl.conf  test.com.conf[[email protected] vhost]# vim aaa.com.conf 增加如下配置内容:location ~ \.php$    {        include fastcgi_params;        fastcgi_pass unix:/tmp/aming.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;    }

3. Test syntax

[[email protected] etc]# /usr/local/php-fpm/sbin/php-fpm -t[09-Jan-2018 16:49:00] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

4. Restart PHP-FPM

[[email protected] etc]# /etc/init.d/php-fpm restart  //或/etc/init.d/php-fpm reloadGracefully shutting down php-fpm . doneStarting php-fpm  done

5. View PHP-FPM boot status

Input PS aux |grep php-fpm view, show two pool

PHP-FPM Slow Execution Log

With PHP-FPM's slow execution log, we can see very clearly where PHP's script executes for a long time, and it can navigate to specific rows.

1. Edit the configuration file

[[email protected] vhost]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf //针对www文件做一个测试增加如下内容:request_slowlog_timeout = 1  //超过一秒中就会记录日志slowlog = /usr/local/php-fpm/var/log/www-slow.log  //日志存放的路径

1. Test syntax

[[email protected] vhost]# /usr/local/php-fpm/sbin/php-fpm -t[09-Jan-2018 19:15:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

2. Restart PHP-FPM

[[email protected] vhost]# /etc/init.d/php-fpm restartGracefully shutting down php-fpm . doneStarting php-fpm  done

3. Configure Nginx Virtual host test.com.conf, change Unix:/tmp/php-fcgi.sock to Unix:/tmp/www.sock

4. Writing test Scripts

[[email protected] php-fpm.d]# vim /data/wwwroot/test.com/sleep.php增加如下配置内容:<?phpecho "test slow log";sleep(2);echo "done";?>

5. Test with Curl

[[email protected] php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I  //访问的时候出现500,说明有语法错误。 HTTP/1.1 500 Internal Server ErrorServer: nginx/1.12.1Date: Tue, 09 Jan 2018 11:23:17 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.6.30

Error handling:

    1. Enter Vim/usr/local/php-fpm/etc/php.ini
    2. Locate the Display_error, change it to on, and then access the error main will have the output information

    1. Remember reload reload the configuration file
    2. Test the error message, due to the sign problem.

[[email protected] php-fpm.d]# /etc/init.d/php-fpm reloadReload service php-fpm  done[[email protected] php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php<br /><b>Parse error</b>:  syntax error, unexpected ‘slow‘ (T_STRING), expecting ‘,‘ or ‘;‘ in <b>/data/wwwroot/test.com/sleep.php</b> on line <b>2</b><br />
    1. To change the symbolic problem of the configuration file vim/data/wwwroot/test.com/sleep.php

6. View Slow Execution Log

[[email protected] php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log  //慢执行日志里指出了哪个脚本运行慢,也指出了是哪一行执行慢  [09-Jan-2018 19:39:54]  [pool www] pid 81864script_filename = /data/wwwroot/test.com/sleep.php[0x00007f5987a07290] sleep() /data/wwwroot/test.com/sleep.php:3[[email protected] php-fpm.d]# cat /data/wwwroot/test.com/sleep.php <?php echo "test slow log";sleep(2);       //这个脚本超过1秒echo "done";?>

Define Open_basedir

The purpose of Open_basedir is security, as long as the corresponding Nginx virtual host configuration file to call the corresponding pool, you can use Open_basedir to physically isolate multiple sites, so as to achieve security purposes.

1. Configure the virtual host file

[[email protected] php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf 增加如下配置内容:php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

2. Using Curl Testing

[[email protected] php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I  //正常访问HTTP/1.1 200 OKServer: nginx/1.12.1Date: Tue, 09 Jan 2018 12:32:50 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.6.30

3. Configuring error log Path Testing

    • Add Error path

[[email protected] php-fpm.d]# vim /usr/local/php-fpm/etc/php.ini //增加错误日志路径及定义日志级别,如


    • Go to www.conf config file and change test.com to aming.com, test again, check the error log
    • Viewing the error log

[[email protected] php-fpm.d]# grep error_log/usr/local/php-fpm/etc/php.ini//view log path; 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/v Ar/log/php_errors.log; Opcache error_log file name. Empty string assumes "stderr".; o  Pcache.error_log=[[email protected] php-fpm.d]# Ls/usr/local/php-fpm/var/log/php-errors.log php-fpm.log Www-slow.log[[email protected] php-fpm.d]# ls/usr/local/php-fpm/var/log///See if the log path exists Php-fpm.log Www-slow.log [[email protected] php-fpm.d]# touch/usr/local/php-fpm/var/log/php_errors.log//Manually create a log file [[email  Protected] php-fpm.d]# chmod 777/usr/local/php-fpm/var/log/php_errors.log//change to all permissions to prevent writing to log [[email protected ] php-fpm.d]#!curlcurl-x127.0.0.1:80 test.com/3.php-ihttp/1.1 404 Not Foundserver:nginx/1.12.1date:tue, 2018 1 3:05:05 gmtcontent-type:text/html; Charset=utf-8connection:keep-alivex-powered-by:php/5.6.30[[email protected] php-fpm.d]# cat/usr/local/  Php-fpm/var/log/php_errors.log//Because Open_basedir limit address is incorrectly written, all will be error, Access appears 404[09-jan-2018 13:05:05 UTC] php Warning: Unknown:open_basedir restriction in effect. File (/data/wwwroot/test.com/3.php) is not within the allowed path (s): (/data/wwwroot/aming.com:/tmp/) in Unknown on line 0 [09-jan-2018 13:05:05 UTC] PHP Warning:Unknown:failed to open stream:operation not permitted in Unknown on Lin

PHP-FPM Process Management

    1. PM = dynamic//Defines PHP-FPM's child process startup mode, which dynamically is managed by the dynamics process, starting with only a small number of sub-processes, dynamically increasing or decreasing the number of child processes according to actual requirements, up to no more than the values defined by Pm.max_children. Another mode is static, the number of processes in this mode is determined by Pm.max_children, one-time boot so much, will not be reduced or increased.
    2. Pm.max_children = 50//maximum number of sub-processes, PS aux can be viewed
    3. Pm.start_servers = 20//For dynamic mode, it defines the number of child processes that the PHP-FPM service produces when the service is started by the child process service.
    4. Pm.min_spare_servers = 5//For dynamic mode, define the minimum number of child processes during idle time, and if this value is reached, the PHP-FPM service automatically derives new child processes.
    5. Pm.max_spare_servers = 35//For the dynamic mode, define the maximum number of child processes in the idle period, or, if higher than this value, start cleaning up idle child processes.
    6. Pm.max_requests = 500//For the dynamic mode, define the maximum number of requests processed by a child process, that is, in a PHP-FPM child process can handle so many requests, when this value is reached, it automatically exits.

Linux php-fpm pool, slow execution log, process management, and Open_basedir introduction

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.