17

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!

Archivar
  • 273
  • 1
  • 2
  • 9

2 Answers2

24

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
Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
  • Thank you so much for your quick reply! I made exactly the changes you supplied in your answer, but still could not get it to work. The docker image could be built and the container can be run, but the rewrite rule does not work. I assume it is the "AllowOverride" directive missing? If so, how would I activate it via sed? For further reference, I'll put the content of my .htaccess here: `Options +FollowSymLinks` `RewriteEngine on` `RewriteRule ^(.*)$ https://www.youtube.com [R=301,L]` (And yes, I would like to forward calls to my server to youtube as of now.) – Archivar Apr 13 '18 at 08:34
  • 2
    @Archivar the post didn't resolve your question as i see it didn't work for you. Please do not mark it as correct answer. – GusDeCooL Jun 19 '19 at 06:33
  • @Archivar yes above DockerFile doesn't work for me. Please don't mark as correct. – Nishant kumar May 05 '20 at 08:55
  • 2
    Maybe in April 2018 everything worked, maybe things changed since then, maybe you shouldn't simply copy & paste without even trying to understand what's going on - but maybe the answer is simply just wrong. lol – Thomas Schwärzl May 05 '20 at 09:42
  • Currently cannot enable mod_rewrite in any way for that image – Anton Mitsev Apr 26 '23 at 14:45
  • Seriously? To enable a module you need to do a cryptic 'sed' command to modify a configuration file? I haven't tried the above, and it likely works given the upvotes, but surely there is a better more standard way of configuring the server to enable this. – Jim Leask Aug 25 '23 at 14:35
10

I suppose you want to have working .htaccess rewrite mechanism. To do that you need to do 2 things:

  • In httpd.conf enable mod_rewrite by uncommenting line:

    LoadModule rewrite_module modules/mod_rewrite.so

  • In httpd.conf change AllowOverride for your working directory to All (Do not change every AllowOverride because this is security issue)

    AllowOverride All

It is a good practice to download httpd.conf from container, change this values and then upload new httpd.conf when building image to the same directory:

for example

  • extract httpd.conf docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf

  • upload new httpd.confon build process: COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf