LNMP架构php-fpm相关配置

来自:互联网
时间:2020-03-26
阅读:

php-fpm的pool

php-fpm.conf可以设置多个pool,在其中一个pool资源耗尽,会导致其他站点无法访问资源,报502错误。有必要把站点进行分离,分别使用单独的pool。

  • 修改php-fpm配置文件代码
# 文件地址:/usr/local/php-fpm/etc/php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
...
# 新增一个pool代码设置
[test]
listen = /tmp/test.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
  • 修改虚拟主机配置文件
# 这里设置另外一个虚拟主机
# 实现不同虚拟主机访问资源pool的隔离,互不影响
[root@localhost etc]# cat /usr/local/Nginx/conf/vhost/aaa.com.conf 
server
{
	listen 80 default_server;
	index index.html index.htm index.php
	root /data/www/default;
        location ~ .php$
        {
            include fastcgi_params;
            # 这里写对应php-fpm.conf内设置的sock
            fastcgi_pass unix:/tmp/test.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/www/default$fastcgi_script_name;
        }
}
  • 重启php-fpm服务
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t
[... 19:37:17] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost etc]# /etc/init.d/php-fpm reload
Reload service php-fpm  done
  • 查看配置是否生效
# 已经可以看到创建的pool --- test
[root@localhost etc]# ps aux | grep php-fpm
root       2895  1.7  0.3 227208  5008 ?        Ss   19:37   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
...
php-fpm    2915  0.0  0.3 227148  4712 ?        S    19:37   0:00 php-fpm: pool www
php-fpm    2916  1.4  0.3 227148  4708 ?        S    19:37   0:00 php-fpm: pool test
...
root       2937  0.0  0.0 112680   976 pts/0    S+   19:37   0:00 grep --color=auto php-fpm

实现代码分离

在php-fpm.conf内添加如下代码就可以实现类似虚拟主机配置文件分离的效果。

[root@localhost 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
include = etc/php-fpm.d/*.conf

# 将原文件内的www、test pool模块分别写在不同的conf文件内
[root@localhost etc]# vim 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@localhost etc]# vim php-fpm.d/test.conf
[test]
listen = /tmp/test.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服务
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t
[... 20:19:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 查看pool是否配置成功
# 这里test池在前,www池在后
[root@localhost etc]# ps aux | grep php-fpm
root       3068  0.2  0.3 227236  4980 ?        Ss   20:20   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    3069  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3070  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3071  0.0  0.3 227176  4712 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3075  0.0  0.3 227176  4716 ?        S    20:20   0:00 
...
php-fpm    3079  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3080  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3081  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool test
php-fpm    3089  0.0  0.3 227176  4716 ?        S    20:20   0:00 php-fpm: pool www
...
php-fpm    3107  0.0  0.3 227176  4724 ?        S    20:20   0:00 php-fpm: pool www
php-fpm    3108  0.0  0.3 227176  4724 ?        S    20:20   0:00 php-fpm: pool www
root       3110  0.0  0.0 112680   976 pts/0    S+   20:20   0:00 grep --color=auto php-fpm

php-fpm慢执行日志

通过分析php的慢执行日志,可以得知php网站访问缓慢原因。

  • 修改php配置文件www.conf
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
[www]
# 这里监听的sock修改为www.sock
listen = /tmp/www.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
# 这里添加下面两行代码

# 设置slowlog间隔为1s,间隔超过1s会记录到慢执行日志内
# 实际工作中,可以设置为2s,1s太过短暂
request_slowlog_timeout = 1

# 指定slowlog文件存放路径
slowlog = /usr/local/php-fpm/var/log/www-slow.log
  • 重启php服务
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t
[... 20:43:24] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost etc]# /etc/init.d/php-fpm  reload
  • 修改虚拟主机配置文件
[root@localhost etc]# vim /usr/local/nginx/conf/vhost/test.com.conf
# 修改sock文件
fastcgi_pass unix:/tmp/www.sock;
...
  • 重启nginx服务
[root@localhost etc]# /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@localhost etc]# /usr/local/nginx/sbin/nginx -s reload
  • 设置测试php文件
[root@localhost etc]# vim /data/www/test.com/sleep.php 
<?php
echo "test slow log";
sleep(2);
echo "done";
?>

要查看具体的错误信息,可以开启php.ini内的display_error参数

  • 查看慢执行日志
[root@localhost etc]# cat /usr/local/php-fpm/var/log/www-slow.log 

[... 21:13:15]  [pool www] pid 3430
script_filename = /data/www/test.com/sleep.php
[0x00007f616ec772d8] sleep() /data/www/test.com/sleep.php:3

open_basedir参数设置

  • 相关配置代码
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 添加如下代码
php_admin_value[open_basedir]=/data/www/test.com:/tmp/

效果测试

  • 先测试错误配置时php脚本的效果
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 将添加的代码内站点错误书写为aaa.com
php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/

重启php-fpm服务

[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t
[... 18:35:50] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

使用curl测试网页访问

# 由于已经设置了open_basedir,网页无法访问
[root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Tue, ... 10:38:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
  • 修改正确后验证
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf 
# 将添加的代码错误书写为
php_admin_value[open_basedir]=/data/www/test.com:/tmp/

重启服务并验证

[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t
[... 18:39:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@localhost etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

# 成功访问
[root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, ... 10:39:20 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

php错误日志设置

修复php.ini配置文件,关闭display_errors参数

# 不在浏览器上显示错误信息
display_errors = Off

# 开启错误日志记录
log_errors = On
# 设置存放路径
error_log = /usr/local/php-fpm/var/log/php_errors.log
# 设置错误记录级别
error_reporting = E_ALL

为了防止错误日志无法创建并修改,最好先行创建并给予权限。

[root@localhost etc]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@localhost etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log 

为了产生错误信息,先故意写错php代码

[root@localhost etc]# touch /usr/local/php-fpm/var/log/php_errors.log
[root@localhost etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log 
php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/

访问网页,其错误信息如下

[root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Tue, ... 10:52:01 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

# 这里明确指出所要访问的网页不在aaa.com站点
[root@localhost etc]# cat /usr/local/php-fpm/var/log/php_errors.log 
[... 10:52:01 UTC] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/www/test.com/test.php) is not within the allowed path(s): (/data/www/aaa.com:/tmp/) in Unknown on line 0
[... 10:52:01 UTC] PHP Warning:  Unknown: fAIled to open stream: Operation not permitted in Unknown on line 0

修改正确后再查看日志(php文件已被解析)

[root@localhost etc]# cat /usr/local/php-fpm/var/log/php_errors.log 
...
[... 10:53:18 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/www/test.com/test.php on line 2

php-fpm进程管理

  • 进程管理
# 动态进程管理
pm = dynamic

# 静态进程管理
# 设置了静态进程管理的配置项只有紧跟该行的那项配置生效
pm = static
  • 最大子进程数
# 可以使用ps aux进行查看
pm.max_children = 50
  • 设置启动时开启的PHP进程数
# 值可以根据需要进行修改
pm.start_servers = 20
  • 在空闲时段,子进程数的最小值
# 如果达到这个数值,php-fpm服务会自动派生新的子进程
pm.min_spare_servers = 5
  • 定义在空闲时段,子进程数的最大值
# 如果高于这个数值,就开始清理空闲的子进程
pm.max_spare_servers = 35
  • 定义一个子进程最多处理的请求数
# 对于该项而言,超出该值的话进程会因此而退出
pm.max_requests = 500

我们需要根据实际应用时的需要对上述参数的值进行配置,配置后重启php-fpm服务使其生效即可。

返回顶部
顶部