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

Similar question have been asked many times and I have gone through most of them and still I am not able to solve the problem.

I am running Apache2 and PHP 7 FPM in different docker containers and they are not able to connect to each other. The connection fails with following errors.

[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 (*)

Below is my docker-compose file

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

Below is my docker file for apache2.

#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" ]

Below is my virtual host file for 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>

Below is my PHP-FPM docker file.

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

Below is the output of docker ps command.

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

Note: PHP-FPM is listening for connections on port 9000 and its working properly. When I try to access it from my host OS (for instance http://localhost/ instead of http://localhost:8080 I am able to connect and get the desired output).

I have also tried setting up permission on the www/html and try making www-data the owner.

Can someone please help me get this working?

Thanks in advance for your help.

douti0687
douti0687 SetHandler“proxy:fcgi://127.0.0.1:9000”应该是SetHandler“proxy:fcgi://php:9000”。顺便说一句,你不需要将fpm端口绑定到主机,因为php和apache容器是链接的。
大约 3 年之前 回复

1个回答



在您的示例中 localhost </ code>或 127.0.0.1 </ code>是容器的本地( 不是东道主)。 因此,当Apache尝试通过 127.0.0.1:9000 </ code>连接到FPM时,它实际上是在尝试连接到自身(仅限Apache容器)。</ p>

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

因此, 你的Apache配置应该是:</ p>

  SetHandler“proxy:fcgi:// php:9000”
</ code> </ pre>
</ div>

展开原文

原文

In your examples localhost or 127.0.0.1 is local to the containers (not the host). So, when Apache tries to connect to FPM at 127.0.0.1:9000, it is actually trying to connect to itself (the Apache container only).

What you want to do is have Apache connect to the FPM container. The easiest way to do this with Docker networking is to use the service name from Docker Compose. In your case, the service name is php and will be available at the DNS name php from the Apache container.

Thus, your Apache configuration should be:

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

dongqing4070
dongqing4070 非常感谢@Andy-shin解决了这个问题。 我好几天都在研究这个问题。 非常感谢您的帮助。
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐