无法让 SSL 在 Docker 容器中工作

Cannot get SSL to work in Docker container

首先让我说我不是服务器管理员,有很多事情我不知道。因此,我确信我在设置 Docker 容器的某个地方犯了一个错误,因为 SSL 不起作用。

容器 运行ning Apache 2.4 与 PHP 5.6 在 Ubuntu 14.04 上,并链接到 MySQL 5.6 Docker 容器。

我从 Docker - Official PHP Repo 开始使用这个基本设置。以下是相关文件:

Dockerfile

FROM php:5.6-apache

RUN apt-get update
RUN apt-get install -y net-tools
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install sockets

RUN a2enmod rewrite
RUN a2enmod ssl

ADD 000-default.conf /etc/apache2/sites-enabled/
ADD default-ssl.conf /etc/apache2/sites-anabled/
ADD apache2.conf /etc/apache2/
ADD www-server/ www-server/

EXPOSE 443

000-default.conf

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /www-server/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<Directory /home/www-server/>
    # allow .htaccess overrides to work
    AllowOverride All
    DirectoryIndex login.html index.html index.php
</Directory>

<Directory /home/www-server/client>
    DirectoryIndex home.html
    Options All
    AllowOverride All
    Require all granted
</Directory>

默认-ssl.conf

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /home/www-server

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # Enable/Disable SSL for this virtual host.
        SSLEngine on

        SSLCertificateFile /etc/ssl/certs/site.crt
        SSLCertificateKeyFile /etc/ssl/certs/site.key
        SSLCACertificatePath /etc/ssl/certs/digicert/

     </VirtualHost>
</IfModule>

apache2.conf

Mutex file:/var/lock/apache2 default
PidFile /var/run/apache2/apache2.pid
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User www-data
Group www-data
HostnameLookups Off
ErrorLog /proc/self/fd/2
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# ports.conf
Listen 80
<IfModule ssl_module>
        Listen 443
</IfModule>
<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /home/www-server/>
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

DocumentRoot /home/www-server

AccessFileName .htaccess
<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

CustomLog /proc/self/fd/1 combined

<FilesMatch \.php$>
        SetHandler application/x-httpd-php
</FilesMatch>

# Multiple DirectoryIndex directives within the same context will add
# to the list of resources to look for rather than replace
# https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex
DirectoryIndex disabled
DirectoryIndex index.php index.html

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

注意:我已经删除了这些文件中不相关的部分。

运行 Docker 容器我使用这个:

sudo docker run -v /home/src/ssl-cert:/etc/ssl/certs --name app-gateway --link mysql56:mysql -p 80:80 -p 443:443 -d  app-image

这是日志文件的一部分:

::1 - - [10/Nov/2015:19:26:19 +0000] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.4.10 (Debian) PHP/5.6.15 OpenSSL/1.0.1k (internal dummy connection)"
::1 - - [10/Nov/2015:19:26:20 +0000] "OPTIONS * HTTP/1.0" 200 152 "-" "Apache/2.4.10 (Debian) PHP/5.6.15 OpenSSL/1.0.1k (internal dummy connection)"
72.5.190.136 - - [10/Nov/2015:19:26:21 +0000] "\x16\x03\x01" 400 0 "-" "-"
72.5.190.136 - - [10/Nov/2015:19:26:21 +0000] "\x16\x03\x01" 400 0 "-" "-"
72.5.190.136 - - [10/Nov/2015:19:26:21 +0000] "\x16\x03\x01" 400 0 "-" "-"

在浏览器中出现此错误:

ERR_SSL_PROTOCOL_ERROR

TL;DR 我已按照我在网上找到的所有步骤在 Docker 容器中安装和使用我的 SSL 证书,但我没有成功使其工作。让 SSL 在 Docker 容器中工作是否有明显的我忽略的事情?

有时,尤其是当您尝试解决一个问题已经很长时间并且在命令行中的控制台 window 中编码时,您会错过最简单的事情。看看 Dockerfile 中的这两行:

ADD 000-default.conf /etc/apache2/sites-enabled/
ADD default-ssl.conf /etc/apache2/sites-anabled/

ae 几乎没有区别,因此很难找到拼写错误。 Dockerfile 正确构建映像并 添加 容器中的新目录,然后将配置文件放在该目录中。

drwxr-xr-x  2 www www  4096 Nov 11 15:56 sites-anabled
drwxr-xr-x  2 www www  4096 Oct 23 20:19 sites-available
drwxr-xr-x  2 www www  4096 Nov 11 15:56 sites-enabled

由于未知目录不会抛出任何错误并且映像构建成功,因此它将 运行,但在这种情况下,SSL 将无法正常工作。

我不想像其他答案那样 'add' 文件。 HTML 文件将通过挂载到 /var/www 安装。 我所需要的只是接受 HTTPS (SSL) 的 apache 服务器, 将 HTTP(非 SSL)重定向到 HTTPS,以及一些次要的 PHP 配置。

这是我在 DockerFile 中所做的...

# Install PHP Apache as the base image
FROM "php:7.3-apache"

# Install ssl-cert (snakeoil) package
RUN set -eux; \
    apt-get update && apt-get install -y ssl-cert; \
    : "Clean Apt system" ;\
    apt-get clean; \
    rm -rf /var/lib/apt/lists/*; \
    rm -rf /tmp/* /var/tmp/* /var/log/lastlog /var/log/faillog; \
    rm -f /var/log/{apt/*,alternatives.log,dpkg.log}; \
    :;

# Set up apache with HTTPS redirect
RUN set -eux; \
    : "Time Zone"; \
    ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime; \
    : "Apache Changes" ;\
    # enable SSL \
    a2enmod rewrite ssl; \
    a2ensite default-ssl; \
    cd /etc/apache2/sites-enabled/; \
    sed -i '/<\/VirtualHost>/d'                          000-default.conf; \
    echo 'RewriteEngine On'                           >> 000-default.conf; \
    echo 'RewriteRule . https://%{SERVER_NAME} [R,L]' >> 000-default.conf; \
    echo '</VirtualHost>'                             >> 000-default.conf; \
    :;

# PHP Configuration
RUN set -eux; \
    cd $PHP_INI_DIR; \
    cp php.ini-production php.ini; \
    cd conf.d ;\
    echo "date.timezone=Australia/Sydney"      > date_timezone.ini; \
    echo "session.gc_probability=1"            > session.ini; \
    echo "session.gc_divisor=100"             >> session.ini; \
    echo "session.gc_maxlifetime=1440"        >> session.ini; \
    echo "session.cookie=1440"                >> session.ini; \
    :;

希望对您有所帮助