php-fpm
php-fpm的pool
其中一个网站发生故障,会把php-pool资源耗尽,我们应该把站点隔离开来,使用不同的pool
1.修改php-fpm配置文件
[root@antong etc]# 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
[123.com]
listen = /tmp/123.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@antong etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
2.查看进程
[root@antong etc]# ps -aux | grep php
root 5251 0.0 0.2 232000 5068 ? Ss 22:55 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 5252 0.0 0.2 231940 4808 ? S 22:55 0:00 php-fpm: pool www
···省略
php-fpm 5272 0.0 0.2 231940 4816 ? S 22:55 0:00 php-fpm: pool 123.com
···省略
3.隔离站点,使用不同的pool
[root@antong etc]# vim /usr/local/nginx/conf/vhost/test.com.conf
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name
;
}
[root@antong etc]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
{
include fastcgi_params;
fastcgi_pass unix:/tmp/123.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name
;
}
4.分开管理pool配置文件
[root@antong ~]# 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@antong ~]# mkdir /usr/local/php-fpm/etc/php-fpm.d
[root@antong ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[www]
listen = /tmp/php-fcgi.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@antong ~]# vim /usr/local/php-fpm/etc/php-fpm.d/123.com.conf
[123.com]
listen = /tmp/123.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@antong ~]# /usr/local/php-fpm/sbin/php-fpm -t
[07-Sep-2021 23:21:09] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@antong ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@antong ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
php-fpm慢执行日志
php网站如果访问慢了,慢执行日志可以发现问题出现在哪里。
1.修改php配置文件
[root@antong ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf //在尾部加上下面两行
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
[root@antong ~]# /usr/local/php-fpm/sbin/php-fpm -t
[07-Sep-2021 23:29:09] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@antong ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@antong ~]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
2.模拟访问速度慢
[root@antong ~]# vim /data/wwwroot/test.com/sleep.php
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
3.测试查看慢执行日志
[root@antong ~]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone
[root@antong ~]# cat /usr/local/php-fpm/var/log/www-slow.log
[07-Sep-2021 23:35:09] [pool www] pid 5868
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007f660b71e2e0] sleep() /data/wwwroot/test.com/sleep.php:3
php-fpm定义open_basedir
1.修改php配置文件,进行测试
[root@antong ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf //在尾部加上以下内容
php_admin_value[open_basedir]=/data/wwwroot/wwwtest.com:/tmp/
[root@antong ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@antong ~]# curl -x127.0.0.1:80 test.com/3.php
No input file specified.
2.修改路径,再次测试
[root@antong ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf //将内容改正
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
[root@antong ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@antong ~]# curl -x127.0.0.1:80 test.com/3.php
//访问成功省略内容
3.查看错误日志
[root@antong ~]# vim /usr/local/php-fpm/etc/php.ini //修改以下内容
display_errors = Off
;error_log = syslog //搜索这行在下面加
error_log = /usr/local/php-fpm/var/log/php_error.log
error_reporting = E_ALL
[root@antong ~]# touch /usr/local/php-fpm/var/log/php_error.log
[root@antong ~]# chown 777 /usr/local/php-fpm/var/log/php_error.log
[root@antong ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
//改错配置文件后进行测试
[root@antong ~]# !curl
curl -x127.0.0.1:80 test.com/3.php
Access denied.
[root@antong ~]# cat /usr/local/php-fpm/var/log/php_error.log
[08-Sep-2021 03:51:15 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[08-Sep-2021 03:51:35 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[08-Sep-2021 03:52:51 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[08-Sep-2021 03:53:39 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
php-fpm进程管理
1.查看php-fpm配置文件
[root@antong ~]# cat /usr/local/php-fpm/etc/php-fpm.d/www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic //动态进程管理,也可以是static,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的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出
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/