无法在Docker容器

时间:2015-11-10 19:41:31

标签: apache ssl https docker openssl

首先我要说的是我不是服务器管理员,而且还有许多我不知道的事情。因此,我确信我在设置我的Docker容器时犯了一个错误,因为SSL不起作用。

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

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

Dockefile

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证书,但我没有成功使其工作。有没有明显的东西让我忽略了在Docker容器中使用SSL?

2 个答案:

答案 0 :(得分:32)

有时候,特别是当您尝试长时间解决问题并且在命令行的控制台窗口中进行编码时,您会错过最简单的事情。看看Dockerfile中的这两行:

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

a几乎与e无法区分,导致拼写错误难以找到。 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将无法正常工作。

答案 1 :(得分:0)

我不想像其他答案那样“添加”文件。 并且 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; \
    :;

希望对您有所帮助