一、php-fpm的pool(连接池)
我们查看php的进程时,会发现,在最后一个pool的选项,而这个就是我们在php-fpm配置文件里写的一个连接池。
[root@lnmp ~]# ps aux | grep php-fpm
root 2158 0.0 0.4 227148 4952 ? Ss 15:57 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2164 0.0 0.4 227148 4708 ? S 15:57 0:00 php-fpm: pool www
php-fpm 2165 0.0 0.4 227148 4708 ? S 15:57 0:00 php-fpm: pool www
php-fpm 2166 0.0 0.4 227148 4708 ? S 15:57 0:00 php-fpm: pool www
当多个服务器使用同一池子时,一个连接池的资源可能耗尽时,可能别的服务器连接时报错502,这个时候就需要建立多个连接池供不同的服务器使用,每个连接池监听不通的sock,或者tcp/ip。
我们可以学nginx的虚拟主机一样,将连接池从主配置文件里拿出来,放到一个目录里,并将他们分开,方便管理
配置如下:
1.在配置文件里[global]加下面一行,并将之前的连接池删掉。
[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.conf
include = etc/php-fpm.d/*.conf
2.创建连接池文件
[root@lnmp ~]# mkdir /usr/local/php-fpm/etc/php-fpm.d
[root@lnmp ~]# cd /usr/local/php-fpm/etc/php-fpm.d
[root@lnmp php-fpm.d]# vim www.conf (将之前在配置文件里删除的连接池写到这里)
[www]
listen = /tmp/www.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
3.添加新的连接池 (注意连接池的名字不要一样,监听的sock和tcp/ip不一样,并且要到nginx的虚拟配置文件里修改php要监听的sock)
[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.d/lty.conf
[lty]
listen = /tmp/lty.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@lnmp ~]# vim /usr/local/nginx/conf/vhost/aaa.com.conf
server
{
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/lty.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
}
4.检查php配置文件以及重新启动php-fpm
[root@lnmp ~]# /usr/local/php-fpm/sbin/php-fpm -t
[18-Dec-2017 17:56:11] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@lnmp ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
5.ps aux查看进程 (因为太长的缘故,我只复制连接池的前2行)
[root@lnmp ~]# ps aux | grep php-fpm
root 2779 1.0 0.4 227236 4980 ? Ss 17:56 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2780 0.0 0.4 227176 4716 ? S 17:56 0:00 php-fpm: pool lty
php-fpm 2781 0.0 0.4 227176 4716 ? S 17:56 0:00 php-fpm: pool lty
..........................................................................................
php-fpm 2818 0.0 0.4 227176 4728 ? S 17:56 0:00 php-fpm: pool www
php-fpm 2819 0.0 0.4 227176 4728 ? S 17:56 0:00 php-fpm: pool www
root 2821 0.0 0.0 112684 972 pts/0 S+ 17:56 0:00 grep --color=auto php-fpm
二、php-fpm慢执行日志
如果要做一个php网站,会推荐你用lnmp架构,原因就是可以分析php-fpm慢执行日志。因为在做运维工程师的生涯中,老板或者客户会经常反馈用户网站访问慢,慢执行日志则会记录为什么网站访问会慢。对运维工作是一个很大的帮助
配置:
首先在php的配置文件里修改 (上一次我们讲连接池的时候已经将不同的池分开,以修改www.conf为例)
[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf (在配置文件里添加以下)
request_slowlog_timeout = 1 (连接超过1秒则记录日志)
slowlog = /usr/local/php-fpm/var/log/www-slow.log (日志存放地址)
2.检查配置文件语法错误已经重启服务。
[root@lnmp ~]# /usr/local/php-fpm/sbin/php-fpm -t
[18-Dec-2017 18:11:25] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@lnmp ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
3.配置主机test.com,让它使用的是www连接池
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
[root@lnmp ~]# vim /data/wwwroot/test.com/sleep.conf
<?php
echo "test slow log";
sleep(2);
echo "done";
?>
4.检查语法错误,并且重新加载nginx
[root@lnmp test.com]# /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@lnmp test.com]# /usr/local/nginx/sbin/nginx -s reload
5.我们在test.com的页面里写等待连接,以便观察结果
[root@lnmp test.com]# curl -x127.0.0.1:80 test.com/sleep.php
test slow logdone
[root@lnmp test.com]# cat /usr/local/php-fpm/var/log/www-slow.log (查看日志)
[18-Dec-2017 19:15:13] [pool www] pid 3344
script_filename = /data/wwwroot/test.com/sleep.php (这里显示那个文件会访问慢)
[0x00007f1ae63661f8] sleep() /data/wwwroot/test.com/sleep.php:3 (这里显示文件第几行慢)
三、php-fpm定义open_bashdir (将 PHP 所能打开的文件限制在指定的目录树,包括文件本身。)
[root@lnmp ~]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf (修改配置文件添加以下一列)
php_admin_value[open_basedir]=/data/wwwroot/wwtest.com:/tmp/ (为了方便后期效果,把地址写成错误的)
定义php-fpm的错误日志
[root@lnmp ~]# vim /usr/local/php-fpm/etc/php.ini
446: display_errors = Off (这一行的意思是将你的错误信息是否显示到浏览器上,不能让用户看到,所以一般是off)
575: error_log = /usr/local/php-fpm/var/log/php_errors.log (添加一行,定义记录错误日志的路径。)
449: error_reporting = E_ALL (定义日志级别,记录所有错误日志)
创建错误日志文件,修改权限,防止不能正常写入。
[root@lnmp ~]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@lnmp ~]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
重启php配置文件并且访问,查看错误日志
[root@lnmp ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm .^[[A done
Starting php-fpm done
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/1.php -I (报错404)
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Mon, 18 Dec 2017 12:35:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp ~]# cat /usr/local/php-fpm/var/log/php_errors.log (查看错误日志)
[18-Dec-2017 12:33:51 UTC] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /data/wwwroot/test.com/1.php on line 2
[18-Dec-2017 12:34:11 UTC] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /data/wwwroot/test.com/1.php on line 2
[18-Dec-2017 12:35:09 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/1.php) is not within the allowed path(s): (/data/wwwroot/wwtest.com:/tmp/) in Unknown on line 0
[18-Dec-2017 12:35:09 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
四、php-fpm进程管理
我们打开一个php-fpm的配置文件。
[root@lnmp ~]# cat /usr/local/php-fpm/etc/php-fpm.d/www.conf
[www]
listen = /tmp/www.sock
listen.mode=666
user = php-fpm
group = php-fpm
pm = dynamic (进程以动态的形式启动,以下是他的配置,可以用静态static,那下面只有pm.max_children一行生效,一直保持50个子进程)
pm.max_children = 50 (最大子进程数)
pm.start_servers = 20 (一开始启动20个子进程)
pm.min_spare_servers = 5 (定义空闲时段,最低不能低于5个子进程,如果达不到,会自动派生新的子进程)
pm.max_spare_servers = 35 (定义空闲时段,最大值不能超过35个,如果高于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/wwtest.com:/tmp/