9

I build 2 dockers, one docker with apache, one docker with php5, and I use docker-compose to start.

apache2 Dockerfile in directoy apache2:

FROM debian:latest
RUN apt-get update && apt-get install -y apache2
ADD test.php /var/www/html

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

and test.php:

<?php
phpinfo();
?>

php5 Dorckerfile in directory php:

FROM debian:latest
RUN apt-get update && apt-get install -y php5

docker-compose.yml:

apache:
    build: ./apache2
    container_name: apache
    ports:
      - "80:80"
    links:
      - "php5"

php5:
    build: ./php
    container_name: php

then I run:

docker-compose up

apache2 server start successfully. Then I access this server by http://server_ip, then I get index of debian.But when I access http://server_ip/test.php, just occur this:

<?php
phpinfo();
?>

php just doesn't work.And I don't why.

xina1i
  • 748
  • 4
  • 9
  • 21
  • 1
    php and apache now live in two different containers and can only interact with each other via web calls /links / data exchange but no by simply calling each other. You might want to build a combined php5/apache container and make sure you use the php5 module. – Wolfgang Fahl Oct 20 '15 at 09:57
  • @WolfgangFahl what do you mean " interact with each other via web calls /links / data exchange "? I can't understand. – xina1i Oct 20 '15 at 11:53
  • You can use the separate containers for apache and php but you need to use the `php-fpm` that run on some port and then you need to configure the `php-fpm` in your apache config file. – Prashant Barve Dec 26 '17 at 08:49

3 Answers3

6

You can separate Apache and PHP with PHP-FPM. It is however that the DocumentRoot must be mounted on both containers.

Apache must be able to access the files locally (inside its container) as well as the PHP-FPM server.

I am currently working on the same, have a look at my docker-compose.yml here

https://github.com/cytopia/devilbox/blob/master/docker-compose.yml

Both volumes (in PHP and apache) are mounted to /shared/httpd

cytopia
  • 413
  • 5
  • 15
  • +1 good point about "DocumentRoot must be mounted on both containers." I'll add that to the outline answer I've provided regarding connecting php-fpm and apache in separate containers here: https://stackoverflow.com/questions/43071337/different-php-fpm-containers-with-apache/50883912#50883912 – therobyouknow Jun 16 '18 at 00:00
1

I would say its not possible to run seperate containers for php as apache module. I guess this is what Wolfgang meant.

If you want to seperate apache and php in two different containers you need to run php as fpm.
Have a look here for inspiration: How to correctly link php-fpm and Nginx Docker containers together?

If you need to run apache and php as apache_mod use a combined container like this: https://github.com/docker-library/php/blob/fec7f537f049aafd2102202519c3ca9cb9576707/5.5/apache/Dockerfile from: https://hub.docker.com/_/php/

ivoba
  • 5,780
  • 5
  • 48
  • 55
  • I've outlined a possible answer here for the apache equivalent to the nginx setup: https://stackoverflow.com/questions/43071337/different-php-fpm-containers-with-apache/50883912#50883912 – therobyouknow Jun 16 '18 at 00:02
0

If you don't specifically need to separate Apache from PHP, then you might be good to go with an official php:5.6-apache image which comes with Apache out of the box.

For example, your docker-compose.yml might look something like this:

version: '3'
services:
  web:
    image: php:5.6-apache
    ports:
      - "8080:80" # Map container port 80 to host machine port 8080
    volumes:
      - ".:/var/www/html" # Mount current folder as volume to container at /var/www/html

Or, for a more real-life example, if you also need at least one of the following:

  • A custom web root (for Laravel, Symfony, etc)
  • Other Apache modules installed
  • Other PHP extensions installed

You might do something more like this:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:80" # Map container port 80 to host machine port 8080
    environment:
      APACHE_DOCUMENT_ROOT: "/var/www/yourapp.com/public"
    volumes:
      - ".:/var/www/yourapp.com" # Mount current folder as volume to container at /var/www/yourapp.com

And then your Dockerfile (which we reference from the docker-compose.yml above):

FROM php:5.6-apache

# Declare an environment variable with a default value for changing Apache's document root
# We will override this in docker-compose.yml
ENV APACHE_DOCUMENT_ROOT /var/www/html

# Configure web root
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Install additional Apache modules
# This example: mod_rewrite & mod_headers
RUN a2enmod rewrite headers

# Install additional PHP extensions
# This example: memcached & mysqli
# For other extensions see official docs:
# https://hub.docker.com/_/php (section: How to install more PHP extensions)
RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \
    && pecl install memcached-2.2.0 \
    && docker-php-ext-enable memcached \
    && docker-php-ext-install -j$(nproc) mysqli
Jonas Masalskis
  • 1,206
  • 1
  • 15
  • 14