关于在 mac 下 PHP -fpm 无法访问的问题[救救孩子吧]

40 天前
 fibroblast

关于在 mac 下 php-fpm 无法访问的问题


php 8.2 brew 本地安装 nginx 是 docker 请排除 nginx 找不到 index.php 文件的问题 因为在 nginx html 目录下有个 demo.html 文件可以正常访问 即 静态资源可以加载

pm-conf

[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
[www]
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 20
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 10
request_slowlog_timeout = 30
slowlog = log/php-fpm-slow.log

nginx-conf

server {
    listen       80;	#监听 80 端口
    listen  [::]:80;
    server_name  localhost;		#也可以填写自己注册的域名
    location / {
        root   /usr/share/nginx/html;	#当前配置的页面文件根目录
        index  index.php index.html index.htm;	#添加 index.php 作为默认首页
    }
    #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   /usr/share/nginx/html;
    }
    # 与 php-fpm 通信的关键设置
    location ~ \.php$ {
         root   /usr/share/nginx/html;	#页面文件根目录
         fastcgi_pass   127.0.0.1:9000;	#php-fpm 的通信端口,由于已经将容器 9000 端口映射到了主机的 9000 端口,所以这里填“主机 ip:9000”也是可以的。
         fastcgi_index  index.php;		#默认主页文件设置
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
         include        fastcgi_params;
    }
}

nginx 报错

2024/05/14 03:46:10 [error] 25#25: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.65.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:8088"

fpm 日志 只有启动

[14-May-2024 12:05:48] NOTICE: Terminating ...
[14-May-2024 12:05:48] NOTICE: exiting, bye-bye!
[14-May-2024 12:05:48] NOTICE: fpm is running, pid 18393
[14-May-2024 12:05:48] NOTICE: ready to handle connections

直接访问 http://127.0.0.1:9000 也不行 日志无东西

1638 次点击
所在节点    PHP
43 条回复
cwcc
40 天前
docker 的网络问题一般都是映射端口不当造成的。排查下端口映射是否有问题(-p )。

题外话,mac 下搞 php 开发可以用下 Laravel Herd ,一键安装环境。
moell
40 天前
这种坑我踩过几次,昨天晚上还在搞。注意两个地方。
```php
location ~ \.php$ {
root /www/laravel/public; #必须为 docker 内部地址
fastcgi_pass 127.0.0.1:9013;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /www/laravel/public$fastcgi_script_name;
include fastcgi_params;
}
```
fibroblast
40 天前
可是本地访问 9000 端口也不能访问啊,我主要还是想解决这个问题
julyclyde
40 天前
你为啥要 http 访问 9000 ?
9000 并不是 http 服务啊

说白了你这事就是因为你不懂 docker 还非要用 docker 导致的。网络没搞通
但凡认真看过错误提示信息 connect() failed (111: Connection refused)也知道不可能是 nginx 找不到 index.php 文件的问题,并不需要特地去提示。而且 fastcgi 模式下其实 nginx 根本不在乎有没有那个文件。nginx 并不是把文件送去 fastcgi 之星的,而是直接找 fastcgi 要执行结果


我给个提示吧:
你的 nginx 是 listen80 ,但是错误信息里 host: "127.0.0.1:8088"
你到底装了几个 nginx ?
fibroblast
40 天前
@moell 按照您的意思
改为了
···
location ~ \.php$ {
root 172.17.0.2; #页面文件根目录
fastcgi_pass 127.0.0.1:9000; #php-fpm 的通信端口,由于已经将容器 9000 端口映射到了主机的 9000 端口,所以这里填“主机 ip:9000”也是可以的。
fastcgi_index index.php; #默认主页文件设置
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
···
好像没有作用啊
moell
40 天前
@fibroblast 抱歉,看错了。你的 nginx 和 php 位置和我的刚好相反。核心的点反正就是要告诉 php-fpm php 文件的位置 。个人觉得网络有问题。
fibroblast
40 天前
@julyclyde ngixn 内部监听 80 端口 外部 8088 映射 内部 80 端口
wheat0r
40 天前
php-fpm 在宿主机上,nginx 在容器里,nginx 怎么通过 127.0.0.1 访问 fastcgi ?
fibroblast
40 天前
@julyclyde 好吧 不能直接访问 9000 这个我确实没想到
julyclyde
40 天前
@fibroblast 你再想想,nginx 会知道自己被外边映射到多少端口,并把这个数字写在日志里么?
fibroblast
40 天前
@wheat0r 🤔️ 知道了
fibroblast
40 天前
@julyclyde 好的我想我应该明白了
yulgang
40 天前
php-fpm 和 nginx 在同一个 docker 里吗?
keyfunc
40 天前
比较简单的方法是,fpm 监听一个 sock 文件,然后把这个文件映射给 docker 的 nginx ,通过 sock 通讯。
不然就是 fpm 监听 docker 的网桥 ip ,一般是 172.17.0.1 ,通过这个 ip 通讯。
andrewDDC
40 天前
brew 安装一条开发环境应该也不复杂。
ysc3839
40 天前
@keyfunc macOS 下 Docker 是跑在虚拟机内的,应该不能透传 Unix socket 。
楼主写 127.0.0.1 也访问不到宿主机,访问的是 Docker 本机。
fibroblast
40 天前
@julyclyde 我理解您的意思 目前我把 nginx 改为了
···
location ~ \.php$ {
root /usr/share/nginx/html; #页面文件根目录
fastcgi_pass host.docker.internal:9000; #php-fpm 的通信端口,由于已经将容器 9000 端口映射到了主机的 9000 端口,所以这里填“主机 ip:9000”也是可以的。
fastcgi_index index.php; #默认主页文件设置
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
···

nginx error
```
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.65.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://192.168.65.254:9000", host: "127.0.0.1:8088"
```
fibroblast
40 天前
@ysc3839 好的好的 我为我愚蠢的行为搞到抱歉
fibroblast
40 天前
@yulgang 不是 fpm 在宿主机 nginx 在 docker
wheat0r
40 天前
@ysc3839 #16
@keyfunc #14
op 这个结构太费劲,我觉得如果不是为了研究这种结构,不如把 php-fpm 也跑进容器。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1040588

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX