You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下Docker分离部署Apache与PHP-FPM连接故障求助

Apache + PHP-FPM Docker容器通信失败排查与修复

问题背景

通过docker-compose将apache和php-fpm:alpine配置为独立服务,容器间通信失败,访问时提示“请求的URL未找到”。已排查路径配置两天,手动挂载www.conf也无效,请求技术支持。

相关配置文件

docker-compose.yml

version: "3"
services:
  apache:
    image: httpd:2.4
    ports: 
      - "8081:80"
    networks:
      internal:
       aliases:
        - apache.virtualhosting
    container_name: "apache_virtualhosting"
    volumes:
      - ./conf/httpd.conf:/usr/local/apache2/httpd.conf
      - ./conf/sites:/usr/local/apache2/sites
      - ./data/sites/one.website:/var/www/sites/one.website
      - ./data/sites/two.website:/var/www/sites/two.website
      - ./data/logs/apache:/usr/local/apache2/logs
    depends_on:
      - php 
  php:
    image: php:fpm-alpine
    networks:
       internal:
        aliases:
         - php.virtualhosting
    container_name: "php_virtualhosting"
    environment: 
      - PHP_FPM_LISTEN="0.0.0.0:9000"
      - PHP_FPM_ENVIRONMENT="docker"
    command: ["php-fpm"]
    volumes:
      - ./conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
      - ./data/sites/one.website:/var/www/sites/one.website
      - ./data/sites/two.website:/var/www/sites/two.website
      - ./data/logs/php.log:/var/log/fpm-php.www.log

networks:
  internal:
    driver: bridge

www.conf

[www]
user = www-data
group = www-data

listen = 0.0.0.0:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

;access.log = /var/log/php-fpm/access.log

one.website.conf

<VirtualHost *:80>
ServerName one.website
DocumentRoot/var/www/sites/one.website

<Directory/var/www/sites/one.website>
OptionsIndexesFollowSymLinks
AllowOverride All
Require all granted
</Directory>

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

ErrorLog/usr/local/apache2/one_error.log
CustomLog/usr/local/apache2/one_access.logcombined
</VirtualHost>

httpd.conf

ServerRoot "/usr/local/apache2"
Listen 80

LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so

DocumentRoot "/var/www/sites"

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<Directory "/var/www/sites">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<Directory /var/www/sites/one.website>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted   
</Directory>

<Directory /var/www/sites/two.website>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>


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

Include conf/sites/*.conf

ErrorLog /usr/local/apache2/error.log
LogLevel warn
CustomLog /usr/local/apache2/access.log combined

本地hosts文件

127.0.0.1   localhost
127.0.1.1   OptiPlex-7020
127.0.0.1   one.website
127.0.0.1   two.website

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

目录结构

apache_php_fpm/
├── conf/
│   ├── httpd.conf
|   ├── www.conf 
│   └── sites/
│       ├── one.website.conf
│       └── two.website.conf
├── data/
│   ├── logs/
│   │   └── apache/
│   └── sites/
│       ├── one.website/
│       │   ├── index.html
│       │   └── index.php
│       └── two.website/
│           ├── index.html
│           └── index.php
└── docker-compose.yml

测试命令及结果

# curl http://php.virtualhosting:9000
curl: (56) Recv failure: Connection reset by peer

核心问题修复方案

1. 修复Apache虚拟主机配置语法错误

one.website.conf存在多处空格缺失,导致Apache无法解析配置,修正后内容如下:

<VirtualHost *:80>
ServerName one.website
DocumentRoot /var/www/sites/one.website

<Directory /var/www/sites/one.website>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

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

ErrorLog /usr/local/apache2/one_error.log
CustomLog /usr/local/apache2/one_access.log combined
</VirtualHost>

2. 修正PHP-FPM环境变量格式

docker-compose中环境变量的引号会被当作值的一部分,导致PHP-FPM监听配置失效,修改如下:

environment: 
  - PHP_FPM_LISTEN=0.0.0.0:9000
  - PHP_FPM_ENVIRONMENT=docker

3. 修复PHP-FPM日志权限问题

单个文件挂载会导致权限异常,改为挂载目录,并调整www.conf日志路径:

docker-compose.yml修改:

volumes:
  - ./conf/www.conf:/usr/local/etc/php-fpm.d/www.conf
  - ./data/sites/one.website:/var/www/sites/one.website
  - ./data/sites/two.website:/var/www/sites/two.website
  - ./data/logs/php:/var/log/php-fpm

www.conf修改:

[www]
user = www-data
group = www-data

listen = 0.0.0.0:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

access.log = /var/log/php-fpm/access.log
error_log = /var/log/php-fpm/error.log

4. 正确验证容器连通性

PHP-FPM使用FCGI协议,无法用curl直接访问,正确验证方式:

  1. 进入Apache容器:
docker exec -it apache_virtualhosting /bin/bash
  1. 安装FCGI测试工具:
apt update && apt install libfcgi-bin -y
  1. 测试FCGI连接:
cgi-fcgi -bind -connect php:9000

返回Status: 400 Bad Request说明连接正常,问题出在配置或文件路径。


验证步骤

  1. 重启容器:docker-compose down && docker-compose up -d
  2. 查看Apache日志确认无配置错误:docker logs apache_virtualhosting
  3. 访问http://one.website:8081/index.php验证PHP页面加载

内容的提问来源于stack exchange,提问作者Vishy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 20:25:53