Add mod_rewrite to Docker image httpd:alpine

Archivar picture Archivar · Apr 13, 2018 · Viewed 24.1k times · Source

I have almost no experience with Alpine Linux, to be honest, but I like its approach and therefore want to change that. I'm also relatively new to Docker, so please bear with me if this is a "stupid" question.

What I would like to achieve is building upon the httpd:alpine image and extending the HTTPd to my needs.

That would include activating the mod_rewrite module and copying a custom .htaccess into the image.

Here is what I have so far:

FROM httpd:alpine

# Copy .htaccess into DocumentRoot
COPY ./.htaccess /var/www/html/

RUN apk update
RUN apk upgrade
RUN apk add apache2-utils
RUN a2enmod rewrite
RUN rc-service apache2 restart

My issue is now, that I constantly receive an "a2enmod not found" error, which I don't know how to resolve. That might be because a2enmod is a pure Debian/Ubuntu/... thing, but I don't know any alternative way of activating mod_rewrite (or any module for that matter).

Thank you all very much in advance for your support!

Answer

Thomas Schwärzl picture Thomas Schwärzl · Apr 13, 2018

mod_rewrite is installed by default with apache in alpine, so no need to install it again. So here's how you enable mod_rewrite in Alpine:

FROM httpd:alpine

# Copy .htaccess into DocumentRoot
COPY ./.htaccess /var/www/html/

RUN sed -i '/LoadModule rewrite_module/s/^#//g' /usr/local/apache2/conf/httpd.conf

RUN { \
  echo 'IncludeOptional conf.d/*.conf'; \
} >> /usr/local/apache2/conf/httpd.conf \
  && mkdir /usr/local/apache2/conf.d