Module 14 Lesson 3: Project: Legacy PHP/MySQL App
·DevOps

Module 14 Lesson 3: Project: Legacy PHP/MySQL App

Bring the past into the future. Learn how to wrap an old PHP application and a MySQL database into containers, providing safety and portability to legacy code.

Module 14 Lesson 3: Project: Legacy PHP/MySQL App

Legacy apps are often the most stressful to manage. They require old versions of software (like PHP 5.6) that are no longer available for your modern operating system. Docker is the ultimate "Time Machine" for these apps.

1. The Survival Dockerfile

We need an image that combines PHP and an Apache web server.

# We use an older version of PHP because the app won't run on PHP 8
FROM php:7.4-apache

# 1. Install old MySQL extensions (modern ones are different)
RUN docker-php-ext-install mysqli pdo pdo_mysql

# 2. Enable Apache rewrite module (for pretty URLs)
RUN a2enmod rewrite

# 3. Copy the old source code
COPY ./src /var/var/www/html/

# 4. Fix permissions (Apache needs to write to 'uploads')
RUN chown -R www-data:www-data /var/www/html/uploads

2. Managing the Multi-Container Legacy

Legacy apps often have hardcoded database IPs (like 127.0.0.1). We need to fix this without changing too much code.

services:
  web:
    build: .
    ports:
      - "80:80"
    environment:
      - DB_HOST=db
    depends_on:
      - db

  db:
    # Use an older MySQL version to avoid password plugin errors
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=oldsecret
      - MYSQL_DATABASE=legacy_app
    volumes:
      - ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql

3. Why Dockerize Legacy Apps?

  1. Stop the Rot: You no longer have to worry about your OS updates breaking the app. The app is "Frozen" in its container.
  2. Safety: You can run the legacy app behind a modern Nginx proxy (Module 10) to add SSL and basic security that the old app lacks.
  3. Developer Experience: New developers don't have to spend 2 days installing "XAMPP" or "WAMP" to get the old project running.

4. The "Import" Trick

Notice the init.sql volume in the Compose file? Docker's MySQL image will automatically "Execute" any .sql file in that folder the first time it starts. This is the fastest way to "Onboard" a new database.


Exercise: The Time Machine

  1. Find a very old PHP script (even just a mysqli_connect loop).
  2. Build the image above.
  3. Try to connect to the database using the environment variable DB_HOST.
  4. If the app fails with a "Headers already sent" error, how would you check the PHP logs inside the container? (Hint: docker logs).
  5. Why did we use mysql:5.7 instead of mysql:latest? (Research: "MySQL 8 authentication plugin change").

Summary

Containerizing legacy apps is one of the highest-value tasks a DevOps engineer can perform. It turns a "Fragile" system into a "Reliable" one, allowing you to maintain old software with zero stress while you focus on building the new stuff.

Next Lesson: Scale and complexity: Building a multi-service microservice project.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn