Skip to content

A local Apache server in Docker for testing .htaccess

Testing .htaccess rules on a live site is asking for trouble: one typo and visitors get a 500 error. It is far calmer to have your own Apache server on your computer, where you can break things as much as you like. Docker lets you set one up in minutes without installing Apache or PHP on your system - and when you no longer need it, one command removes it.

In short

  • Docker runs programs in "containers": ready, isolated boxes with a server inside.
  • Four small files give you a local Apache with PHP and .htaccess enabled at localhost:8095.
  • The same approach sets up WordPress with a MySQL database and phpMyAdmin for testing plugins or updates.

A container, like a shipping container

A shipping container has one standard shape, so every crane and every ship can handle it, whatever is inside. A Docker container works the same way: inside is a server with all the software it needs, and outside are standard "handles" - a port you connect through and a folder you share with it. You do not need to know how Apache is configured inside, or install it yourself.

Ready recipes for containers are images (such as the official php:8.3-apache), and Docker Compose describes in one file which containers to run and how they connect.

Installing

  • Windows and macOS - install Docker Desktop from docker.com. Compose comes with it.
  • Linux - install Docker Engine from your distribution's packages or following docs.docker.com. The docker compose command (with a space) is the compose plugin, usually installed alongside.

Check that it works:

docker --version
docker compose version

An Apache server for testing .htaccess

Create a folder, say htaccess-playground, with four files in it. The first is the Dockerfile - a recipe for your own image, built on the official PHP image with Apache:

FROM php:8.3-apache
# mod_rewrite (redirects), mod_headers (headers), mod_expires (caching), mod_deflate (compression)
RUN a2enmod rewrite headers expires deflate \
 && sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

The second line does two things: it enables the modules most rules rely on, and it lets Apache read .htaccess files (this image ignores them by default). That is the step people most often skip on their own servers, and then nothing works 🙂

The second file, compose.yaml, says how to run the container:

services:
  web:
    build: .
    ports:
      - "8095:80"
    volumes:
      - ./public:/var/www/html

That is: build the image from the Dockerfile, expose it on port 8095 of your computer, and mount the public folder as the site's folder. So every change to the files shows up immediately, without a rebuild.

The third file, public/index.php, shows what reached the script - very handy when checking what a rule did:

<?php
header('Content-Type: text/plain; charset=utf-8');
echo "Address:      {$_SERVER['REQUEST_URI']}\n";
echo "Script:       {$_SERVER['SCRIPT_NAME']}\n";
echo "Parameters:   " . http_build_query($_GET) . "\n";

And the fourth, public/.htaccess, with rules to test:

RewriteEngine On
RewriteRule ^old-page/?$ /new-page/ [R=301,L]
RewriteRule ^product/([0-9]+)/?$ /index.php?id=$1 [QSA,L]

Run it and test

In the folder with the files:

docker compose up -d --build

The first run downloads the image (about 500 MB); after that it takes seconds. Now check your rules, preferably with curl, which does not cache redirects the way a browser does:

curl -I http://localhost:8095/old-page
curl "http://localhost:8095/product/15?colour=red"

The first command shows HTTP/1.1 301 Moved Permanently and a Location header, the second prints id=15&colour=red. Change .htaccess, save, check again - no restart needed.

When something does not work, look at the server logs:

docker compose logs -f web

And when you are done:

docker compose down

This is exactly the server I used to check the examples in .htaccess RewriteRule flags explained and Speeding up a website with .htaccess. To follow what your rules do step by step, see Debugging .htaccess too.

WordPress with a database and phpMyAdmin

The same idea works for a whole site. A compose.yaml with three containers - a MySQL database, WordPress and phpMyAdmin:

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: root-password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wp-password
    volumes:
      - database:/var/lib/mysql

  wordpress:
    image: wordpress:php8.2-apache
    depends_on:
      - db
    ports:
      - "8096:80"
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wp-password
    volumes:
      - ./site:/var/www/html

  phpmyadmin:
    image: phpmyadmin:5
    depends_on:
      - db
    ports:
      - "8097:80"
    environment:
      PMA_HOST: db

volumes:
  database:

After docker compose up -d, the WordPress installer waits at localhost:8096 and phpMyAdmin at localhost:8097. Match the MySQL and PHP versions to your host, so you test under the same conditions.

What stays and what goes

  • The site's files (./site) are an ordinary folder on your disk. You can edit the theme, plugins and .htaccess there, and even keep it in Git (the basics are in Git in 15 minutes).
  • The database lives in the database volume, which Docker manages. It survives docker compose down, but docker compose down -v removes it along with the containers.
  • The container is disposable. Anything you change inside it, outside the mounted folders and volumes, disappears on the next start. That is a feature: you always start from a clean server.

Tips

  • Port taken? Change the first number in "8095:80" to another one, e.g. "8080:80".
  • An empty site, or 404 for everything? Docker may not see the folder your files are in (Docker Desktop only shares selected folders, and some Linux installs cannot see temporary folders). Move the project into your home folder.
  • A copy of a real site: put the files in ./site and import a database dump through phpMyAdmin. Replace the addresses in the database with a search-and-replace plugin or the wp search-replace command.
  • Without Docker? Possible too (XAMPP, Laragon, MAMP), but then everything is installed on your system, and several PHP versions at once becomes a hassle.
Advertisement