0
votes

I'd like to setup an completely LNMP environment with docker on a machine. But there is something wrong with separated php-fpm & nginx container.

What I'v done is :

  1. pull images from docker.io :

    docker pull php:7.1-fpm

    docker pull nginx

  2. run with 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

All directories & files are 755 privileges.

Config files r below :

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;
    }
}

And php-fpm.conf(some of important config sections):

listen 0.0.0.0:9000
listen.allowed_clients = any

Then I checked the environment of 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))

Everything seems fine. Then I create a.php under /data/Docker/nginx/html(which is mouted to nginx's /var/www/html directory) with following content :

<?php
phpinfo();
?>

Then access http://localhost:80/a.php in web browser. But I only got an empty page, I checked the access.log of nginx :

[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"

It seems a.php is correctly parsed & executed. But why I can not see phpinfo() result in web browser ?

2

2 Answers

1
votes

well, after I changed nginx's config file :

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

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

to

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;
    }
}

erverything goes fine.

fastcgi.conf is environment config file. you can check under your nginx's config directory(usually /etc/nginx), and see if there is fastcgi.conf or fastcgi_param in it.

0
votes

maybe, if you want to use the name "php-fpm" in the nginx conf, you need to link the container like this:

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

and after:

 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