How to Oust XAMPP with Docker for PHP, MySQL and WordPress

Posted January 4, 2024
How to Oust XAMPP with Docker for WordPress, PHP and MySQL

Are you giving up on using XAMPP? In this post, I’ll give the Reasons why you shouldn’t be XAMPP or similar tools from 2024 moving forward. You will learn why you should Stop using XAMPP and switch your PHP, WordPress, and MySQL local development to Docker containers.

Setting up XAMPP is a frustrating process. If you’re looking to replace traditional tools like XAMPP with a Docker container, this perfect XAMPP (WAMP Server) vs Docker container tutorial is for you. Dive in and learn how to use Docker Compose to streamline your PHP local development.

To make this post fun, I will create a step-by-step process for using Docker Compose with PHP instead of XAMPP with the following summary:

  • How to run Docker and PHP instead of XAMPP.
  • How to Stop Using XAMPP and switch to Docker.
  • What you need to run PHP with Docker and not XAMPP.
  • Replacing XAMPP MySQL and PhpMyAdmin with Docker services.
  • How to connect MySQL and PHP on Docker.
  • How to replace XAMPP with Docker for WordPress.

A Step-by-Step Guide to Replace XAMPP With Docker Compose

If you are looking for a Step-by-Step Guide for replacing XAMPP with Docker for PHP and MySQL, I have written this How to Oust XAMPP with Docker for WordPress, PHP, and MySQL đź’Ş to painless install PHP extension, update ini settings, configure MySQL and other PHP XAMPP centered configurations.

Why I Consider XAMPP Limited and Docker Compose the King

I have been in the PHP and WordPress with XAMPP game for almost 10 years now. The main challenge has been always how to perfectly set up XAMPP locally. Through this headache process, every project will have some specific hosting needs and configurations. You will obviously encounter multiple occasions where your project is not working as you expected.

It’s a lot of work to have every project have its PHP versions and extensions, and are on your machine.

It here you need a Docker container to choose what you need for every project. And after developing your app, at some point, a real server is a must to run as production. There is no simple and sane way to run XAMPP on production while ensuring scalability, performance, and all production factors.

Reasons to Move XAMPP to Docker for PHP and WordPress

See, just ask yourself, how do you use PHP 7.4 in XAMPP? Will you be able to run two different PHP versions? Will your local app run exactly the same on the server?

This development and production lifecycle is what Docker simplifies to make this respective task done within one setup. The Reasons to Move XAMPP to Docker for PHP and WordPress include:

  • Isolation and portability of your dependencies, libraries, and configurations consistently across different environments.
  • XAMPP can’t scale. Docker scales your application by running multiple containers.
  • Docker container reduces overhead as it shares the host OS kernel to run apps.
  • Packaging your application and its dependencies into a single container makes it easier to create production deployments on different servers or the cloud.
  • If you love CI/CD workflow, containers are made for you.

How to Move XAMPP to Docker Step-by-Step

Let’s now dive and learn what you need to replace XAMPP with Docker compose and run your PHP apps as containers. XAMPP makes you lazy with zero knowledge, Let’s now Explore Docker container and the opportunities it offers to us as Web developers

Step One: What you need to Replace XAMPP with Docker

XAMPP is an open-source server. It doesn’t require many prerequisites to get installed. Now to Replace it with Docker, you need:

  • Docker Desktop installed on your machine.
  • Once installed, Docker must be up and running.
  • I will assume you have some basic background working with Docker.

Step Two: Spinning Up a PHP Docker Container, Instead of XAMPP

XAMPP bundles the PHP version you’re using, the server, MySQL, and PhpMyAdmin. When using Docker Container, you use Docker Compose to help you spin up different services as one. Docker will then Combine them and run them to run your application the same on the server. In this example, you will create a Docker container for:

  • PHP with Apache to serve PHP code:
  • MySQL to spin up a database.
  • PhpMyAdmin to connect and access MySQL.

These are the same services XAMPP uses. But the advantage is, that with Docker, you already know what advantages you get to eliminate most of XAMPP’s repetitive configuration tasks.

To achieve this, you don’t need XAMPP anymore. If you have Docker installed and running, you will create a project directory as usual. However, you will need two important files, dockerfile and docker-compose.yml as follows:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

  • dockerfile will represent the PHP version you want to use and how you will install any PHP extension your app needs.
  • docker-compose.yml - container the code instructions needed to spin up Docker Container using Compose and expose your app to the web.
  • src/index.php contains PHP application code. You can scale up different code files as you would like to. Provided the files and code are working based on PHP structuring.

Step Three: Setting Up the Docker PHP Environment with Dockerfile

Now, Dockerfile in your ultimate XAMPP magic. Here, you specify what your project wants. Do you remember how hard it is to get PHP extensions installed and configured? Dockerfile makes this step so simple. At the same time, you get to choose what PHP version fits every specific project you are working on without installing additional Environments. Here is how:

Go to the dockerfile you created in the above step and:

  • Add PHP version using the Docker PHP Docker Hub image. In this case, I use PHP coupled with Apache as follows:
FROM php:8.2-apache

Just Looking at this line, you will run PHP version 8.2. How do you run a different PHP version? You only need to update 8.2 with a PHP version compatible with your projects, for example, php:7.4-apache, php:7.0-apache, etc.

  • Enable PHP Apache using the following command:
RUN a2enmod rewrite
  • The fun part is how you install your extensions. This example uses MySQL. Therefore, you require extensions such as mysqli, pdo, and pdo_mysql ready. Use the RUN command and install them using the docker-php-ext-install command as follows:
RUN docker-php-ext-install mysqli pdo_mysql pdo

Note: Some PHP extensions require extra steps, such as enabling and configuring them. Check this how Install PHP Extensions with docker php-ext-install and Pecl Commands tutorial for extra steps.

At this point, your Dockerfile is complete as such:

# Using the PHP 8.2 as Apache base image
FROM php:8.2-apache
# Enable the Apache module rewrite
RUN a2enmod rewrite
# Install PHP extensions for MySQL database connection
RUN docker-php-ext-install mysqli pdo_mysql pdo

Step Four: Running PHP and MySQL with Docker Compose (Completely Oust XAMPP)

A single installed XAMPP packages, PHP, PhpMyAdmin, and MySQL together. If you are on Docker, you will use Docker Compose. In this case, the docker-compose.yml file contains the instructions needed to have all these individual components running as one.

Go to your docker-compose.yml file and add the following code instructions:

version: "3.9"

services:
  # PHP Application Service
  php-app:
    build:
      # Build Dockerfile context on the current directory
      context: .
      dockerfile: Dockerfile 
    volumes:
      # local ./src directory contains PHP code
      # Mount it to the to /var/www/html on the container
      - ./src:/var/www/html
    depends_on:
      # Ensure MySQL is started before PHP
      - database-container
    ports:
      # Let Apache Expose PHP to the web on 3000
      - 3000:80 

  # Database Container Service
  database-container:
    # Get MySQL image from Docker Hub
    image: mysql:latest
    environment:
      # Set the MySQL root password
      MYSQL_ROOT_PASSWORD: php_mysql_pass
      # Create your MySQL database user
      MYSQL_USER: php_test_user
      # Create a sample MySQL database
      MYSQL_DATABASE: php_mysql_db
      #Add the password for the MySQL user
      MYSQL_PASSWORD: php_user_pass
    ports:
      # MySQL port
      - "3306:3306"

  # PhpMyAdmin Service
  phpmyadmin:
    image: phpmyadmin:latest
    links:
      # Link the MySQL service to allow phpmyadmin access to the MySQL server
      - database-container
    ports:
      - "3001:80"
    environment:
    # PhpMyAdmin host is a MySQL Database container
      PMA_HOST: database-container 
      # Set the MySQL root password to open PhpMyAdmin on the web
      MYSQL_ROOT_PASSWORD: php_mysql_pass

Here is what’s happening:

  • You have PHP Apache, MySQL, and PhpMyAdmin services to Completely oust XAMPP.
  • The service php-app will run your Dockerfile and get your PHP configurations set.
  • Note that you have a volume ./src:/var/www/html. It’s critical. The /src directory points to the location of your PHP code files and mount to /var/www/html in the container so Apache can run them.
  • database-container runs MySQL. Note the environment variables needed to configure MySQL requirements.
  • To access MySQL, you create a phpmyadmin service that connects to MySQL and has access to PhpMyAdmin on the web.

The above example only uses Apache with PHP and MySQL, I have the following tutorial if you want to use other variations, such as PostgreSQL, MongoDB, Nginx, or PHP-FPM:

Step Five: Connecting PHP with MySQL on Docker

Assume you want to run a simple script that checks if you’re able to connect PHP with MySQL. Based on the directory structure we have; you will go to the src/index.php file and add the script as such:

<?php
$serverName = "database-container";
$databaseName = "php_mysql_db";
$dbUsername = "php_test_user";  
$dbPassword = "php_user_pass"; 

// Create a connection to MySQL service
$conn = mysqli_connect($serverName, $dbUsername, $dbPassword, $databaseName);

// Check connection status and log the message
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully to MySQL";

// Close connection
mysqli_close($conn);
?>

The above connection variables match MySQL variables added to the docker-compose.yml file. In this case:

  • Check the server’s name. If on XAMPP it would be localhost. On Docker, the server name depends on the name of your MySQL service. I named it database-container. Therefore, the server’s name must reflect as such.

Step Six: Running the Docker Container

To run your whole application on Docker, you now only need to use a single command pointing to your working directory path. Because you are using docker-compose.yml file, you will use a Docker Compose command as follows:

docker-compose up -d --build

This command will package and run your app as follows:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

Note that you might be asked to share the local directory with Docker, especially on Windows:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

Check your Docker Engine and PHP Docker container should be running:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

Docker should server index.php on http://localhost:3000/. Open It on the browser and the script will run and execute a connection to MySQL:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

Step Seven: Accessing XAMPP PhpMyAdmin Module with Docker PhpMyAdmin Service

On XAMPP, you access MySQL with PhpMyAdmin. The same is not limited To Docker. The PhpMyAdmin is accessible on http://localhost:3001/ as follows:

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

To access MySQL, use:

  • php_test_user as the Username and
  • php_user_pass as the password to log in.

How to Oust XAMPP with Docker for WordPress, PHP and MySQL

The Above is the exact XAMPP PhpMyAdmin UI, but this time on Docker. Verify this by checking the database name you added in your docker-compose.yml file as MySQL variables.

Step Eight: How to Replace XAMPP with Docker for WordPress

WordPress uses PHP. Locally, you use XAMPP. Just like PHP you can use Docker and get a whole WordPress setup running on a single Docker container.

To make this process independent, I create a WordPress Local Development Environment with Docker Compose guide for you. Check it out, if you are looking to get XAMPP replaced with Docker when running WordPress.

Conclusion

Have you replaced Docker with XAMPP now? This guide taught you:

  • How to run Docker and PHP instead of XAMPP.
  • Replacing XAMPP MySQL and PhpMyAdmin with Docker services.
  • How to connect MySQL and PHP on Docker.
  • How to replace XAMPP with Docker for WordPress.
How to Oust XAMPP with Docker for PHP, MySQL and WordPress

Written By:

Joseph Chege