Docker:Nginx和php5-fpm码头工人没有说话 [英] Docker: Nginx and php5-fpm dockers are not talking

查看:288
本文介绍了Docker:Nginx和php5-fpm码头工人没有说话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个完全Dockerized的Drupal安装。我的第一步是使用Nginx和php5-fpm来运行容器,这两种都是基于Debian的。我在CoreOS Alpha频道(使用数码海洋)。



我的Dockerfiles如下:



Nginx :

  FROM debian 
MAINTAINER fvhemert
RUN apt-get update&& apt-get install -y nginx&&&回声\\\
daemon关闭 >> /etc/nginx/nginx.conf
CMD [nginx]
EXPOSE 80

此容器构建并运行良好。我看到我的服务器ip上的默认Nginx页面。



Php5-fpm:

  FROM debian 
MAINTAINER fvhemert
RUN apt-get update&& apt-get install -y \
php5-fpm \
&&& sed's /; daemonize = yes / daemonize = no /'-i /etc/php5/fpm/php-fpm.conf
CMD [php5-fpm]
EXPOSE 9000

此容器也构建无问题,并在启动时持续运行。



我首先启动php5-fpm容器:

  docker run -d  -  name php5-fpm freek / php5-fpm:1 

然后我开始Nginx ,,链接到php5-fpm:

  docker run -d -p 80:80  -  link php5-fpm:phpserver --name nginx freek / nginx-php:1 

链接似乎工作,在/ etc / hosts中有一个名为phpserver的条目。两个码头运行:

  core @ dockertest〜$ docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fd1a9ae0f1dd freek / nginx-php:4nginx38分钟前Up 38分钟0.0.0.0:80->80/tcp nginx
3bd12b3761b9 freek / php5-fpm:2php5-fpm38分钟前up 38分钟9000 / tcp php5-fpm

我已经调整了一些配置文件。对于Nginx容器,我编辑/ etc / nginx / sites-enabled / default并更改:

  server {
#听80 ##听ipv4;这行是默认的,隐含的
#listen [::]:80 default_server ipv6only = on; ##听ipv6

root / usr / share / nginx / www;
index index.html index.htm index.php;

(我添加了index.php)



进一步说:

  location〜\.php $ {
fastcgi_split_path_info ^(。+ \。 PHP)(/.+)$;
##注意:你应该有cgi.fix_pathinfo = 0;在php.ini

##单独使用php5-cgi:
fastcgi_pass phpserver:9000;
##用php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
包含fastcgi_params;
}

在php5-fpm码头中我更改了/ etc / php5 / fpm / php .ini:

  cgi.fix_pathinfo = 0 

php5-fpm运行:

  [2014年11月21日06:15: 29]注意:fpm正在运行,pid 1 
[2014年11月21日06:15:29]注意:准备处理连接

我还将index.html更改为index.php,看起来像这样(/usr/share/nginx/www/index.php):

 < html> 
< head>
< title>欢迎使用nginx!< / title>
< / head>
< body bgcolor =whitetext =black>
< center>< h1>欢迎使用nginx!< / h1>< / center>

<?php
phpinfo();
?>

< / body>
< / html>

我已经从Nginx码头处扫描了9000端口,它似乎是关闭的。当然不是一个好兆头:

  root @ fd1a9ae0f1dd:/#nmap -p 9000 phpserver 

启动Nmap 6.00(http://nmap.org)在2014-11-21 06:49 UTC
phpserver的Nmap扫描报告(172.17.0.94)
主机已启动(0.00022秒延迟)。
端口状态服务
9000 / tcp closed cslistener
MAC地址:02:42:AC:11:00:5E(未知)

Nmap done:1 IP地址(1个主机)以0.13秒扫描

Nginx日志:

  root @ fd1a9ae0f1dd:/#vim /var/log/nginx/error.log 
2014/11/20 14:43:46 [error ] 13#0:* 1 connect()failed(111:Connection refused)while connection to upstream,client:194.171.252.110,server:localhost,request:GET / HTTP / 1.1,upstream:fastcgi://127.0 .0.1:9000,主机:128.199.60.95
2014/11/21 06:15:51 [错误] 9#0:* 1连接()连接(111:连接被拒绝)连接到上游,client:145.15.244.119,server:localhost,request:GET / HTTP / 1.0,上游:fastcgi://172.17.0.94:9000,主机:128.199.60.95

我的问题是:究竟出了什么问题?我的猜测是,我在php配置文件中缺少一些设置。



编辑更多详细信息:
这是结果(从php5- fpm容器,apt-get install net-tools之后):


root @ 3bd12b3761b9:/#netstat -tapen

活动的互联网连接

(服务器和已建立)Proto Recv-Q Send-Q本地地址

外部地址状态用户Inode PID /程序名称


从Nginx容器内部:


root @ fd1a9ae0f1dd:/#netstat - tapen

活动的Internet连接

(服务器和已建立)Proto Recv-Q Send-Q本地地址外部地址状态用户Inode PID /程序
name tcp 0 0 0.0。 0.0:80 0.0.0.0:*

LISTEN 0 1875387 -


EDIT2:
进步!



在php5-fpm con在文件中:

  /etc/php5/fpm/pool.d/www.conf 

我将某个套接字名称的listen参数更改为:

  listen = 9000 

现在,当我去我的网页时,我得到错误:
没有指定输入文件。



可能我在某处有拖尾/错误。我会更仔细地研究一下!



EDIT3:



所以我用上面的重建了码头他们正在说话。但是,我的网页告诉我:找不到文件。
我非常确定它与nginx对php-fpm的文档有关,但我不知道它应该如何。我使用默认值,使用始终工作的套接字方法。现在它不再工作了在/ etc / nginx / sites-enabled / default下的位置〜.php $ {?

解决方案

没有工作,就像你发现自己一样,nginx只将PHP文件的路径发送到PHP-FPM,而不是文件本身(这将是非常低效的)。解决方案是使用第三个仅数据的VOLUME容器托管文件,然后将其挂载到两个Docker实例上。

  FROM debian 
VOLUME / var / www
CMD ['true']

构建上述Docker文件并创建一个实例(称为例如:storage-www),然后运行nginx和PHP-FPM容器,选项如下:

   -  volume-from storage-www 

您在同一物理服务器上运行两个容器。
但是,如果您将这种仅数据的容器放在联网的文件系统(如GlusterFS)上,您仍然可以使用不同的服务器,这是非常有效的,可以通过大规模网络分发。



希望有所帮助。



更新



截至2015年,在容器之间建立持久链接的最佳方式是使用 docker-compose


I’d like to make a fully dockerized Drupal install. My first step is to get containers running with Nginx and php5-fpm, both Debian based. I’m on CoreOS alpha channel (using Digital Ocean.)

My Dockerfiles are the following:

Nginx:

FROM debian
MAINTAINER fvhemert
RUN apt-get update && apt-get install -y nginx && echo "\ndaemon off;" >> /etc/nginx/nginx.conf
CMD ["nginx"]
EXPOSE 80

This container build and runs nicely. I see the default Nginx page on my server ip.

Php5-fpm:

FROM debian
MAINTAINER fvhemert
RUN apt-get update && apt-get install -y \
            php5-fpm \
            && sed 's/;daemonize = yes/daemonize = no/' -i /etc/php5/fpm/php-fpm.conf
CMD ["php5-fpm"]
EXPOSE 9000

This container also builds with no problems and it keeps running when started.

I start the php5-fpm container first with:

docker run -d --name php5-fpm freek/php5-fpm:1

Ad then I start Nginx,, linked to php5-fpm:

docker run -d -p 80:80 --link php5-fpm:phpserver --name nginx freek/nginx-php:1

The linking seems to work, there is an entry in /etc/hosts with name phpserver. Both dockers run:

core@dockertest ~ $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
fd1a9ae0f1dd        freek/nginx-php:4   "nginx"             38 minutes ago      Up 38 minutes       0.0.0.0:80->80/tcp   nginx
3bd12b3761b9        freek/php5-fpm:2    "php5-fpm"          38 minutes ago      Up 38 minutes       9000/tcp             php5-fpm

I have adjusted some of the config files. For the Nginx container I edited /etc/nginx/sites-enabled/default and changed:

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root /usr/share/nginx/www;
        index index.html index.htm index.php;

(I added the index.php)

And further on:

location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
                fastcgi_pass phpserver:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

In the php5-fpm docker I changed /etc/php5/fpm/php.ini:

cgi.fix_pathinfo=0

php5-fpm runs:

[21-Nov-2014 06:15:29] NOTICE: fpm is running, pid 1
[21-Nov-2014 06:15:29] NOTICE: ready to handle connections

I also changed index.html to index.php, it looks like this (/usr/share/nginx/www/index.php):

<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>

<?php
phpinfo();
?>

</body>
</html>

I have scanned the 9000 port from the Nginx docker, it appears as closed. Not a good sign of course:

root@fd1a9ae0f1dd:/# nmap -p 9000 phpserver

Starting Nmap 6.00 ( http://nmap.org ) at 2014-11-21 06:49 UTC
Nmap scan report for phpserver (172.17.0.94)
Host is up (0.00022s latency).
PORT     STATE  SERVICE
9000/tcp closed cslistener
MAC Address: 02:42:AC:11:00:5E (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 0.13 seconds

The Nginx logs:

root@fd1a9ae0f1dd:/# vim /var/log/nginx/error.log
2014/11/20 14:43:46 [error] 13#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 194.171.252.110, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "128.199.60.95"
2014/11/21 06:15:51 [error] 9#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 145.15.244.119, server: localhost, request: "GET / HTTP/1.0", upstream: "fastcgi://172.17.0.94:9000", host: "128.199.60.95"

Yes, that goes wrong and I keep getting a 502 bad gateway error when browsing to my Nginx instance.

My question is: What exactly goes wrong? My guess is that I’m missing some setting in the php config files.

EDIT FOR MORE DETAILS: This is the result (from inside the php5-fpm container, after apt-get install net-tools):

root@3bd12b3761b9:/# netstat -tapen
Active Internet connections
(servers and established) Proto Recv-Q Send-Q Local Address
Foreign Address State User Inode PID/Program name

From inside the Nginx container:

root@fd1a9ae0f1dd:/# netstat -tapen
Active Internet connections
(servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:*
LISTEN 0 1875387 -

EDIT2: Progression!

In the php5-fpm container, in the file:

/etc/php5/fpm/pool.d/www.conf

I changed the listen argument from some socket name to:

listen = 9000

Now when I go to my webpage I get the error: "No input file specified."

Probably I have trailing / wrong somewhere. I'll look into it more closely!

EDIT3:

So I have rebuild the dockers with the above mentioned alterations and it seems that they are talking. However, my webpage tells me: "file not found." I'm very sure it has to do with the document that nginx sents to php-fpm but I have no idea how it should look like. I used the defaults when using the socket method which always worked. Now it doesn't work anymore. What should be in /etc/nginx/sites-enabled/default under location ~ .php$ { ?

解决方案

The reason it doesn't work is, as you have discovered yourself, that nginx only sends the path of the PHP file to PHP-FPM, not the file itself (which would be quite inefficient). The solution is to use a third, data-only VOLUME container to host the files, and then mount it on both docker instances.

FROM debian
VOLUME /var/www
CMD ['true']

Build the above Dockerfile and create an instance (call it for example: storage-www), then run both the nginx and the PHP-FPM containers with the option:

--volumes-from storage-www

That will work if you run both containers on the same physical server. But you still could use different servers, if you put that data-only container on a networked file-system, such as GlusterFS, which is quite efficient and can be distributed over a large-scale network.

Hope that helps.

Update:

As of 2015, the best way to make persistent links between containers is to use docker-compose.

这篇关于Docker:Nginx和php5-fpm码头工人没有说话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆