Search code examples
phpdockerapachedocker-composecontainers

apache and php in seperate containers on ubuntu


I have pulled apache and php-fpm:alpine images into docker-compose.yml as services and trying to connect between these two containers but failed to do so. It says requested URL was not found on the server. I could clearly understand that somewhere the path is misconfigured but I have been struggling to identify it for 2 days. I even created www.conf and configured in volumes so that the php container will be connected but of no use. Any help is greatly appreciated. docker-compose.yml file

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 file:

[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

This is a host file

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

folder structure:

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

There are no error logs except curl commandn

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

Solution

  • For starters you have the wrong path to the apache config directory in the docker-compose.yml file:

    ...
        volumes:
          - ./conf/httpd.conf:/usr/local/apache2/conf/httpd.conf
          - ./conf/sites:/usr/local/apache2/conf/sites
    ...
    

    Then in the configuration file for the apache you need to load all the necessary modules.

    httpd.conf:

    ...
    LoadModule authz_core_module modules/mod_authz_core.so
    LoadModule log_config_module modules/mod_log_config.so
    ...
    

    and lastly the configuration file for the site should be corrected.

    one.website.conf:

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

    Now you should be able to run curl on http://one.website:8081