Alpine variants of PHP and Apache/httpd in Docker

Christian Ivicevic picture Christian Ivicevic · Dec 23, 2016 · Viewed 14.5k times · Source

I am experimenting with Docker and want to move from a local MAMP stack to Docker. Of course I stumbled upon the official php:7.0 image but I want to use Apache as well so it seems as if php:7.0-apache is the way to go. However I saw that there is an image called php:7.0-alpine which is much slimmer while there are two versions for Apache as well namely httpd:2.4 and httpd:2.4-alpine.

Is there any suggested combination to use both Apache and PHP (either separated or combined) while still having small image sizes? Furthermore I would like to know where I can review the available modules in the images since I want to use MariaDB and mod_rewrite as well which could possibly have more dependencies which have been omitted to keep the size small.


Information on implementing the desired infrastructure with nginx

I came across this very detailed and awesome tutorial on how to split up nginx and PHP as well as MySQL into different containers but attach PHP to nginx using FCGI. This implies that I can use all the different alpine-based images of the tools and link them using FCGI. Unfortunately I have never heard of or worked with FCGI but I guess some more research will yield information on how to implement this infrastructure using Apache.

Answer

Christian Ivicevic picture Christian Ivicevic · Dec 24, 2016

Running official Apache and PHP with FCGI

In addition to helmberts proposed solution which worked for me I was fiddling around with the official httpd:2.4-alpine image. I want to add some more information and caveats I stumbled upon while working on this.

  • The Apache configuration was slightly more difficult since it is a lighter version with no usual vhost data structure, no a2en* and no a2dis* scripts.
  • There is no generic folder structure in /etc/apache2. Everything is in /usr/local/apache2 and you have your global httpd.conf in /usr/local/apache2/conf/. In this file you have to uncomment the module lines manually to load them as well as further configuration files like httpd-vhosts.conf which is stored in /usr/local/apache2/conf/extra/.
  • I used the httpd-vhosts.conf to setup the basic options for my testing:

<VirtualHost *:80>
    DocumentRoot /usr/local/apache2/htdocs

    <Directory /usr/local/apache2/htdocs>
        Options -Indexes +FollowSymLinks -MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /usr/local/apache2/main-error.log
    LogLevel warn
    CustomLog /usr/local/apache2/main-access.log combined
</VirtualHost>

  • For the FCGI server I had to uncomment the following two lines:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

  • Everytime to change something in the configuration you have to ./bin/apachectl restart as usual assuming you are in /usr/local/apache2.
  • Since I use nano I had to install it manually using apk --no-cache add nano. This can be done in an interactive session or globally using a Dockerfile to fork the base image.
  • I had an error when opening nano - in my case I had to export TERM=xterm in an interactive shell or ENV TERM xterm in the Dockerfile.
  • It turned out that php-fpm has to have access to the very same files - this is something I didn't actually notice in the proposed solution at first. This means as well that I had the source files mounted at two different paths (/usr/local/apache2/htdocs in the httpd image and /var/www/html in the php-fpm image) and had to forward calls properly. I used the following line in the httpd.conf.

ProxyPassMatch "^/(.*\.php)$" "fcgi://fpm:9000/var/www/html/$1"

  • The fpm name in the FCGI link is the name of my container that is linked to httpd using --link fpm and automatically added to the /etc/hosts.
  • So far I managed to achieve want I want by interactively doing all these changes. I am going to add all the changes to my Dockerfile using either COPY commands or applying basic sed calls.
  • I know there is the possibility to use unix sockets via the command SetHandler "proxy:unix:/var/run/php7-fpm.sock|fcgi://fpm/" but I don't know if that is useful or not.