php-fpm

一.php-fmp 的pool

1.给不同的站点分别配置不同的pool

ps -ef |grep php查看信息

oot       1218      1  0 08:22 ?        00:00:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    1221   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1222   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1223   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1224   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1225   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1226   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1228   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1230   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1232   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1233   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1234   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1235   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1236   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1237   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1238   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1239   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1240   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1242   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1243   1218  0 08:22 ?        00:00:00 php-fpm: pool www
php-fpm    1244   1218  0 08:22 ?        00:00:00 php-fpm: pool www
root       2312   2293  0 08:24 pts/0    00:00:00 grep --color=auto php

cd /usr/local/php-fpm/etc/(进入配置目录)

vim php-fpm.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

[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

/usr/local/php-fpm/sbin/php-fpm -t(检查一下)

/etc/init.d/php-fpm restart(重启)

Gracefully shutting down php-fpm . done
Starting php-fpm done

ps -aux |grep php(查看信息)

root       2325  0.0  0.2 231996  5080 ?        Ss   08:29   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm    2326  0.0  0.2 231936  4816 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2327  0.0  0.2 231936  4816 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2328  0.0  0.2 231936  4816 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2329  0.0  0.2 231936  4820 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2330  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2331  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2332  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2333  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2334  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2335  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2336  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2337  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2338  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2339  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2340  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2341  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2342  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2343  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2344  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2345  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool www
php-fpm    2346  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2347  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2348  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2349  0.0  0.2 231936  4824 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2350  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2351  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2352  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2353  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2354  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2355  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2356  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2357  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2358  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2359  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2360  0.0  0.2 231936  4828 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2361  0.0  0.2 231936  4832 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2362  0.0  0.2 231936  4832 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2363  0.0  0.2 231936  4832 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2364  0.0  0.2 231936  4832 ?        S    08:29   0:00 php-fpm: pool 123.com
php-fpm    2365  0.0  0.2 231936  4832 ?        S    08:29   0:00 php-fpm: pool 123.com
root       2367  0.0  0.0 112820   964 pts/0    S+   08:30   0:00 grep --color=auto php

cd /usr/local/nginx/conf/vhost/

vim default.conf(主要注意

fastcgi_pass unix:/tmp/123.sock;地址是否正确

server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/nginx/default;
location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/123.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
    }

}

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

2.配置拆分多个pool

cd /usr/local/php-fpm/etc/

vim 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
加入一行:
include = etc/php-fpm.d/*.conf


创建存放配置文件的目录

mkdir php-fpm.d

cd php-fpm.d/

vim 123.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

vim 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

/usr/local/php-fpm/sbin/php-fpm -t

/etc/init.d/php-fpm restart(重启)

ls /tmp/*.sock

/tmp/123.sock  /tmp/mysql.sock  /tmp/php-fcgi.sock

ps -ef |grep php

3.慢执行文件

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

/usr/local/php-fpm/sbin/php-fpm -t

/etc/init.d/php-fpm reload(重启)

ls /usr/local/php-fpm/var/log/

php-fpm.log  www-slow.log

cat /usr/local/php-fpm/var/log/www-slow.log(什么都没是空的)

vim /data/nginx/test.com/sleep.php(写一个慢执行文件)

<?php
echo "test slow log";
sleep(2);
echo "done";
?>

curl -x127.0.0.1:80 test.com/sleep.php

cat /usr/local/php-fpm/var/log/www-slow.log

[19-Aug-2021 10:27:09]  [pool www] pid 3243
script_filename = /data/nginx/test.com/sleep.php
[0x00007feda8c3c288] sleep() /data/nginx/test.com/sleep.php:3

4.php-fpm定义open_basedir

vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

php_admin_value[open_basedir]=/data/nginx/wwwtest.com:/tmp/(故意配错)

/etc/init.d/php-fpm restart(重启)

ls /data/nginx/test.com/

curl -x127.0.0.1:80 test.com/sleep.php -I

HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Mon, 13 Sep 2021 13:01:03 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

curl -x127.0.0.1:80 test.com/3.php -I

HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Mon, 13 Sep 2021 13:02:25 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

vim /usr/local/php-fpm/etc/php-fpm.d/www.conf(改回来)

php_admin_value[open_basedir]=/data/nginx/test.com:/tmp

/etc/init.d/php-fpm restart(重启)

curl -x127.0.0.1:80 test.com/3.php -I

HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Mon, 13 Sep 2021 13:05:53 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

curl -x127.0.0.1:80 test.com/sleep.php -I

HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Mon, 13 Sep 2021 13:06:32 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

5.配置错误日志

vim /usr/local/php-fpm/etc/php.ini

display_errors = Off

log_errors = On

error_log = /usr/local/php-fpm/var/log/php_error.log ;

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

error_reporting = E_ALL

 vim /usr/local/php-fpm/etc/php-fpm.d/www.conf(配置错误文件)

创建日志文件并且授权

touch /usr/local/php-fpm/var/log/php_error.log

chmod 777 /usr/local/php-fpm/var/log/php_error.log

curl -x127.0.0.1:80 test.com/3.php -I

状态码是404

curl -x127.0.0.1:80 test.com/sleep.php -I

状态码是404

curl -x127.0.0.1:80 test.com/sleep.php(显示open_basedir限制生效,目录不能访问)

cat /usr/local/php-fpm/var/log/php_error.log

[19-Aug-2021 03:21:22 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/3.php) is not within the allowed path(s): (/data/nginx/wwwtest.com:/tmp/) in Unknown on line 0 [19-Aug-2021 03:21:22 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0 [19-Aug-2021 03:21:32 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/wwwtest.com:/tmp/) in Unknown on line 0 [19-Aug-2021 03:21:32 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0 [19-Aug-2021 03:21:45 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/wwwtest.com:/tmp/) in Unknown on line 0 [19-Aug-2021 03:21:45 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0

 将配置文件改回来之后在重启服务PHP就会正常。

上一篇:徒手用 Docker 构建自己的 PHP 开发环境


下一篇:基于Docker搭建 Php-fpm + Nginx 环境