Search code examples
dockerapache.htaccessdockerfile.htpasswd

.htaccess with .htpasswd Password Mismatch In Docker Container


I am trying to take a website i build on Windows into a Docker Container. The Website works without Flaws but the Problem is when i try to Login i get this error in the Logs of the Docker Container:

172.17.0.2:80 192.168.2.27 - - [03/Dec/2023:13:00:37 +0000] "GET / HTTP/1.1" 200 1597 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0"
172.17.0.2:80 192.168.2.27 - - [03/Dec/2023:13:00:43 +0000] "GET /admin/adminpanel.php HTTP/1.1" 401 728 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0"
[Sun Dec 03 13:00:46.198385 2023] [auth_basic:error] [pid 18] [client 192.168.2.27:61450] AH01617: user finn: authentication failure for "/admin/adminpanel.php": Password Mismatch
172.17.0.2:80 192.168.2.27 - finn [03/Dec/2023:13:00:46 +0000] "GET /admin/adminpanel.php HTTP/1.1" 401 727 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0"

This is the DockerFile:

# Use an official PHP runtime as a parent image
FROM php:7.4-apache

# Set the working directory to /var/www/html
WORKDIR /var/www/html

# Copy the current directory contents into the container at /var/www/html
COPY . /var/www/html

# Install additional dependencies and enable required Apache modules
RUN apt-get update \
    && apt-get install -y libpng-dev libjpeg-dev libzip-dev \
    && docker-php-ext-configure gd --with-jpeg \
    && docker-php-ext-install gd mysqli zip \
    && a2enmod rewrite \
    && service apache2 restart

# Set proper permissions for the Files directory
RUN chown -R www-data:www-data /var/www/html/Files \
    && chmod -R 755 /var/www/html/Files

# Copy the .htpasswd file to the correct location and set permissions
COPY --chmod=644 .htpasswd /var/www/html/admin/.htpasswd
RUN chmod 644 /var/www/html/admin/.htpasswd

# Update Apache configuration to allow .htaccess
RUN { \
    echo '<Directory /var/www/html/>'; \
    echo '    Options Indexes FollowSymLinks'; \
    echo '    AllowOverride All'; \
    echo '    Require all granted'; \
    echo '</Directory>'; \
} > /etc/apache2/sites-available/000-default.conf

# Expose port 80 for Apache
EXPOSE 80

# Start Apache in the foreground
CMD ["apache2-foreground"]

This is the .htacces File for the Admin Folder:


AuthType Basic
AuthName "restricted area"
AuthUserFile /var/www/html/admin/.htpasswd
require valid-user

This is the .htpasswd file also in the admin Folder:

finn:test

I had the error that it coud not open the file but after giving it the permission with the chmod command that error whent away but the password is now Missmatch.


Solution

  • .htpasswd files should not contain plain text values though, they only work on Windows: https://httpd.apache.org/docs/2.4/misc/password_encryptions.html. As you mentioned, you're using php7.4-apache docker image which is built on top of debian11-slim

    Change your Dockerfile to include the run of $ htpasswd -nbd finn test

    or copy an .htpasswd file with the value already encrypted when building the image.