给不同的站点分别配置不同的pool
## 给两个站点分别配置不同的pool
[root@lnmp ~]# ps -ef |grep php
root 1195 1 0 02:15 ? 00:00:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 1209 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1211 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1212 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1213 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1214 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1215 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1216 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1219 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1220 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1221 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1222 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1228 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1229 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1230 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1231 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1232 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1233 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1234 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1235 1195 0 02:15 ? 00:00:00 php-fpm: pool www
php-fpm 1238 1195 0 02:15 ? 00:00:00 php-fpm: pool www
root 2537 2517 0 02:15 pts/0 00:00:00 grep --color=auto php
# 进入配置文件目录
[root@lnmp ~]# cd /usr/local/php-fpm/etc/
# 添加一个pool为123.com
[root@lnmp 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
[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
# 检查语法
[root@lnmp etc]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Aug-2021 07:13:29] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
# 重启
[root@lnmp etc]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
# 看到多了新的pool
[root@lnmp etc]# ps -aux |grep php
root 2997 0.4 0.2 231720 4880 ? Ss 07:14 0:00php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2998 0.0 0.2 231660 4608 ? S 07:14 0:00php-fpm: pool www
php-fpm 2999 0.0 0.2 231660 4608 ? S 07:14 0:00php-fpm: pool www
php-fpm 3000 0.0 0.2 231660 4608 ? S 07:14 0:00php-fpm: pool www
php-fpm 3001 0.0 0.2 231660 4608 ? S 07:14 0:00php-fpm: pool www
php-fpm 3002 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool www
php-fpm 3003 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool www
php-fpm 3004 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool www
php-fpm 3005 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool www
php-fpm 3006 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3007 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3008 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3009 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3010 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3011 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3012 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3013 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3014 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3015 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3016 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3017 0.0 0.2 231660 4616 ? S 07:14 0:00php-fpm: pool www
php-fpm 3018 0.2 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3019 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3020 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3021 0.0 0.2 231660 4612 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3022 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3023 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3024 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3025 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3026 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3027 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3028 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3029 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3030 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3031 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3032 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3033 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3034 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3035 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3036 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
php-fpm 3037 0.0 0.2 231660 4620 ? S 07:14 0:00php-fpm: pool 123.com
root 3039 0.0 0.0 112820 968 pts/1 S+ 07:14 0:00 grep --color=auto php
[root@lnmp etc]# cd /usr/local/nginx/conf/vhost/
[root@lnmp vhost]# vim default.conf
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;
}
}
# fastcgi_pass unix:/tmp/123.sock; 主要就是修改这一行配置文件选择不同的pool。
[root@lnmp vhost]# /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 vhost]# /usr/local/nginx/sbin/nginx -s reload
配置 拆分多个pool
在Nginx.conf
// 进入配置文件目录
[root@lnmp vhost]# cd /usr/local/php-fpm/etc/
// 加入一行include
[root@lnmp 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
// 创建存放配置文件的目录
[root@lnmp etc]# mkdir php-fpm.d
[root@lnmp etc]# cd php-fpm.d/
[root@lnmp 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
[root@lnmp php-fpm.d]# 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
[root@lnmp php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Aug-2021 07:40:02] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf
test is successful
// 重启服务
[root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
// 查看一下sock文件
[root@lnmp php-fpm.d]# ls /tmp/*.sock
/tmp/123.sock /tmp/mysql.sock /tmp/php-fcgi.sock
[root@lnmp php-fpm.d]# ps -ef |grep php
root 3075 1 0 07:40 ? 00:00:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 3076 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3077 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3078 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3079 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3080 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3081 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3082 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3083 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3084 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3085 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3086 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3087 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3088 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3089 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3090 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3091 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3092 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3093 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3094 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3095 3075 0 07:40 ? 00:00:00 php-fpm: pool 123.com
php-fpm 3096 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3097 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3098 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3099 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3100 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3101 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3102 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3103 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3104 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3105 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3106 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3107 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3108 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3109 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3110 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3111 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3112 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3113 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3114 3075 0 07:40 ? 00:00:00 php-fpm: pool www
php-fpm 3115 3075 0 07:40 ? 00:00:00 php-fpm: pool www
root 3118 2900 0 07:40 pts/1 00:00:00 grep --color=auto php
php-fpm 定义open_baseddir
# 修改配置文件,特意配置为错误路径
# 添加一行配置文件,其中test.com的路径为错误路径
[root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
php_admin_value[open_basedir]=/data/nginx/wwwtest.com:/tmp/
[root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
# 测试发现这些PHP文件都无法访问,就是因为php open_basedir做了限制。
[root@lnmp php-fpm.d]# ls /data/nginx/test.com/
1.js 1.jss 2.jpg 2.js 3.php admin index.html sleep.php
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 02:51:12 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 02:51:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
## 把配置文件改为正确的
## 发现可以正常使用
[root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
php_admin_value[open_basedir]=/data/nginx/test.com:/tmp/
[root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 02:57:52 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 02:58:00 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
配置错误日志
// 修改配置文件
[root@lnmp php-fpm.d]# 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
// 再次将php的配置文件改错
[root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
php_admin_value[open_basedir]=/data/nginx/wwwtest.com:/tmp/
// 这个日志文件不会自动生成,所以我们自己创建一个,并授权。
[root@lnmp php-fpm.d]# touch /usr/local/php-fpm/var/log/php_error.log
[root@lnmp php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 03:21:22 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 03:21:32 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
No input file specified.
// 查看错误日志,显示open_basedir限制生效,目录不能访问
[root@lnmp php-fpm.d]# 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
// 再次将配置文件修改回来
[root@lnmp php-fpm.d]# 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
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 = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/nginx/test.com:/tmp/
// 重启服务,可以正常使用,也没有产生错误日志。
[root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 03:25:43 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Thu, 19 Aug 2021 03:25:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30