Creating a Docker Container for a Legacy PHP 5.6 System with SSL on Apache

Hello, developers! Today’s blog post is a special one for those working on legacy systems. We’re going to walk through creating a Docker container for a legacy PHP 5.6 application, complete with Apache and self-signed SSL certificates. Perfect for local development environments where you need to keep older systems up and running securely. Let’s get started!

Why Dockerize a Legacy PHP 5.6 System?

  • Standardization: Uniform environment regardless of your local setup.
  • Isolation: Keep your machine clean; no need to install outdated dependencies system-wide.

Step 1: Generate a Self-Signed SSL Certificate

First things first, we need an SSL certificate. Since we’re just setting up a local environment, a self-signed certificate will do:

Open your Terminal or Command Prompt.

Run this OpenSSL command to create your certificate and key:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt

Step 2: Crafting the Dockerfile

Of course you want to create a Docker file named Dockerfile!

We’ll use ubuntu:22.04 as our base image and install Apache and PHP 5.6:

Start with Ubuntu 22.04 and install Apache:

FROM ubuntu:22.04
RUN apt-get update && apt-get install -y apache2

Install PHP 5.6 and necessary extensions:

RUN apt-get install -y php5.6 php5.6-cli php5.6-json php5.6-common php5.6-mysql \
php5.6-zip php5.6-gd php5.6-mbstring php5.6-curl php5.6-xml php5.6-bcmath && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Step 3: Building and Running Your Container

With your Dockerfile ready, build and run your container:

Build the image:

docker build -t my-legacy-php56-app .

Run the container:

docker run -d -p 443:443 my-legacy-php56-app

Accessing Your Legacy PHP 5.6 Site

Visit https://localhost in your browser, and voilĂ ! You should see your PHP 5.6 application running. Expect a browser warning about the self-signed certificate; that’s normal for local development.

Wrapping Up

And there you go! You now have a Dockerized legacy PHP 5.6 environment with SSL, ideal for keeping those older projects going in a secure, modern workflow. Remember, while great for development, always use certificates from trusted authorities in production.

FROM ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -y && \
    apt-get install -y software-properties-common apache2 git nano && \
    add-apt-repository ppa:ondrej/php && \
    apt-get update -y && \
    apt-get install -y php5.6 php5.6-cli php5.6-json php5.6-common php5.6-mysql php5.6-zip php5.6-gd php5.6-mbstring php5.6-curl php5.6-xml php5.6-bcmath && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer 


RUN a2enmod rewrite headers && \
    sed -ri -e 's/^([ \t]*)(<\/VirtualHost>)/\1\tHeader set Access-Control-Allow-Origin "*"\n\1\2/g' /etc/apache2/sites-available/*.conf && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

COPY ssl-self-signed-certs/localhost.crt /etc/ssl/certs/
COPY ssl-self-signed-certs/localhost.key /etc/ssl/private/

# Customize the default SSL site configuration
RUN sed -i \
    -e 's,/etc/ssl/certs/ssl-cert-snakeoil.pem,/etc/ssl/certs/localhost.crt,g' \
    -e 's,/etc/ssl/private/ssl-cert-snakeoil.key,/etc/ssl/private/localhost.key,g' \
    /etc/apache2/sites-available/default-ssl.conf

# Enable SSL module and the default SSL site
RUN a2enmod ssl && a2ensite default-ssl.conf

# Expose port 443 for SSL
EXPOSE 443

# Clear out pre-existing vendors (if any)
WORKDIR /var/www/html
RUN rm -rf vendor/
#COPY . /var/www/html/

CMD ["apachectl", "-D", "FOREGROUND"]

Happy coding, and enjoy your secure, legacy-friendly dev environment!

Leave a Reply

Your email address will not be published. Required fields are marked *