mac下安装多版本php和配置php-fpm的一些坑

mac下安装多版本php和配置php-fpm的一些坑

phpbrew的安装

请参考phpbrew的安装

php -v和(php-fpm或 phpinfo())版本不一样

phpbrew安装php5.6.30,然后用phpbrew switch php-5.6.30切换成5.6.30

在终端下使用php -v

1
2
3
PHP 5.6.30 (cli) (built: May 14 2019 18:30:16)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

在终端下使用php-fpm -v

1
2
3
PHP 7.1.16 (fpm-fcgi) (built: Mar 31 2018 03:00:16)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

在web端如下

解决

添加php-fpm软链

1
sudo ln -s /Users/kb/.phpbrew/php/php-5.6.30/sbin/php-fpm /usr/sbin/php-fpm

如果/usr/sbin下存在php-fpm,就先把原来的删除

创建软连接以后,关闭终端,重新进入,分别运行php -vphp-fpm -v看下版本是否一致

1
2
3
4
5
6
7
8
9
php -v
PHP 5.6.30 (cli) (built: May 28 2019 16:01:05)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

php-fpm -v
PHP 5.6.30 (fpm-fcgi) (built: May 28 2019 16:01:14)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

配置完上面后,在web页面打开会遇到访问php页面404的问题

解决nginx访问php页面404问题

查看php5.6.30 sock的绝对路径

1
cat ~/.phpbrew/php/php-5.6.30/etc/php-fpm.conf | grep "listen = /"

然后把这个路径添加到nginx 的配置文件中,nginx 的配置文件在/usr/local/etc/nginx/nginx.conf

1
2
3
4
5
6
7
location ~ \.php$ {
root html;
fastcgi_pass unix:路径地址;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

解决Permission denied while connecting to upstream问题

过程中碰到[crit] 2963#0: *138 connect() to unix:/tmp/php5-fpm.sock failed (13: Permission denied) while connecting to upstream的问题, 这是由于没有指定nginx的用户, nginx的用户没有权限访问fpm造成的
/Users/kb/.phpbrew/php/php-5.6.30/etc/php-fpm.conf中找到listen行, 在后面添加

1
2
listen.owner = nobody
listen.group = nobody

并且在/usr/local/etc/nginx/nginx.conf中找到user行, 默认的是被注释掉的, 取消注释, 这时的用户是nobody, 重启nginx和fpm, 问题得到解决

然后就可以愉快地使用php+nginx 5.6版本了

我的nginx的配置

下面贴出我的nginx的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;
env PHPBREW_ROOT;

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 82;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /Users/xianyu123/PhpstormProjects;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /Users/xianyu123/PhpstormProjects;
# fastcgi_pass unix:/Users/xianyu123/.phpbrew/php/php-7.1.29/var/run/php-fpm.sock;
fastcgi_pass unix:/Users/xianyu123/.phpbrew/php/php-5.6.40/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}

Reference

php7 + nginx + mysql 安装小计

笔记:MAC使用brew配置nginx、php、mysql、php-fpm、redis

Mac OS下配置PHP Nginx PHP-FPM

本文标题:mac下安装多版本php和配置php-fpm的一些坑

文章作者:xianyu123

发布时间:2019年05月28日 - 19:01

最后更新:2020年11月13日 - 10:43

原始链接:http://0clickjacking0.github.io/2019/05/28/mac下安装多版本php和配置php-fpm的一些坑/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------    本文结束  感谢您的阅读    -------------