信任本地主机的自签名SSL证书(非交互式)

时间:2018-12-27 07:25:41

标签: docker ssl https localhost self-signed

我已经使用以下Dockerfile在本地主机上设置了HTTPS:

FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive TZ=America/New_York APACHE_RUN_USER=www-data APACHE_RUN_GROUP=www-data APACHE_LOG_DIR=/var/log/apache2 APACHE_LOCK_DIR=/var/lock/apache2 APACHE_PID_FILE=/var/run/apache2.pid
COPY . /var/www/html
WORKDIR /var/www/html
# install apache, php, ssl cert for localhost
RUN apt-get update && \
  apt-get -y install apt-utils tzdata apache2 php libapache2-mod-php && \
  ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
  a2enmod ssl headers rewrite && \
  mv ./apache/ssl-cert-snakeoil.pem /etc/ssl/certs/ssl-cert-snakeoil.pem && \
  mv ./apache/ssl-cert-snakeoil.key /etc/ssl/private/ssl-cert-snakeoil.key && \
  mv ./apache/000-default.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80 443
# start webserver
CMD /usr/sbin/apache2ctl -D FOREGROUND

其中ssl-cert-snakeoil.pemssl-cert-snakeoil.key的生成方式是:

openssl req -x509 -out ssl-cert-snakeoil.pem -keyout ssl-cert-snakeoil.key \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=localhost' -extensions EXT -config <( \
   printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

000-default.conf看起来像这样:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "/var/www/html/"
    ServerName localhost
    SSLEngine on
    SSLCertificateFile "/etc/ssl/certs/ssl-cert-snakeoil.pem"
    SSLCertificateKeyFile "/etc/ssl/private/ssl-cert-snakeoil.key"
</VirtualHost>

现在,我可以在浏览器中访问https://localhost,但是它警告我说该证书不受信任。我意识到我可以将证书添加到浏览器的信任存储中,但是我的目标是让运行在容器中的其他程序能够访问主机上的https://localhost

StackExchange上的其他答案(123)说,我应该能够通过将ssl-cert-snakeoil.pem从服务于{{1}的容器中移出来做到这一点。 },使其专门访问https://localhost/usr/share/ca-certificates//usr/local/share/ca-certificates/(取决于答案)之一,然后运行/etc/ssl/certs/或{{1 }}(同样,取决于答案)。

因此,我用dpkg-reconfigure ca-certificates旋转了一个容器,将证书添加到各个位置并运行了各种命令,但是没有任何效果。运行update-ca-certificates --fresh似乎很有希望,因为它实际上会提示我说“此软件包在/ usr / share / ca-certificates中安装了常见的CA(证书颁发机构)证书”,然后要求我从{{3 }}我可以添加docker run --network="host" -it ubuntu /bin/bash。但这行不通。

我已经完成所有这些步骤以及它们的每种组合,但是当我尝试dpkg-reconfigure ca-certificates时,我仍然会得到echo /path/to/cert.crt >> /etc/ca-certificates.conf。同样,当我尝试运行curl https://localhost并说curl: (60) SSL certificate problem: unable to get local issuer certificate时,PHP会抱怨。我究竟做错了什么?此外,由于我试图在Dockerfile中完成所有这些操作,因此我需要一种非交互方式运行<?php echo file_get_contents("https://localhost");的方式(如果这确实是正确的命令)。

0 个答案:

没有答案