Alpine variants of PHP and Apache/httpd in Docker Alpine variants of PHP and Apache/httpd in Docker apache apache

Alpine variants of PHP and Apache/httpd in Docker


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


Running Apache/NGINX and PHP with FCGI

If you want to run Apache and PHP in separate containers, you'll need to use a PHP-FPM container (like for example, using the php:7-fpm or php:7-fpm-alpine image) and then use FCGI to connect the two. By default, the official PHP-FPM images expose the TCP port 9000 for this, which should be sufficient for most cases.

For PHP-FPM, the official PHP image should do fine (regarding size, the 7.0.14-fpm-alpine tag is only 31M in size). Regarding Apache, I've come to like the webdevops/apache image (I'm not affiliated in any way). It also comes with an Alpine-based version that is only 38M in size and works well together with PHP-FPM.

Here's how you start separate PHP-FPM and Apache containers, linked together using FCGI:

$ docker run -d \    -v /path/to/data:/var/www/html \    --name fpm \    php:fpm-7.0.14-alpine$ docker run -d \    -v /path/to/data:/var/www/html \    --name apache \    --link fpm \    -e WEB_PHP_SOCKET=fpm:9000 \    -e WEB_DOCUMENT_ROOT=/var/www/html \    webdevops/apache:alpine-3

For using Nginx instead, simply substitute the webdevops/apache image with webdevops/nginx.

Adding custom extensions

Since you've also asked about adding additional PHP extensions to your image: this is covered in the official PHP image's documentation. You can add custom PHP extensions to the PHP base image by running docker-php-ext-install in a custom Dockerfile:

FROM php:7.0.14-fpm-alpineRUN docker-php-ext-install pdo_mysql

This allows you to build your custom image based on one of the PHP-FPM base images, adding all extensions that you require in the Dockerfile.