How to enable mod_rewrite in Docker Official PHP

Maybe are suffering like me, trying to run you PHP application that uses a .htaccess file to overwrite some URL on official PHP Docker image, in my case was using Laravel.

The problem? Well, I have to add some feature to and old app made in Laravel some year ago and I did using xampp, we are living in the future so now we have containers. I don’t be very good using Docker yet I think I have a lot to learn but I try my best.

I have a repository with a Docker configuration to run a PHP server with a mysql services that fit with my need to run the old app and work in the new features. The first problem that I have was to enable the directory where the Laravel app is, I solved this adding the volume to the docker-compose file

version: '3.1'
services:
  laravel:
    build: .
  ports:
    - ${APACHE_PORT}:80
  links:
    - db
  volumes:
    - ./www:/var/www/html/
    # Laravel app
    - ./local_laravel_app:/var/www/server_laravel_app/

Great, now the app is available in the PHP server container, but there one thing left to fix, the mod_rewrite. This module doesn’t come enabled by default in php:x.x-apache image thus is have to be added to the Dockerfile configuration. So the Docker file it can look like this:

FROM php:5.6-apache
RUN docker-php-ext-install mysqli
RUN a2enmod rewrite
RUN service apache2 restart

We have to add the a2enmod to enable the mod_rewrite and restart the Apache server.

Leave a Reply

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