24

I use docker compose. However, when I run "docker-compose up", I came across an error : /var/www/html/.htaccess: Invalid command 'RewriteEngine'.

Can you tell me where I fails ??

Project architecture :

project-name /
             / docker-compose.yml
             / Dockerfile
             / apache.conf
             / php.ini
             / src /
                   / index.php
                   / .htaccess

docker-compose.yml :

web:
    build: .
    ports:
        - "80:80"
    volumes:
        - ./src:/var/www/html
        - php.ini:/usr/local/etc/php/conf.d/30-custom.ini
        - apache.conf:/etc/apache2/sites-enabled
    environment:
        - ALLOW_OVERRIDE=true

Dockerfile :

FROM php:7.0-apache

RUN a2enmod rewrite

RUN service apache2 restart

ADD ./src /var/www/html

php.ini :

display_errors=1
error_reporting=E_ALL

apache.conf (with my ip address) :

<VirtualHost *:80>
    ServerName xxx.xxx.xx.xxx
    DocumentRoot /var/www/html
</VirtualHost>

I type in the command line :

docker@default:/blabla/project-name$ docker-compose up

it returns me :

AH00558: apache2: Could not reliably determine the server's fully 
qualified domain name, using xxx.xx.x.x. Set the 'ServerName' directive 
globally to suppress this message

and

/var/www/html/.htaccess: Invalid command 'RewriteEngine', 
perhaps misspelled or defined by a module not included in the server
configuration

and in the browser, in my ip address (http://xxx.xxx.xx.xxx/) :

500 Internal servor error

my .htaccess :

<files .htaccess>
    Require all denied
</files>
Options +FollowSymlinks -Indexes -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$   /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

I'm on windows and i use Oracle VM Virtual Box.

Thank you in advance !

EDIT : I should say that if I delete rewrite rules, everything works.

HeyDaddy
  • 261
  • 1
  • 2
  • 7

3 Answers3

54

This works for me:

# Dockerfile
FROM php:5.6-apache

MAINTAINER Raphael Mäder <me@randm.ch>

RUN a2enmod rewrite

ADD . /var/www/html

Don't forget to run your docker-compose up command with --build if you have already built the image previously, otherwise it will run the old image which may have not included the RUN a2enmod rewrite statement.

Raphael Mäder
  • 776
  • 5
  • 16
  • 2
    Has anyone figured out how to do this FROM any of the httpd images? I'm more interested in an apache+mod_rewrite image as opposed to a apache+php+mod_rewrite image. – Christian Jul 07 '17 at 19:42
1

Add this to your Dockerfile:

# Enable mod_rewrite for images with apache
RUN if command -v a2enmod >/dev/null 2>&1; then \
        a2enmod rewrite headers \
    ;fi
karel
  • 5,489
  • 46
  • 45
  • 50
0

I had the same problem but I fixed it once I changed the order at httpd.conf. Specifically, I loaded the rewrite module between the server definition and the document definition.

ServerRoot "/usr/local/apache2"
Listen 80

LoadModule rewrite_module modules/mod_rewrite.so

<IfModule unixd_module>
    User daemon
    Group daemon
</IfModule>

ServerAdmin you@example.com
ServerName localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/htdocs/m1"
    ServerName www.sub1.domain.tld
    ServerPath "/sub1/"
    RewriteEngine on
    RewriteRule "^(/sub1/.*)" "/usr/local/apache2/htdocs/s1$1"
</VirtualHost>

This allowed me to use the httpd container instead of the php.

FROM httpd:2.4

I wanted to share in case it helps anyone.