无法从apache 2 docker容器中连接到php7 fpm docker容器

时间:2017-09-19 00:15:31

标签: php apache docker docker-compose dockerfile

类似的问题已被多次询问,我已经完成了大部分问题,但我仍然无法解决问题。

我在不同的docker容器中运行Apache2和PHP 7 FPM,但它们无法相互连接。 连接失败并出现以下错误。

[Tue Sep 19 00:05:51.743322 2017] [proxy:debug] [pid 336:tid 140441754191616] proxy_util.c(2422): [client 172.18.0.1:36332] AH00947: connected /var/www/html/info.php to 127.0.0.1:9000
[Tue Sep 19 00:05:51.743387 2017] [proxy:error] [pid 336:tid 140441754191616] (111)Connection refused: AH00957: FCGI: attempt to connect to 127.0.0.1:9000 (*) failed
[Tue Sep 19 00:05:51.743405 2017] [proxy_fcgi:error] [pid 336:tid 140441754191616] [client 172.18.0.1:36332] AH01079: failed to make connection to backend: 127.0.0.1
[Tue Sep 19 00:05:51.743410 2017] [proxy:debug] [pid 336:tid 140441754191616] proxy_util.c(2175): AH00943: FCGI: has released connection for (*)
[Tue Sep 19 00:05:56.809604 2017] [authz_core:debug] [pid 336:tid 140441672480512] mod_authz_core.c(809): [client 172.18.0.1:36442] AH01626: authorization result of Require all granted: granted
[Tue Sep 19 00:05:56.809646 2017] [authz_core:debug] [pid 336:tid 140441672480512] mod_authz_core.c(809): [client 172.18.0.1:36442] AH01626: authorization result of <RequireAny>: granted
[Tue Sep 19 00:05:56.809676 2017] [proxy:debug] [pid 336:tid 140441672480512] mod_proxy.c(1160): [client 172.18.0.1:36442] AH01143: Running scheme fcgi handler (attempt 0)
[Tue Sep 19 00:05:56.809687 2017] [proxy_fcgi:debug] [pid 336:tid 140441672480512] mod_proxy_fcgi.c(879): [client 172.18.0.1:36442] AH01076: url: fcgi://127.0.0.1:9000/var/www/html/info.php proxyname: (null) proxyport: 0
[Tue Sep 19 00:05:56.809694 2017] [proxy_fcgi:debug] [pid 336:tid 140441672480512] mod_proxy_fcgi.c(886): [client 172.18.0.1:36442] AH01078: serving URL fcgi://127.0.0.1:9000/var/www/html/info.php
[Tue Sep 19 00:05:56.809703 2017] [proxy:debug] [pid 336:tid 140441672480512] proxy_util.c(2160): AH00942: FCGI: has acquired connection for (*)
[Tue Sep 19 00:05:56.809712 2017] [proxy:debug] [pid 336:tid 140441672480512] proxy_util.c(2213): [client 172.18.0.1:36442] AH00944: connecting fcgi://127.0.0.1:9000/var/www/html/info.php to 127.0.0.1:9000
[Tue Sep 19 00:05:56.809840 2017] [proxy:debug] [pid 336:tid 140441672480512] proxy_util.c(2422): [client 172.18.0.1:36442] AH00947: connected /var/www/html/info.php to 127.0.0.1:9000
[Tue Sep 19 00:05:56.809927 2017] [proxy:error] [pid 336:tid 140441672480512] (111)Connection refused: AH00957: FCGI: attempt to connect to 127.0.0.1:9000 (*) failed
[Tue Sep 19 00:05:56.809946 2017] [proxy_fcgi:error] [pid 336:tid 140441672480512] [client 172.18.0.1:36442] AH01079: failed to make connection to backend: 127.0.0.1
[Tue Sep 19 00:05:56.809953 2017] [proxy:debug] [pid 336:tid 140441672480512] proxy_util.c(2175): AH00943: FCGI: has released connection for (*)

以下是我的docker-compose文件

version: '2'
services:
    php:
        build: ./etc/php/
        volumes:
          - ./etc/php/config/php.ini:/etc/php/7.1/fpm/php.ini
          - ./etc/php/config/www.conf:/etc/php/7.1/fpm/pool.d/www.conf
          - ./etc/php/config/php-fpm.conf:/etc/php/7.1/fpm/php-fpm.conf
          - ./etc/php/logs/php-fpm.log:/var/log/php-fpm.log 
          - ./etc/php/logs/fpm-php.www.log:/var/log/fpm-php.www.log
          - ./source/:/var/www/html/
        expose:
          - 9000
        ports:
          - "9000:9000"
        links:
          - mysql

    apache2:
        build: ./etc/apache/
        volumes:
          - ./source/:/var/www/html/
          - ./etc/apache/config/sites-enabled/:/etc/apache2/sites-enabled/
          - ./etc/apache/config/conf-enabled/:/etc/apache2/conf-enabled/
          - ./etc/apache/logs/error.log:/var/log/apache2/error.log
          - ./etc/apache/logs/access.log:/var/log/apache2/access.log
          - ./etc/apache/logs/sm-error.log:/var/log/apache2/sm-error.log
          - ./etc/apache/logs/sm-access.log:/var/log/apache2/sm-access.log
        expose:
          - 80
          - 443
        ports:
          - "8080:80"
          - "443:443"
        links:
          - php

    mysql:
        image: mysql:latest
        volumes:
          - ./etc/mysql/data/:/var/lib/mysql/
          - ./etc/mysql/config/:/etc/mysql/conf.d/
        ports:
          - "3307:3306"
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: sp
            MYSQL_USER: spadmin
            MYSQL_PASSWORD: root

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports:
            - "8081:80"
        links:
            - mysql
        environment:
            PMA_HOST: mysql

下面是apache2的docker文件。

#Get the base image
FROM ubuntu:16.04

#Install Apache
RUN apt-get update && \
    apt-get dist-upgrade -y && \
    apt-get install -y \
      apache2 \
      apache2-utils

# Enable modules
RUN a2enmod rewrite
RUN a2enmod proxy
RUN a2enmod proxy_fcgi

# Add user to www-data
RUN groupadd 1000
RUN usermod -a -G 1000 www-data
RUN usermod -a -G staff www-data

RUN service apache2 restart

# Run config
ENTRYPOINT [ "/usr/sbin/apache2ctl" ]
CMD [ "-D", "FOREGROUND" ]

以下是我的apache2虚拟主机文件。

<VirtualHost *:80>
    ServerName localhost

    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>

    DocumentRoot /var/www/html
    <Directory /var/www/html>
        Options FollowSymlinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/apache2/sm-error.log
    CustomLog /var/log/apache2/sm-access.log combined
</VirtualHost>

下面是我的PHP-FPM泊坞窗文件。

FROM ubuntu:16.04

# Install PHP
RUN apt-get clean && \
    apt-get -y update && \
    apt-get install -y locales \
    curl \ 
    software-properties-common \ 
    git && \
    locale-gen en_AU.UTF-8   
RUN LC_ALL=en_AU.UTF-8 add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get install -y --force-yes php7.1-bcmath \ 
                php7.1-bz2 \ 
                php7.1-cli \
                php7.1-common \
                php7.1-curl \
                php7.1-cgi \ 
                php7.1-dev \
                php7.1-fpm \
                php7.1-gd \
                php-ldap \
                php7.1-gmp \
                php7.1-intl \
                php7.1-json \
                php7.1-mbstring \
                php7.1-mcrypt \
                php7.1-mysql \
                php-sqlite3 \
                php7.1-opcache \
                php7.1-phpdbg \
                hp7.1-pspell \
                php7.1-readline \
                php7.1-recode \ 
                php7.1-soap \ 
                php7.1-tidy \
                php7.1-xml \
                php7.1-xmlrpc \
                php7.1-xsl \
                php7.1-zip \
                php-xdebug

# Install Composer
RUN curl https://getcomposer.org/installer > composer-setup.php && php composer-setup.php && mv composer.phar /usr/local/bin/composer && rm composer-setup.php

# Clean files
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Run PHP-FPM
CMD ["php-fpm7.1"]

以下是docker ps命令的输出。

a85830192ad4        spendingmanager_apache2   "/usr/sbin/apache2..."   15 minutes ago      Up 15 minutes       0.0.0.0:443->443/tcp, 0.0.0.0:8080->80/tcp   spendingmanager_apache2_1
4574c977d0d4        phpmyadmin/phpmyadmin     "/run.sh phpmyadmin"     15 minutes ago      Up 15 minutes       0.0.0.0:8081->80/tcp                         spendingmanager_phpmyadmin_1
82735fa580e8        spendingmanager_php       "php-fpm7.1"             15 minutes ago      Up 15 minutes       0.0.0.0:9000->9000/tcp                       spendingmanager_php_1
32422e934247        mysql:latest              "docker-entrypoint..."   15 minutes ago      Up 15 minutes       0.0.0.0:3307->3306/tcp                       spendingmanager_mysql_1

注意:PHP-FPM正在侦听端口9000上的连接及其正常工作。当我尝试从我的主机操作系统(例如http://localhost/而不是http://localhost:8080访问它时,我能够连接并获得所需的输出。)

我还尝试设置www/html的权限,并尝试将www-data设为所有者。

有人可以帮我解决这个问题吗?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

在您的示例中,localhost127.0.0.1是容器的本地(而不是主机)。因此,当Apache尝试在127.0.0.1:9000连接到FPM时,它实际上是在尝试连接到自身(仅限Apache容器)。

您要做的是让Apache连接到FPM容器。使用Docker网络执行此操作的最简单方法是使用Docker Compose中的服务名称。在您的情况下,服务名称为php,并且可以从Apache容器的DNS名称php获得。

因此,您的Apache配置应为:

SetHandler "proxy:fcgi://php:9000"

答案 1 :(得分:0)

我也遇到了同样的错误,只是花了整整一天的时间才弄清楚原因。

在我的情况下,问题是在我的VPS中启用了IP v6,而没有将真正的IP6地址分配给我的接口。

我刚刚禁用了IPV6,一切又恢复了正常。 (如果未使用IPv6 / IPv4,则将其禁用)

第1步-在外壳程序中使用以下命令编辑sysctl.conf文件

vi /etc/sysctl.conf

第2步-添加以下两行以禁用IPv6

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

第3步-保存并关闭文件(Esc->:wq)

第4步-为立即生效,在shell中运行以下代码

sysctl -p