Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add mod_rewrite to Docker image httpd:alpine

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!

like image 259
Archivar Avatar asked Apr 13 '18 05:04

Archivar


2 Answers

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

like image 95
Dawid Świtoń-Maniakowski Avatar answered Oct 03 '22 22:10

Dawid Świtoń-Maniakowski


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
like image 30
Thomas Schwärzl Avatar answered Oct 03 '22 22:10

Thomas Schwärzl