我建造了两个码头工人,一个带有apache的码头工人,一个带有php5的码头工人,我使用docker-compose来启动。
bootoy apache2中的apache2 Dockerfile:
FROM debian:latest
RUN apt-get update && apt-get install -y apache2
ADD test.php /var/www/html
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
和test.php:
<?php
phpinfo();
?>
目录php中的php5 Dorckerfile:
FROM debian:latest
RUN apt-get update && apt-get install -y php5
搬运工-compose.yml:
apache:
build: ./apache2
container_name: apache
ports:
- "80:80"
links:
- "php5"
php5:
build: ./php
container_name: php
然后我跑:
docker-compose up
apache2服务器启动成功。然后我通过http://server_ip访问这个服务器,然后我得到了debian的索引。但是当我访问http://server_ip/test.php时,就发生这个:
<?php
phpinfo();
?>
php只是没有工作。我不知道为什么。
答案 0 :(得分:6)
您可以使用PHP-FPM分离Apache和PHP。但是,DocumentRoot必须安装在两个容器上。
Apache必须能够在本地(在其容器内)以及PHP-FPM服务器上访问文件。
我目前正在进行同样的工作,请看看我的docker-compose.yml
https://github.com/cytopia/devilbox/blob/master/docker-compose.yml
两个卷(在PHP和apache中)都已挂载到/shared/httpd
答案 1 :(得分:1)
我会说它不可能作为apache模块运行php的单独容器。我想这就是沃尔夫冈的意思。
如果你想在两个不同的容器中分离apache和php,你需要以fpm运行php 看看灵感:How to correctly link php-fpm and Nginx Docker containers together?
如果你需要运行apache和php作为apache_mod使用这样的组合容器:https://github.com/docker-library/php/blob/fec7f537f049aafd2102202519c3ca9cb9576707/5.5/apache/Dockerfile 来自:https://hub.docker.com/_/php/
答案 2 :(得分:0)
如果你没有特别需要将Apache与PHP分开,那么你可能会选择Apache开箱即用的官方php:5.6-apache image。
例如,您的docker-compose.yml
可能如下所示:
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
或者,对于更现实的例子,如果您还需要至少以下其中一项:
你可能会做更多这样的事情:
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
然后是您的Dockerfile
(我们从上面的docker-compose.yml
引用):
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