51. php-fpm pool, php-fpm slow execution log, open_basedir, php-fpm

51. php-fpm pool, php-fpm slow execution log, open_basedir, php-fpm process management

1. The pool of php-fpm

pool: Right. The column on the far right of ps aux. It's its pool.

# cd /usr/local/php-fpm/etc

# cat php-fpm.conf //Support defining multiple pools

[global]

pid = /usr/local/php-fpm/var/run/php-fpm.pid

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

[www] //This is a pool

listen = /tmp/php-fcgi.sock

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024


Each pool can listen on a different socket or TCP/IP.

     If Nginx has several sites, each site can use a pool, the advantage of this is that when a certain php502 (502 is likely to be insufficient PHP resources), if many sites use the same pool, if Which programmer writes something wrong, it will exhaust PHP resources, so other sites will 502, so it is necessary to use a separate pool for each site .


You can continue to add pools in php-fpm:

# vim php-fpm.conf //Add a new one

[mrx.com]

listen = /tmp/mrx.sock

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024

# /usr/local/php-fpm/sbin/php-fpm -t

[29-Apr-2018 22:10:01] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@MRX etc]# /etc/init.d/php-fpm reload

Reload service php-fpm  done

# ps aux |grep php-fpm //You can see www and mrx.com on the far right

php-fpm    1536  0.0  0.2 227228  4708 ?        S    22:11   0:00 php-fpm: pool www

php-fpm    1537  0.0  0.2 227228  4708 ?        S    22:11   0:00 php-fpm: pool www

php-fpm    1538  0.0  0.2 227228  4708 ?        S    22:11   0:00 php-fpm: pool www

php-fpm    1539  0.0  0.2 227228  4704 ?        S    22:11   0:00 php-fpm: pool mrx.com

php-fpm    1540  0.0  0.2 227228  4704 ?        S    22:11   0:00 php-fpm: pool mrx.com

php-fpm    1541  0.0  0.2 227228  4704 ?        S    22:11   0:00 php-fpm: pool mrx.com

how to use:

# cd /usr/local/nginx/conf/vhost/

# vim test.com.conf

location ~ \.php$

{

include fastcgi_params;

fastcgi_pass unix: /tmp/php-fcgi.sock ; set php-fcgi.sock here

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

}

# vim aaa.com.conf

location ~ \.php$ //This way the two sites are isolated

{

include fastcgi_params;

fastcgi_pass unix:/tmp/mrx.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;

}

If test.com.conf has 50 requests, and the request is full, the request will report 502, because there is no extra PHP to serve it, and the mrx.com site is not affected because it uses another pond.


# tail ../nginx.conf

tcp_nodelay on;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 8k;

gzip_comp_level 5;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

include vhost/*.conf ; nginx has this, php-fpm also supports

}

# cd /usr/local/php-fpm/etc

# vim php-fpm.conf

[global]

pid = /usr/local/php-fpm/var/run/php-fpm.pid

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

include = etc/php-fpm.d/*.conf

Add one in global.

Then split the contents of the pool below. Copy the entire content below first and then delete it.

# mkdir php-fpm.d

[root@MRX etc]# cd php-fpm.d/

        //Because php-fpm.d was just defined, create a directory

[root@MRX php-fpm.d]# vim www.conf

[www]

listen = /tmp/php-fcgi.sock

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024

# vim mrx.conf

[mrx.com]

listen = /tmp/mrx.sock

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

# /usr/local/php-fpm/sbin/php-fpm -t

[29-Apr-2018 23:32:09] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

# /etc/init.d/php-fpm restart

Gracefully shutting down php-fpm . done

Starting php-fpm  done

Then ps aux | grep php-fpm can see, it is the same effect, mrx.com is in the front, www is in the back.


二、php-fpm慢执行日志(非常有用)

做PHP的网站,建议用LNMP,其中一个原因就是可以分析php的慢执行日志, 网站慢可以通过慢执行日志分析。

# cd /usr/local/php-fpm/etc/php-fpm.d/

# vim www.conf

request_slowlog_timeout = 1  //意思是超过1秒钟就记录日志,一般设置2秒适合

//这两行加在最后面

slowlog = /usr/local/php-fpm/var/log/www-slow.log      //日志的路径

# /usr/local/php-fpm/sbin/php-fpm -t

[01-May-2018 16:42:04] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@MRX php-fpm.d]# /etc/init.d/php-fpm reload

Reload service php-fpm  done

# ls /usr/local/php-fpm/var/log/   //检查日志有没有生成

php-fpm.log  www-slow.log

配置Nginx的虚拟主机test.com.conf,把unix:/tmp/php-fcgi.sock改为unix:/tmp/www.sock,再重新加载Nginx服务。

# vim /data/wwwroot/test.com/sleep.php

<?php

echo "test slow log";

sleep(2);

echo "done";

?>

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

display_errors = On  这里将off修改为on就可以在浏览器上看错误日志,线上这里要off掉,不能让别人在浏览器上看到你的错误信息。

# curl -x127.0.0.1:80 test.com/sleep.php  测试,会慢2秒钟,然后记录进了慢执行日志。

# cat /usr/local/php-fpm/var/log/www-slow.log

sleep() /data/wwwroot/test.com/sleep.php:3  

这行代表这个文件的第3行慢了。所以如果网站慢了就可以查看这个慢执行日志slow.log。


三、php-fpm定义open_basedir

      之前在php.ini中定义过open_basedir,如果服务器上有多个网站,在php.ini中再去定义就不合适了。可以针对每个池子做open_basedir。

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

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/  加这行配置,当这行配置写错时,就会404,此处正确的是test.com。

# /etc/init.d/php-fpm restart

# curl -x127.0.0.1:80 test.com/3.php -I   写错时的效果。

HTTP/1.1 404 Not Found

# curl -x127.0.0.1:80 test.com/3.php -I   正确时的效果。

HTTP/1.1 200 OK

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

display_errors = Off     on改回off,线上这里要off掉,不能让别人在浏览器上看到你的错误信息。

log_errors = On    正确的做法是把错误信息记录在服务器的某个文件里。

;error_log = syslog   定义在这一行的下面。

error_log = /usr/local/php-fpm/var/log/php_errors.log  新增加的一行。

error_reporting = E_ALL    定义日志级别,全部记录。

# ls /usr/local/php-fpm/var/log/ 先检查有没有php_errors.log这个文件

php-fpm.log  www-slow.log

# touch /usr/local/php-fpm/var/log/php_errors.log      没有就touch一个

# chmod 777 /usr/local/php-fpm/var/log/php_errors.log  权限改777,防止不能正常的写入。

当www.com.conf写错时:

php_admin_value[open_basedir]=/data/wwwroot/wwtest.com:/tmp/

# /etc/init.d/php-fpm restart

# cat /usr/local/php-fpm/var/log/php_errors.log   看错误日志

[02-May-2018 21:48:14 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/wwtest.com:/tmp/) in Unknown on line 0  不是wwtest.com

# vim www.com.conf

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/

改回test.com


四、php-fpm进程管理

[root@MRX php-fpm.d]# cat www.conf

[www]

listen = /tmp/php-fcgi.sock

#listen = 127.0.0.1:9000

listen.mode = 666

user = php-fpm

group = php-fpm

pm = dynamic     //动态进程管理,可以是static(静态),dynamic(动态)。

pm.max_children = 50     //最大子进程数,ps aux可以查看。static时只有这一行生效,用static时直接启动时就50个,所以需使用动态。

pm.start_servers = 20      //启动服务时会启动的进程数

pm.min_spare_servers = 5    //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm会自动派生新的子进程。

pm.max_spare_servers = 35  //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。

pm.max_requests = 500   //定义同一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。如果不退出这个子进程就会一直存在,万一这个子进程有问题。

rlimit_files = 1024

request_slowlog_timeout = 1

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

php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/


# ps aux |grep mrx.com |wc -l

21    这里是因为mrx.com定义了启动时会启动的进程数是20,如果不使用这些进程,一会儿这些空闲进程会销毁,销毁限度,最低会保留5个子进程在。

# ps aux |grep php-fpm |wc -l

42

这些参数都可以自定义成自己的需求。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325198160&siteId=291194637