Custom Dockerfile for PHP 5.6, Apache, & WP-CLI

Custom Dockerfile for PHP 5.6, Apache, & WP-CLI

Docker container architecture diagram showing Ubuntu 16 running Apache and compiled PHP 5.6 for legacy WordPress management

The Challenge of Running Legacy WordPress 3.4

I wanted to get my old wordpress 3.4 websites running again, so I had to build a couple docker images, and a docker compose file. This starts with Ubuntu 16, as I thought I would be able to get PHP5 on there. But in reality this container comes with PHP7 hooked up in the apt sources. So I ended up compiling PHP 5.6.40 in the container.

Dealing with legacy applications often requires highly specialized environments. Modern server infrastructure has advanced significantly, deprecating numerous functions that older CMS systems blindly rely upon. By constructing a custom Dockerfile, we isolate the outdated environment, ensuring the main host remains secure and modern while the legacy application runs exactly as it did years ago.

Step 1: Constructing the Base Image and Compiling PHP 5.6

The foundation of this setup is an Ubuntu 16.04 image. After updating the repository lists, we install a massive array of development tools and libraries. Crucially, rather than relying on package managers that default to newer versions, we download the PHP 5.6.40 source directly and compile it with explicit flags to support everything from MySQL to GD and OpenSSL.

Base Image Configuration:

# Use an Ubuntu base image
FROM ubuntu:16.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV PHP_VERSION=5.6.40

# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    apache2 \
    apache2-dev \
    libxml2-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    libmysqlclient-dev \
    libreadline-dev \
    libzip-dev \
    libbz2-dev \
    libjpeg-dev \
    libpng-dev \
    libxpm-dev \
    libfreetype6-dev \
    libmcrypt-dev \
    libicu-dev \
    zlib1g-dev \
    libxslt-dev \
    libsodium-dev \
    libmagickwand-dev \
    libpcre3-dev \
    curl \
    wget \
    re2c \
    bison \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Download and extract PHP source
RUN wget --no-check-certificate https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz && \
    tar -xvf php-${PHP_VERSION}.tar.gz && \
    rm php-${PHP_VERSION}.tar.gz

# Change directory to PHP source
WORKDIR php-${PHP_VERSION}

# Install MySQL development libraries for the mysql extension
RUN apt-get update && apt-get install -y --no-install-recommends libmysqlclient-dev && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Reconfigure and build PHP to include the MySQL extension
RUN ./configure \
    --prefix=/usr/local/php5.6 \
    --with-apxs2=/usr/bin/apxs \
    --enable-maintainer-zts \
    --with-mysql \
    --with-mysqli \
    --with-pdo-mysql \
    --enable-mbstring \
    --enable-calendar \
    --enable-ctype \
    --with-curl \
    --enable-exif \
    --enable-ffi \
    --enable-fileinfo \
    --enable-filter \
    --enable-ftp \
    --with-gd \
    --with-gettext \
    --with-iconv \
    --with-imagick \
    --with-libdir=/usr/lib/x86_64-linux-gnu \
    --enable-json \
    --with-libxml-dir=/usr \
    --enable-mbstring \
    --with-mysqli=mysqlnd \
    --with-openssl \
    --enable-pcntl \
    --with-pcre-dir=/usr \
    --enable-pdo \
    --enable-phar \
    --enable-posix \
    --with-readline \
    --enable-session \
    --enable-shmop \
    --enable-simplexml \
    --enable-sockets \
    --with-sodium \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --enable-tokenizer \
    --enable-xml \
    --enable-xmlreader \
    --enable-xmlwriter \
    --with-xsl \
    --enable-opcache \
    --enable-zip \
    --with-zlib && \
    make -j$(nproc) && \
    make install

# Create a symlink for PHP to /bin
RUN ln -s /usr/local/php5.6/bin/php /bin/php

# Enable mod_rewrite module and configure Apache to allow .htaccess files
RUN a2enmod rewrite

# Configure Apache for PHP
RUN echo "LoadModule php5_module /usr/local/php5.6/lib/php/extensions/no-debug-non-zts-20131226/libphp5.so" >> /etc/apache2/apache2.conf && \
    echo "AddType application/x-httpd-php .php" >> /etc/apache2/apache2.conf && \
    echo "DirectoryIndex index.php" >> /etc/apache2/apache2.conf

# Allow overrides for .htaccess files in the Apache configuration
RUN echo "" >> /etc/apache2/apache2.conf && \
    echo "    AllowOverride All" >> /etc/apache2/apache2.conf && \
    echo "" >> /etc/apache2/apache2.conf

# Switch Apache to prefork MPM if needed (threaded MPM requires threadsafe PHP)
RUN a2dismod mpm_event mpm_worker && a2enmod mpm_prefork

# Copy test PHP file
RUN echo "" > /var/www/html/phpinfo.php

# Expose HTTP port
EXPOSE 80

# Start Apache
CMD ["apachectl", "-D", "FOREGROUND"]

Step 2: Integrating WP-CLI for Legacy Database Management

The next step was adding WPCLI. A command-line interface provides indispensable efficiency, letting you orchestrate database search-and-replace functions and plugin uninstalls without relying on the often broken graphical interface of a legacy WordPress installation.

Adding WP-CLI Configuration:

# Use your custom PHP image as the base
FROM php56:latest


# Install dependencies for WP-CLI
RUN apt-get update && apt-get install -y \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Ensure PHP is linked to /usr/local/bin/php (change path based on where PHP was compiled)
ENV PATH="/usr/local/bin:/usr/local/php5.6/bin:$PATH"
RUN ln -s /usr/local/php-5.6.40/bin/php /usr/local/bin/php

# Install WP-CLI
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
    php wp-cli.phar --info && \
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp

# Verify WP-CLI installation
RUN wp --info

# Expose port 80 (optional)
EXPOSE 80

# Start Apache (or your desired service)
CMD ["apache2ctl", "-D", "FOREGROUND"]

Step 3: Docker Compose for Database and Application Orchestration

With our custom PHP and WP-CLI images prepared, we must connect them to an appropriate database. And then using docker compose to bring up Apache / PHP / MYSQL services online:

version: '3.7'
services:
  mysql:
    image: mysql/mysql-server:5.7.37
    environment:
     MYSQL_DATABASE: webdesign
     MYSQL_USER: ROOT
     MYSQL_PASSWORD: PASSWORD
    restart: always
    volumes:
     - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
     - "3307:3306"
  legacy-php:
    depends_on:
     - mysql
    image: php5.6-apache-wpcli
    volumes:
     - .:/var/www/html
    ports:
     - "80:80"

Conclusion: Successfully Exporting and Restoring Data

The entire purpose of this intricate setup is to gain functional access to obsolete sites. Over writing the Wordpress 3.4 files with 3.7 allowed me to export an XML.

By establishing a robust, containerized environment using a custom Dockerfile with PHP 5.6 and Apache, developers can confidently preserve their old digital assets, export vital historical content, and gracefully upgrade their infrastructure.

Frequently Asked Questions (FAQ)

Q: Why do I need a custom Dockerfile for PHP 5.6?
A: Many modern base images come pre-installed with PHP 7 or 8. To successfully run legacy applications like WordPress 3.4, which heavily rely on deprecated PHP functions, compiling PHP 5.6 from source within the container ensures complete compatibility and stability.

Q: Can I install WP-CLI on a legacy PHP 5.6 image?
A: Yes, WP-CLI can be installed on PHP 5.6 by downloading the specific phar file, linking the compiled PHP binary to your system path, and making the executable globally accessible, allowing you to seamlessly manage older WordPress installations.

Q: What base OS is recommended for compiling older PHP versions?
A: Ubuntu 16.04 is an excellent base OS because its package repositories still contain many of the older dependency versions (like libmysqlclient-dev and specific openssl versions) needed to cleanly compile and run PHP 5.6 without encountering severe dependency conflicts.