12.21 php-fpm的pool (池子,支持定义多个池子)
配置多个pool
[root
@test ~]# cat /usr/local/php-fpm/etc/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
[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
[root
@test ~]# vim /usr/local/php-fpm/etc/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
[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
[test]
listen = /tmp/test.sock
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
检测重启
[root
@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 11:00:59] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root
@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
不同站点定义使用不同池子
第一个站点
[root
@test ~]# vim /usr/local/nginx/conf/vhost/2.com.conf
server
{
listen 80;
index index.html index.htm index.php;
root /data/wwwroot/2.com;
if ($host != '2.com' ) {
rewrite ^/(.*)$
http://2.com/$1 permanent;
}
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/test.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/data/wwwroot/2.com$fastcgi_script_name;
}
location /admin/
{
allow 127.0.0.1;
deny all;
}
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.2.com ;
if ($invalid_referer) {
return 403;
}
access_log off;
}
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
# {
# expires 7d;
# access_log off;
# }
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
}
第二个站点
[root@test ~]# vim /usr/local/nginx/conf/vhost/1.com.conf
server
{
listen 80 default_server;
server_name 1.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/data/wwwroot/default$fastcgi_script_name;
}
}
检测加载
[root@test ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test ~]# /usr/local/nginx/sbin/nginx -s reload
第二种配置方式
php-fpm支持include
[root@test ~]# vim /usr/local/php-fpm/etc/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
[root@test ~]# mkdir /usr/local/php-fpm/etc/php-fpm.d
[root@test ~]# vim /usr/local/php-fpm/etc/php-fpm.d/
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
[root@test ~]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf
[test]
listen = /tmp/test.sock
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
检测加载
[root@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 11:28:22] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
12.22 php-fpm慢执行日志 (php网站lnmp,可以分析慢执行日志)
[root@test ~]# vim /usr/local/php-fpm/etc/php-fpm.d/
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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
检测加载
[root@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 11:37:27] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@test ~]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
使用这个池子的站点
[root@test ~]# cat /usr/local/nginx/conf/vhost/1.com.conf
server
{
listen 80 default_server;
server_name 1.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/data/wwwroot/default$fastcgi_script_name;
}
}
[root@test ~]# vim /data/wwwroot/default/
www.php
<?php
echo “test slow log”;
sleep(2);
echo “done”;
?>
配置有错误
[root@test ~]# curl -x127.0.0.1:80 1.com/
www.php -I
HTTP/1.1 500 Internal Server Error
Server: nginx/1.14.2
Date: Wed, 26 Dec 2018 03:45:26 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.38
查看错误(浏览器显示错误信息)
[root@test ~]# vim /usr/local/php-fpm/etc/php.ini
检测加载
[root@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 11:47:48] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
错误信息
[root@test ~]# curl -x127.0.0.1:80 1.com/
www.php
<br />
<b>Parse error</b>: syntax error, unexpected 'slow' (T_STRING), expecting ',' or ';' in <b>/data/wwwroot/default/
www.php</b> on line <b>2</b><br />
修改(标点不是英文)
[root@test ~]# vim /data/wwwroot/default/
www.php
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
结果
[root@test ~]# curl -x127.0.0.1:80 1.com/
www.php
test slow logdone[root@test ~]#
[root@test ~]# cat /usr/local/php-fpm/var/log/www-slow.log
[26-Dec-2018 11:50:21] [pool www] pid 1589
script_filename = /data/wwwroot/default/
www.php
[0x00007f09965282d0] sleep() /data/wwwroot/default/
www.php:3 (表示第三行慢)
12.23 open_basedir (
open_basedir 的作用是限制php在指定的目录里活动)
[root@test ~]# vim /usr/local/php-fpm/etc/php-fpm.d/
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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/default:/tmp/
[root@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 12:11:41] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@test ~]# curl -x127.0.0.1:80 1.com/index.html
hello !! nginx!!
root@test ~]# !curl
curl -x127.0.0.1:80 1.com/
www.php
test slow logdone[root@test ~]#
定义php-fpm错误日志
[root@test ~]# vim /usr/local/php-fpm/etc/php.ini
第一步不能显示在浏览器上
第二步 错误日志记录在服务器某个日志中
第三步 定义错误日志级别
手动生成日志
[root@test ~]# touch /usr/local/php-fpm/var/log/php_errors.log
权限777
[root@test ~]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
检测加载
[root@test ~]# /usr/local/php-fpm/sbin/php-fpm -t
[26-Dec-2018 12:23:59] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@test ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
12.24 php-fpm进程管理
pm = dynamic //动态进程管理,也可以是static(静态,只有pm.max_children = 50参数生效)
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的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。
[root@test ~]# cat /usr/local/php-fpm/etc/php-fpm.d/
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 进程以什么形式启动 dynamic动态(下面的参数才会生效),自动生成几个,当访问量大时自动增加子进程,空闲的话还会自动销毁,销毁到一定程度(5个)还会自动生成(由下面参数来定义)
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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/default:/tmp/