基本身份验证在 http 和 https 的 apache 服务器中不起作用

问题描述 投票:0回答:1

我有这个 Dockerfile,我正在尝试创建一个容器化的 git 服务器,并希望通过 apache 和 ssh 提供服务

FROM ubuntu:latest


RUN apt-get update && \
    apt-get install -y git openssh-server apache2 apache2-utils ssl-cert && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set up SSH access
RUN mkdir /var/run/sshd
RUN echo 'root:root' | chpasswd
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/^#?PasswordAuthentication\s+.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/^#?ChallengeResponseAuthentication\s+.*/ChallengeResponseAuthentication no/' /etc/ssh/sshd_config

# Add  SSH public key to root's authorized keys
RUN mkdir /root/.ssh && \
    echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEHAtto0bVGTAUATJhiDTjKa/lZg1LYu+gdJcyn4cK8D local" > /root/.ssh/authorized_keys && \
    chmod 600 /root/.ssh/authorized_keys

# Enable Apache SSL and CGI modules
RUN a2enmod ssl && \
    a2enmod cgi && \
    a2ensite default-ssl  # Enable the default SSL site configuration

# Set up Apache to serve the Git repository
RUN mkdir /var/www/git && \
    git init --bare /var/www/git/test.git && \
    chown -R www-data:www-data /var/www/git

# Setup Apache for Git HTTP and HTTPS access with Basic authentication
RUN mkdir /auth && \
    htpasswd -cb /auth/.htpasswd root root && \
    echo "<VirtualHost *:80>\n\
    SetEnv GIT_PROJECT_ROOT /var/www/git\n\
    SetEnv GIT_HTTP_EXPORT_ALL\n\
    ScriptAlias /git/ /usr/lib/git-core/git-http-backend/\n\
    <Directory \"/usr/lib/git-core\">\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    Require all granted\n\
    </Directory>\n\
    <Directory /var/www/git>\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    AuthType Basic\n\
    AuthName \"Git Access\"\n\
    AuthUserFile /auth/.htpasswd\n\
    Require valid-user\n\
    </Directory>\n\
    </VirtualHost>\n\
    <VirtualHost *:443>\n\
    SSLEngine on\n\
    SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem\n\
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key\n\
    SetEnv GIT_PROJECT_ROOT /var/www/git\n\
    SetEnv GIT_HTTP_EXPORT_ALL\n\
    ScriptAlias /git/ /usr/lib/git-core/git-http-backend/\n\
    <Directory \"/usr/lib/git-core\">\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    Require all granted\n\
    </Directory>\n\
    <Directory /var/www/git>\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    AuthType Basic\n\
    AuthName \"Git Access\"\n\
    AuthUserFile /auth/.htpasswd\n\
    Require valid-user\n\
    </Directory>\n\
    </VirtualHost>" > /etc/apache2/sites-available/000-default.conf


# Enable the site and ensure the Apache user can read the .htpasswd file
RUN a2ensite 000-default && \
    chmod 644 /auth/.htpasswd && \
    chown www-data:www-data /auth/.htpasswd


# Expose port 22 for SSH access, 80 for HTTP access, and 443 for HTTPS access
EXPOSE 22 80 443

# Initialize repository and add files
RUN mkdir /tmp/git-repo && \
    cd /tmp/git-repo && \
    git init && \
    git config --global user.email "[email protected]" && \
    git config --global user.name "Your Name" && \
    echo "some: data" > data.yaml && \
    git add data.yaml && \
    git commit -m "Initial commit" && \
    git push /var/www/git/test.git master


# Copy the start script into the container and set permissions
COPY start.sh /start.sh
RUN chmod +x /start.sh

# Command to run the startup script
CMD ["/start.sh"]

这是start.sh

#!/bin/bash
touch /var/log/auth.log
touch /var/log/apache2/access.log
touch /var/log/apache2/error.log
# Starting SSH service
service ssh start
tail -F /var/log/apache2/* /var/log/auth.log &


service apache2 restart

# Run Apache2 in the foreground is important
apache2ctl -D FOREGROUND

现在,当我运行 docker 容器并尝试进入浏览器并访问 http://localhost:8080 时,它不会提示我输入用户名和密码,我也可以 git 克隆 git 存储库,无需身份验证任何方法来解决此问题。我们将不胜感激您的帮助

git docker apache
1个回答
0
投票

我建议不要使用

<Directory>
,而是使用
<Location>
,因为你正在尝试通过url访问git,这样你的apache配置就会看起来像这样

RUN mkdir /auth && \
    htpasswd -cb /auth/.htpasswd root root && \
    echo "<VirtualHost *:80>\n\
    SetEnv GIT_PROJECT_ROOT /var/www/git\n\
    SetEnv GIT_HTTP_EXPORT_ALL\n\
    ScriptAlias /git/ /usr/lib/git-core/git-http-backend/\n\
    <Directory \"/usr/lib/git-core\">\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    Require all granted\n\
    </Directory>\n\
    <Location /git>\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    AuthType Basic\n\
    AuthName \"Git Access\"\n\
    AuthUserFile /auth/.htpasswd\n\
    Require valid-user\n\
    </Directory>\n\
    </VirtualHost>\n\
    <VirtualHost *:443>\n\
    SSLEngine on\n\
    SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem\n\
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key\n\
    SetEnv GIT_PROJECT_ROOT /var/www/git\n\
    SetEnv GIT_HTTP_EXPORT_ALL\n\
    ScriptAlias /git/ /usr/lib/git-core/git-http-backend/\n\
    <Directory \"/usr/lib/git-core\">\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    Require all granted\n\
    </Location>\n\
    <Location /git>\n\
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch\n\
    AuthType Basic\n\
    AuthName \"Git Access\"\n\
    AuthUserFile /auth/.htpasswd\n\
    Require valid-user\n\
    </Location>\n\
    </VirtualHost>" > /etc/apache2/sites-available/000-default.conf

有关更多信息,请参阅此问题

© www.soinside.com 2019 - 2024. All rights reserved.