V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fibroblast
V2EX  ›  PHP

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

  •  
  •   fibroblast · 14 天前 · 1347 次点击

    关于在 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 也不行 日志无东西

    42 条回复    2024-05-17 13:13:50 +08:00
    cwcc
        1
    cwcc  
       14 天前
    docker 的网络问题一般都是映射端口不当造成的。排查下端口映射是否有问题(-p )。

    题外话,mac 下搞 php 开发可以用下 Laravel Herd ,一键安装环境。
    moell
        2
    moell  
       14 天前
    这种坑我踩过几次,昨天晚上还在搞。注意两个地方。
    ```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
        3
    fibroblast  
    OP
       14 天前
    可是本地访问 9000 端口也不能访问啊,我主要还是想解决这个问题
    julyclyde
        4
    julyclyde  
       14 天前   ❤️ 1
    你为啥要 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
        5
    fibroblast  
    OP
       14 天前
    @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
        6
    moell  
       14 天前   ❤️ 1
    @fibroblast 抱歉,看错了。你的 nginx 和 php 位置和我的刚好相反。核心的点反正就是要告诉 php-fpm php 文件的位置 。个人觉得网络有问题。
    fibroblast
        7
    fibroblast  
    OP
       14 天前
    @julyclyde ngixn 内部监听 80 端口 外部 8088 映射 内部 80 端口
    wheat0r
        8
    wheat0r  
       14 天前   ❤️ 1
    php-fpm 在宿主机上,nginx 在容器里,nginx 怎么通过 127.0.0.1 访问 fastcgi ?
    fibroblast
        9
    fibroblast  
    OP
       14 天前
    @julyclyde 好吧 不能直接访问 9000 这个我确实没想到
    julyclyde
        10
    julyclyde  
       14 天前
    @fibroblast 你再想想,nginx 会知道自己被外边映射到多少端口,并把这个数字写在日志里么?
    fibroblast
        11
    fibroblast  
    OP
       14 天前
    @wheat0r 🤔️ 知道了
    fibroblast
        12
    fibroblast  
    OP
       14 天前
    @julyclyde 好的我想我应该明白了
    yulgang
        13
    yulgang  
       14 天前
    php-fpm 和 nginx 在同一个 docker 里吗?
    keyfunc
        14
    keyfunc  
       14 天前
    比较简单的方法是,fpm 监听一个 sock 文件,然后把这个文件映射给 docker 的 nginx ,通过 sock 通讯。
    不然就是 fpm 监听 docker 的网桥 ip ,一般是 172.17.0.1 ,通过这个 ip 通讯。
    andrewDDC
        15
    andrewDDC  
       14 天前
    brew 安装一条开发环境应该也不复杂。
    ysc3839
        16
    ysc3839  
       14 天前 via Android
    @keyfunc macOS 下 Docker 是跑在虚拟机内的,应该不能透传 Unix socket 。
    楼主写 127.0.0.1 也访问不到宿主机,访问的是 Docker 本机。
    fibroblast
        17
    fibroblast  
    OP
       14 天前
    @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
        18
    fibroblast  
    OP
       14 天前
    @ysc3839 好的好的 我为我愚蠢的行为搞到抱歉
    fibroblast
        19
    fibroblast  
    OP
       14 天前
    @yulgang 不是 fpm 在宿主机 nginx 在 docker
    wheat0r
        20
    wheat0r  
       14 天前
    @ysc3839 #16
    @keyfunc #14
    op 这个结构太费劲,我觉得如果不是为了研究这种结构,不如把 php-fpm 也跑进容器。
    yulgang
        21
    yulgang  
       14 天前
    @fibroblast #19 建议直接 brew 安装 php-fpm 和 nginx 按照你的 nginx 配置就没问题了。你的 php 和 nginx 不在同一个主机里,nginx 里配 127.0.0.1 nginx 肯定连不上。

    如果 mac 里运行 php-fpm , 可以将它的 sock 文件映射到 nginx 的 docker 容器内,然后 nginx 里配置相应的 sock 文件
    keyfunc
        22
    keyfunc  
       14 天前
    @ysc3839 好吧,我没试过 mac 下这么干,啊哈哈。
    fibroblast
        23
    fibroblast  
    OP
       14 天前
    @yulgang 好的感谢
    fibroblast
        24
    fibroblast  
    OP
       14 天前
    @wheat0r 那个我拉住你问下 因为我是刚刚接触 php 我想问下就是 如果把 php 也扔进 docker 那么 写代码是不是会很麻烦啊
    wheat0r
        25
    wheat0r  
       14 天前
    @fibroblast #24 应该说是没有任何区别,或者说,这里面的区别不是来自 php ,而是来自 docker 。
    你只是需要一个工作目录,对于 php 制作的网站而言,而这个工作目录是给 php 容器看的,你只需要把你写的东西放进这个目录,并且把目录映射给容器内的正确位置就好了。
    但是 macOS 的 docker 是套了一层虚拟化的,具体怎么做我也不是很清楚。
    julyclyde
        26
    julyclyde  
       14 天前
    @fibroblast FastCGI sent in stderr: "Primary script unknown" 是个好现象,虽然也没成,但至少这句话是 fastcgi 输出的,也就是 nginx 和 fastcgi 的通信这部分工作已经通了

    然后就是文件的问题了。nginx 并不在乎文件在不在,它只管朝 fastcgi 要结果。但是 fastcgi 那边是需要能读到 php 文件的。你的文件放在哪边了呢?
    halobugTurbo
        27
    halobugTurbo  
       14 天前
    你容器的端口 ports 映射了吗?
    halobugTurbo
        28
    halobugTurbo  
       14 天前
    @fibroblast PHP 在 docker 中运行,有个参数 volumes ,映射目录就行,与裸机开发是一样的。
    halobugTurbo
        29
    halobugTurbo  
       14 天前
    没看明白 PHP 或 nginx 那个是容器😭。 把你配置的 127.0.0.1 ,改成局域网下的 IP ( 192.168...)
    fibroblast
        30
    fibroblast  
    OP
       14 天前
    @wheat0r ok 了解了
    fibroblast
        31
    fibroblast  
    OP
       14 天前
    @julyclyde 文件要给 FastCGI 好的
    ranaanna
        32
    ranaanna  
       14 天前   ❤️ 1
    不明白为什么现今的人们都这么喜欢用 docker 。nginx 是一个轻量级应用,为什么不可以直接跑?另一方面直接一个轻量级 linux 虚拟机也未必占用更多内存吧,而且更加隔离。为什么一定要跑一个重新发明的虚拟化软件,再在上面运行别人打包的、塞进各种未必有用的库的组件的、需要额外配置的、不一定占用更少存储和运行空间的“容器”呢?
    gxvsko
        33
    gxvsko  
       14 天前
    nginx 在 docker 里
    php 在数组机
    docker 网络没调好可能会不通
    建议 php-fpm 直接 socket 方式 直接把文件映射进去后 nginx 代理
    QlanQ
        34
    QlanQ  
       14 天前
    刚刚接触 php ,刚刚接触 任何语言的时候,都不要花时间折腾 环境

    如果你用 laravel 做开发,有本地的 server 直接启动,如果是 hyperf 也可以 watch
    heysnakelis
        35
    heysnakelis  
       14 天前
    @fibroblast
    试试以下俩个办法(任一):
    1. nginx 容器 links 关键字链接 php 容器,然后修改 fastcgi_pass php:9000;
    2. 通过 networks 关键字定义网络信息,自定义 ipv4_address , 然后修改 fastcgi_pass 《 IP 》:9000;
    darklost
        36
    darklost  
       14 天前
    listen 地址 和转发地址 fastcgi_pass 问题 参考 laradock 吧
    darklost
        37
    darklost  
       14 天前
    你这个 nginx 容器要访问速度的 php-fpm ? php-fpm 监听 0.0.0.0 nginx 容器转发到 host.docker.internal 这个特殊域名吧 或者内网地址
    zhaoyihuaer
        38
    zhaoyihuaer  
       14 天前   ❤️ 1
    容器内访问主机地址 mac 是 docker.for.mac.host.internal ,但是你这个结构跑项目有点麻烦 nginx 的目录是容器的目录, 与外面还不一样 不如直接把 php 也放容器中
    yc8332
        39
    yc8332  
       14 天前
    找不到就是 nginx 中 fastcgi 的配置问题。网上找找就有了
    yangxiaopeipei
        40
    yangxiaopeipei  
       14 天前
    搞个 dnmp 吧 直接点
    evam
        41
    evam  
       14 天前
    本地调试,brew 安装的 php ?
    要不直接 php -S localhost:8087
    julyclyde
        42
    julyclyde  
       11 天前
    所以你想明白了吧
    如果一开始你没用 docker ,啥麻烦都不会遇到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1063 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:29 · PVG 03:29 · LAX 12:29 · JFK 15:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.