标签:php-fpm配置相关
[toc]和LAMP不同的是,在LNMP架构中,php-fpm作为独立的一个服务存在,既然是独立服务,那么它必然有自己的配置文件。php-fpm的配置文件为/usr/local/php-fpm/etc/php-fpm.conf,它同样也支持include语句,类似于nginx.conf里面的include.
1. php-fpm 的pool
Nginx可以配置多个虚拟主机,php-fpm配置文件php-fpm.conf可以设置多个pool,每个pool可以监听一个端口,在其中一个pool资源耗尽,会导致其他站点无法访问资源,报502错误。有必要把站点进行分离,分别使用单独的pool。
[root@xavi ~]# vim /usr/local/php/etc/php-fpm.conf
[root@xavi ~]# cd /usr/local/php-fpm/etc/
[root@xavi etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php.ini
[root@xavi etc]# cat 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
#[www]
#listen = /tmp/php-fcgi.sock
#监听地址,也可以写:listen = 127.0.0.1:9000,本地监听,也可以监听其他IP:port
#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径
#listen.mode = 666
#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限
#user = php-fpm
#group = php-fpm
#定义php-fpm服务的用户
[xavi.com]
listen = /tmp/xavier.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
[19-Mar-2018 23:02:28] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@xavi etc]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@xavi etc]# ps aux |grep php-fpm
root 4684 0.0 0.2 227268 4984 ? Ss 23:03 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 4685 0.0 0.2 229292 5080 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4686 0.0 0.2 229292 5080 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4687 0.0 0.2 229292 5080 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4688 0.0 0.2 229292 5084 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4689 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4690 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4691 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4692 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4693 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4694 0.0 0.2 229292 5088 ? S 23:03 0:00 php-fpm: pool www
php-fpm 4705 0.0 0.2 229292 5088 ?
php-fpm 4809 0.0 0.2 229292 5084 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4810 0.0 0.2 229292 5084 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4811 0.0 0.2 229292 5084 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4812 0.0 0.2 229292 5084 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4813 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4814 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4815 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4816 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4817 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4818 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
php-fpm 4819 0.0 0.2 229292 5088 ? S 23:08 0:00 php-fpm: pool xavi.com
[root@xavi etc]# cd /usr/local/nginx/conf/vhost/
[root@xavi vhost]# ls
bcd.com.conf load.conf proxy.conf ssl.conf test.com.conf
[root@xavi vhost]# vim test.com.conf
[root@xavi vhost]# vim bcd.com.conf
server
{
listen 80 default_server;
server_name abcd.com;
index index.html index.htm index.php;
root /data/nginx/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/xavier.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/default$fastcgi_script_name;
}
[root@xavi php-fpm.d]# vi 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@xavi php-fpm.d]# vi xavi.conf
[xavi.com]
listen = /tmp/xavier.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
这样原有的php-fpm.conf就只有三行内容了
[root@xavi php-fpm.d]# cat ../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@xavi php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[20-Mar-2018 20:39:01] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@xavi php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
php网站,建议使用lnmp架构的一个好处就是查看“慢日志”,为什么这么说呢?当我们的客户或者manager发现站点访问速度点慢,如果全部的负载均不是很高,一切正常还是慢,怎么办?那唯一的办法就是检查“慢日志”,慢日志会向我们详细的展示某个页面的多少行代码在执行过程中超过了我们设定的阀值!
2.1 配置www.conf吧
在原有的基础上面,增加两行:
request_slowlog_timeout = 2
slowlog = /usr/local/php-fpm/var/log/www_slow.log
命令的意思是:只要执行某条语句超过2秒就会记录到咱们规定的日志文件中去。
开启慢执行日志
[root@xavi php-fpm.d]# 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 = 2
slowlog = /usr/local/php-fpm/var/log/www_slow.log
[root@xavi php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[20-Mar-2018 21:50:45] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@xavi php-fpm.d]# ls /usr/local/php-fpm/var/log/
php-fpm.log www-slow.log
[root@xavi php-fpm.d]# cat /usr/local/php-fpm/var/log/www-slow.log
[root@xavi nginx]# vim /data/nginx/test.com/sleep.
<?php
echo "slowlog test!";
sleep(2);
echo "done";
?>
错误报告:
[root@xavi nginx]# vim /data/nginx/test.com/sleep.php
[root@xavi nginx]# curl -x127.0.0.1:80 test.com/sleep.php
curl: (7) Failed connect to 127.0.0.1:80; 拒绝连接
设置了重复多个default 80端口,以及忘了}符号,以后测试文件及时删除
[root@xavi vhost]# curl -x127.0.0.1:80 test.com/sleep.php
slowlog test!done[root@xavi vhost]#
[root@xavi vhost]# cat /usr/local/php-fpm/var/log/www-slow.log
[20-Mar-2018 22:58:57] [pool www] pid 4662
script_filename = /data/nginx/test.com/sleep.php
[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1
[20-Mar-2018 22:59:49] [pool www] pid 4664
script_filename = /data/nginx/test.com/sleep.php
[0x00007f5860f422e8] sleep() /data/nginx/test.com/sleep.php:1 //第一行
[root@xavi vhost]# cat /data/nginx/test.com/sleep.php
<?php echo "slowlog test!"; sleep(2);echo "done"; ?>
错误分析2
识别出来是语法错误
如果一个server有很多个web服务,那么不建议直接把open_basedir直接配置在php.ini中了。Appache可以针对每个虚拟主机设置一个Open_basedir,php-fpm同样也可以针对不同的pool设置不同的Open_basedir.
[root@xavi vhost]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
vim /usr/local/php-fpm/etc/php.ini
增加如下配置:搜索/error_log
errot_log = /usr/local/php-fpm/var/log/php_error.log
[root@xavi vhost]# grep error_log /usr/local/php-fpm/etc/php.ini
; 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 = syslog
error_log = /usr/local/php-fpm/var/log/php_error.log
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log=
[root@xavi vhost]# touch /usr/local/php-fpm/var/log/php_error.log
[root@xavi vhost]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
[root@xavi vhost]# !curl
curl -x127.0.0.1:80 test.com/sleep.php
slowlog test!done[root@xavi vhost]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@xavi vhost]# curl -x127.0.0.1:80 test.com/sleep.php
No input file specified.
[root@xavi vhost]# !cat
cat /usr/local/php-fpm/var/log/php_error.log
[20-Mar-2018 15:38:25 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[20-Mar-2018 15:38:25 UTC] PHP Deprecated: Comments starting with ‘#‘ are deprecated in Unknown on line 1 in Unknown on line 0
[20-Mar-2018 15:38:31 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/wwtest.com:/tmp/) in Unknown on line 0
[20-Mar-2018 15:38:31 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[ ] pm = dynamic ;
动态进程管理,也可以是static,只有此处设置为dynamic,下面的配置才生效
[ ] pm.max_children = 50 ;
最大子进程数,ps aux可以查看
[ ] pm.start_serve = 20;启动服务时会启动的进程数
[ ] pm.min_spare_servers = 5;
定义空闲时段,子进程数的最小数量,如果达到这个数值时,php-fpm
;服务会自动派生新的子进程。
[ ] pm.max_spare_servers = 35 ;
定义在空闲时段,子进程的最大值,如果高于这个数值就开始清理空闲的子进程。
标签:php-fpm配置相关
原文地址:http://blog.51cto.com/12995218/2089236