LoginSignup
33
41

More than 3 years have passed since last update.

mkcertを使ってローカル環境でもDockerでも楽々SSL

Last updated at Posted at 2018-08-18

概要

mkcertをTwitterで見てローカル環境でもDockerでも楽々にSSLができるとのことで試して見た。

環境

Docker for Mac

ディレクトリ構成

ssl-test/
├── ssl-conf/
│  ├── httpd/
│  │   └── ssl.conf
│  └── secret-keys/
│    ├── localhost+1.pem
│     └── localhost+1-key.pem
├── index.php
├── Dockerfile
└── docker-compose.yml

docker-compose.ymlの記述

docker-compose.yml
version: '3'
services: 
  ssl:
    build: ./
    ports:
      - 80:80
      - 443:443
    privileged: true
    volumes: 
      - ./:/var/www/html
    container_name: 'ssl'

Dockerfileの記述

FROM centos:7.5.1804

RUN yum -y update

# 外部リポジトリ(EPEL, Remi)を追加
RUN yum -y install epel-release
RUN yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

# apache その他 phpパッケージを導入
RUN yum -y install httpd
RUN yum -y install --enablerepo=remi,remi-php72 php php-cli php-common php-devel php-fpm php-gd php-mbstring php-mysqlnd php-pdo php-pear php-pecl-apcu php-soap php-xml php-xmlrpc 

RUN yum -y install mod_ssl openssl

CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

WORKDIR /var/www/html

index.phpの記述

<?php phpinfo(); ?>
// 記述は何でも良い

Dockerfileのbuildおよびdocker-composeの立ち上げ

docker-compose build
docker-compose up -d

ssl.confの作成

docker-compose exec ssl bash
cat /etc/httpd/conf.d/ssl.conf > /var/www/html/ssl.conf

作成したssl.confssl-conf/httpdディレクトリに配置する。

mkcertのインストール

コンテナの外に出て(exit外に出る)


brew install mkcert
mkcert -install
mkcert localhost 127.0.0.1

を実行する。

作成されたlocalhost+1.pem localhost+1-key.pem を ssl-conf/secret-keysに配置する。

ssl.confの編集

ssl.conf
- SSLCertificateFile /etc/pki/tls/certs/localhost.crt
+ SSLCertificateFile /var/www/html/ssl-conf/secret-keys/localhost+1.pem

- SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
+ SSLCertificateKeyFile /var/www/html/ssl-conf/secret-keys/localhost+1-key.pem

dokcer-compse.ymlの編集

docker-comopse.yml
version: '3'
services: 
  ssl:
    build: ./
    ports:
      - 80:80
      - 443:443
    privileged: true
    volumes: 
      - ./:/var/www/html
      - ./ssl-conf/httpd/ssl.conf:/etc/httpd/conf.d/ssl.conf
    container_name: 'ssl'

再度docker-compose up -dでコンテナを立ち上げlocahlostに接続する。

スクリーンショット 2018-08-18 12.22.59.png

参考

mkcert

更新

Nginx版

33
41
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
41