首次Docker化项目遇AH02454错误:Apache无法连接PHP-FPM套接字
问题:Apache无法连接PHP-FPM的Unix Socket错误
访问localhost:8080时,Apache日志出现以下错误:
AH01079: failed to make connection to backend: httpd-UDS (2)No such file or directory: AH02454: FCGI: attempt to connect to Unix domain socket /run/php/php7.0-fpm.sock (*) failed
相关配置文件
docker-compose.yml
version: "3" services: apache: build: context: . dockerfile: apache2/Dockerfile container_name: markitplace-php ports: - '8080:80' volumes: - ./www/html:/var/www/html - ./apache2/configs:/etc/apache2/sites-available networks: - internal depends_on: - php - db db: image: mysql:5.7.22 container_name: db restart: unless-stopped tty: true ports: - "3307:3306" environment: MYSQL_DATABASE: markitplace MYSQL_ROOT_PASSWORD: admin0101 volumes: - db:/var/lib/mysql networks: - internal php: build: context: . dockerfile: php/Dockerfile networks: - internal volumes: - ./www/html/:/var/www/html/ - ./logs/php.log:/var/log/fpm-php.www.log depends_on: - db networks: internal: driver: bridge volumes: db:
apache2/Dockerfile
FROM ubuntu:20.04 ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && \ apt-get install -y apache2 && \ apt-get install -y wget && \ a2enmod rewrite && \ a2dissite 000-default.conf RUN wget https://mirrors.edge.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb RUN dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb # Install FastCGI module RUN apt-get update && apt-get install -y libapache2-mod-fastcgi # Enable FastCGI module RUN a2enmod fastcgi RUN a2enmod actions COPY apache2/configs/php7-fcgi.conf /etc/apache2/conf-available/php7-fcgi.conf RUN a2enconf php7-fcgi RUN a2enmod proxy_fcgi setenvif COPY apache2/configs/docker.markitplace.local.conf /etc/apache2/sites-available/docker.markitplace.local.conf RUN a2enmod headers RUN a2ensite docker.markitplace.local.conf WORKDIR /var/www/html #COPY html/ /var/www/html/ EXPOSE 80 CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
apache2/configs/docker.markitplace.local.conf
<VirtualHost *:80> ServerName docker.markitplace.local ServerAlias www.docker.markitplace.local DocumentRoot /var/www/html/public ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://php:9000" </FilesMatch> <Directory "/var/www/html/public"> AllowOverride All </Directory> </VirtualHost> ServerName docker.markitplace.local
apache2/configs/php7-fcgi.conf
<IfModule mod_fastcgi.c> AddHandler php7-fcgi .php Action php7-fcgi /php7-fcgi Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi FastCGIExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php-fpm.sock -pass-header Authorization <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule>
php/Dockerfile
FROM php:7.0.33-fpm-alpine RUN apk add --no-cache bash mysql-client msmtp perl wget procps shadow libzip libpng libjpeg-turbo libwebp freetype icu RUN apk add --no-cache --virtual build-essentials \ icu-dev icu-libs zlib-dev g++ make automake autoconf libzip-dev \ libpng-dev libwebp-dev libjpeg-turbo-dev freetype-dev && \ docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp && \ docker-php-ext-install gd && \ docker-php-ext-install mysqli && \ docker-php-ext-install pdo_mysql && \ docker-php-ext-install intl && \ docker-php-ext-install opcache && \ docker-php-ext-install exif && \ docker-php-ext-install zip && \ apk del build-essentials && rm -rf /usr/src/php* RUN apk --no-cache add git curl zlib-dev libpng-dev RUN echo 'listen = /var/run/php-fpm.sock\n' > /usr/local/etc/php-fpm.d/zz-socket.conf # Composer RUN curl -sS https://getcomposer.org/installer | php RUN mv composer.phar /usr/local/bin/composer && \ chmod +x /usr/local/bin/composer && \ composer self-update RUN command -v composer RUN php -v RUN composer --version #RUN node -v #RUN npm -v # # Configure PHP #COPY config/php.ini /usr/local/etc/php/ # Set working directory WORKDIR /var/www/html # Add application #COPY www/html /var/www/html # Give permissions # RUN composer install COPY ./www/html/ /var/www/html/ RUN chown -R www-data:www-data /var/www/html/ && chmod -R 777 /var/www/html/ && chmod -R 777 storage && chmod -R 777 bootstrap/cache storage RUN php artisan config:clear #RUN composer dump-autoload RUN composer install RUN composer dump-autoload # Expose port 9000 and start PHP-FPM server EXPOSE 9000 CMD ["php-fpm"]
修复步骤
1. 修正Apache与PHP-FPM的连接方式
Docker容器间无法直接访问彼此的Unix Socket,改用TCP端口连接更可靠:
修改apache2/configs/docker.markitplace.local.conf中的FilesMatch块:
<FilesMatch \.php$> SetHandler "proxy:fcgi://php:9000" </FilesMatch>
这里php是docker-compose中定义的服务名,Docker内部DNS会自动解析到PHP-FPM容器的IP,9000是PHP-FPM默认监听的TCP端口。
2. 清理冗余的FastCGI配置
Apache同时配置了mod_proxy_fcgi和mod_fastcgi,两者冲突,且mod_proxy_fcgi更适配Docker场景:
- 删除
apache2/configs/php7-fcgi.conf文件 - 修改
apache2/Dockerfile,移除以下内容:RUN wget https://mirrors.edge.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb RUN dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb RUN apt-get update && apt-get install -y libapache2-mod-fastcgi RUN a2enmod fastcgi RUN a2enmod actions COPY apache2/configs/php7-fcgi.conf /etc/apache2/conf-available/php7-fcgi.conf RUN a2enconf php7-fcgi - 保留
RUN a2enmod proxy_fcgi setenvif即可
3. 确保PHP-FPM监听TCP端口
修改php/Dockerfile中的监听配置,替换Unix Socket为TCP端口:
RUN echo 'listen = 9000\n' > /usr/local/etc/php-fpm.d/zz-socket.conf
4. 重启容器
修改完成后,重新构建并启动服务:
docker-compose down docker-compose build docker-compose up -d
内容的提问来源于stack exchange,提问作者Imran Mushtaq
相关产品推荐
相关产品推荐

