Test your Apache configs and .htaccess locally

Let’s be honest, modifying Apache configurations or .htaccess is always scary on a remote server, especially if you expect some traffic to reach your website. And testing in production has never been a good choice. This is how I created my small sandbox to play around with Apache before applying things to production servers and eventually get fired.

ℹ️ Sandbox is available on my GitHub account. Check it out.

Requirements 📄

I will assume you are already familiar with the basics of Docker. Also, that you have a running Docker installation, with ability to launch some commands in a terminal. This guide was made using Linux, it should be easy to reproduce on other systems.

Basic file structure 📁

First let’s create some empty files and folder. This is what your project folder should


Configuring Docker 🏗️

Let’s work on the Dockerfile. We want it to fetch the last Apache image, and copy the httpd.conf inside the container so it’s correctly applied.

FROM httpd:latest
COPY conf/httpd.conf /usr/local/apache2/conf/.

Ok this one was pretty easy, let’s move forward to the docker-compose.yml. The latter should build the image from the Dockerfile we just created, and use the htdocs folder as source for the served files. Also, we have to publish the port 80 to be able to request the server inside the container from our browser. This should look like this.

version: '3.9'

      context: .
    container_name: apache-sanbox
      - ./htdocs:/usr/local/apache2/htdocs:ro
      - "80:80"

The container_name is totally optional.

Apache configuration 👨‍🔧

At this point remember we are copying a httpd.conf file which is empty. Pretty sure Apache will not appreciate. Populate your httpd.conf file with a default Apache configuration. You can find one here, on the official apache/httpd repo.

Test run ⚙️

It’s time to make the whole thing shine! Try your luck with:

docker compose up

If your container is attached and seem to be running, it’s time to verify in your browser, head over to http://localhost, and see if anything is served. It should render your index.html file.

.htaccess 🧑‍💻

If you want to test a .htaccess file, create the file in the htdocs folder. Here is an example:

Deny from all

Yeah I know this is basically denying any kind of access to your root served directory. Not very useful in real life but pretty straightforward when it comes to verify that .htaccess is working 😀.

Mimic your production domain 🕵️

It’s much easier to have a domain that is similar to the production one to make rules and stuff. Remember, the /etc/hosts file can be use to bind domains to addresses, locally. So this is what you can do if your website is “bubblegum.tech”:     bubblegum.tech

Save the file and try to type in http://bubblegum.tech in your browser. The Apache server should respond and you now can tweak your Apache configuration with ease.

⚠️ Don’t forget to comment out or to remove this line after your done, to avoid the panic the next time you want to check your production website on this machine.

💡I recommend you to pick a more intuitive domain such as “bubblegum.tech.local” or “bubblegum.local” to prevent confusion.

What’s next ⁉️

Now it’s time to grab the Apache documentation and try to craft some redirections, URL rewrites, cache rules and more.

If you are stuck somewhere in this tutorial, if you liked it, if there is a mistake or for any request, feel free to contact me, or simply use the comment section below 😉

Related Posts

Boost your website for free with Cloudflare

Although Cloudflare is one of the most popular and performant CDN on the market, it’s not necessarily overpriced. I mean, you can benefit even from the free…

Leave a Reply

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