如何通过unix socket设置Apache2和PHP-FPM?

我尝试通过unix套接字设置Apache2和PHP-FPM,但结果是 (111)连接被拒绝:AH02454:FCGI:尝试连接到Unix域套接字/run/php/php7.2-fpm.sock(*)失败 docker-compose.yml

version: "2"
services:
    php:
        build: "php:7.2-rc-alpine"
        container_name: "php"
        volumes:
            - "./code:/usr/local/apache2/htdocs"
            - "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
    apache2:
        build: "httpd:2.4-alpine"
        container_name: "apache2"
        volumes:
            - "./code:/usr/local/apache2/htdocs"
            - "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock"
        ports:
            - 80:80
        links:
            - php

www.conf

listen = /run/php/php7.2-fpm.sock

httpd-vhosts.conf

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>

但是通过TCP连接时可以使用。

www.conf

listen = 127.0.0.1:9000

httpd-vhosts.conf

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

Okie, so have the repo helped to fix the issue.

Issue #1 - www.conf being copied in apache container

You had below statement in your apache container Dockerfile

COPY ./www.conf /usr/local/etc/php-fpm.d/www.conf

This is actually intended for the php container which will be running php-fpm and not the apache container

Issue #2 - Socket was never being created

Your volume bind - "./php7.2-fpm.sock:/run/php/php7.2-fpm.sock" was creating the socket and they were not being created by php-fpm as such. So you created a blank file and trying to connect to it won't do anything

Issue #3 - No config in php to create socket

The docker container by default create listen to 0.0.0.0:9000 inside the fpm container. You needed to override the zz-docker.conf file inside the container to fix the issue.

zz-docker.conf

[global]
daemonize = no

[www]
listen = /run/php/php7.2-fpm.sock
listen.mode = 0666
Updated docker file
FROM php:7.2-rc-fpm-alpine

LABEL maintainer="Eakkapat Pattarathamrong (overbid@gmail.com)"
RUN docker-php-ext-install \
        sockets

RUN set -x \
        && deluser www-data \
        && addgroup -g 500 -S www-data \
        && adduser -u 500 -D -S -G www-data www-data

COPY php-fpm.d /usr/local/etc/php-fpm.d/

Issue #4 - Sockets being shared as volumes to host

You should be sharing sockets using a named volume, so the socket should not be on host at all.

Updated docker-compose.yml
version: "2"
services:
    php:
        build: "./php"
        container_name: "php"
        volumes:
            - "./code:/usr/local/apache2/htdocs"
            - "phpsocket:/run/php"
    apache2:
        build: "./apache2"
        container_name: "apache2"
        volumes:
            - "./code:/usr/local/apache2/htdocs"
            - "phpsocket:/run/php"
        ports:
            - 7080:80
        links:
            - php
volumes:
  phpsocket:

After fixing all the issues I was able to get the php page working

PHP-FPM