dongyan1491 2017-09-18 16:15
浏览 190
已采纳

无法从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.

展开全部

  • 写回答

1条回答 默认 最新

  • dongshenjie3055 2017-09-18 16:23
    关注

    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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部