php-fpm& nginx在分隔的docker容器中,无法执行.php文件

时间:2016-10-11 10:55:21

标签: php nginx docker

我想在机器上使用docker设置完全LNMP环境。但分离的php-fpm&有一些问题。 nginx容器。

我做的是:

  1. 从docker.io中提取图片:

    docker pull php:7.1-fpm

    docker pull nginx

  2. 使用image:

    运行

    docker run -d --name php-fpm -v /data/Docker/php-fpm/configs/:/usr/local/etc/php-fpm.d -v / data / Docker / nginx / html: / var / www / html php:7.1-fpm

    docker run -d --name nginx -v / data / Docker / nginx / configs /:/ etc / nginx -v / data / Docker / nginx / html:/ var / www / html -p 80:80 - -link php-fpm nginx

  3. 所有目录&文件是755特权。

    下面的配置文件:

    nginx.conf

    server {
        listen  80  default_server;
        server_name SkyEyeLab;
        root    /var/www/html;
    
        fastcgi_read_timeout 90;
        location ~ \.php {
        fastcgi_pass    php-fpm:9000;
        }
    }
    

    php-fpm.conf(一些重要的配置部分):

    listen 0.0.0.0:9000
    listen.allowed_clients = any
    

    然后我检查了nginx&的环境。 php-fpm:

    [root@w-Lab01 ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
    6faf4a4b4f7e        nginx               "nginx -g 'daemon off"   19 minutes ago      Up 19 minutes       0.0.0.0:80->80/tcp, 443/tcp   nginx
    9a6caff831d3        php:7.1-fpm         "php-fpm"                20 minutes ago      Up 20 minutes       9000/tcp                      php-fpm
    [root@w-Lab01 ~]# docker exec 6faf4a4b4f7e ping -c3 php-fpm
    PING php-fpm (172.17.0.2): 56 data bytes
    64 bytes from 172.17.0.2: icmp_seq=0 ttl=64 time=0.081 ms
    64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.018 ms
    64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.041 ms
    --- php-fpm ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max/stddev = 0.018/0.047/0.081/0.026 ms
    [root@w-Lab01 ~]# docker exec 9a6caff831d3 ss -apn
    Netid  State      Recv-Q Send-Q     Local Address:Port       Peer Address:Port 
    tcp    LISTEN     0      128                    *:9000                  *:*      users:(("php-fpm",pid=1,fd=7))
    

    一切似乎都很好。然后我在/ data / Docker / nginx / html(在mginted到nginx的/ var / www / html目录下)创建一个带有以下内容的a.php:

    <?php
    phpinfo();
    ?>
    

    然后在网络浏览器中访问http://localhost:80/a.php。 但我只有一个空页面,我检查了nginx的access.log:

    [root@w-Lab01 ~]# docker logs 6faf4a4b4f7e
    220.181.171.120 - - [11/Oct/2016:10:25:11 +0000] "GET /a.php HTTP/1.1" 200 31 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
    220.181.171.120 - - [11/Oct/2016:10:25:12 +0000] "GET /a.php HTTP/1.1" 200 31 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
    220.181.171.120 - - [11/Oct/2016:10:31:58 +0000] "GET /a.php HTTP/1.1" 200 31 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
    220.181.171.120 - - [11/Oct/2016:10:31:59 +0000] "GET /a.php HTTP/1.1" 200 31 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
    

    似乎正确解析了a.php&amp;执行。但为什么我看不到网页浏览器中的phpinfo()结果?

2 个答案:

答案 0 :(得分:0)

好吧,在我更改了nginx的配置文件之后:

server {
    listen  80  default_server;
    server_name SkyEyeLab;
    root    /var/www/html;

    fastcgi_read_timeout 90;
    location ~ \.php {
    fastcgi_pass    php-fpm:9000;
    }
}

server {
    listen  80  default_server;
    server_name SkyEyeLab;
    root    /var/www/html;

    fastcgi_read_timeout 90;
    location ~ \.php {
    fastcgi_pass    php-fpm:9000;
    include fastcgi.conf;
    }
}

一切都很顺利。

fastcgi.conf是环境配置文件。你可以在你的nginx的config目录下检查(通常是/ etc / nginx),看看里面是否有fastcgi.conf或fastcgi_param。

答案 1 :(得分:0)

也许,如果你想使用名称&#34; php-fpm&#34;在nginx conf中,您需要像这样链接容器:

docker run -d --name php-fpm -v /data/Docker/php-fpm/configs/:/usr/local/etc/php-fpm.d -v /data/Docker/nginx/html:/var/www/html php:7.1-fpm

之后:

 docker run -d --name nginx --link php-fpm:php-fpm -v /data/Docker/nginx/configs/:/etc/nginx -v /data/Docker/nginx/html:/var/www/html -p 80:80 --link php-fpm nginx