httpsでの開発環境構築

SSL/TLSでの通信のために必要なファイルを準備

環境

openssl v3.2.1

秘密鍵を作成

$ openssl genpkey -out private.key -algorithm RSA

証明書署名要求を作成(証明書作成のために作成)

$ openssl req -new -key private.key -out apache.csr

上記コマンド実行時の、入力必須項目。

  • Country Name
    国名(JP)
  • Organization Name
    組織名
  • Common Name
    サーバのFQDN

証明書を作成

$ openssl x509 -in apache.csr -out apache.crt -req -signkey private.key -days 3650

次の表示が出て、apache.crtが作成される。

Certificate request self-signature ok
subject=C = JP, ST = Some-State, O = TestCompany, CN = 設定したFQDN

設定ファイルを編集(基本的に過去のものを引き継いで編集している)

過去の参考

default-ssl.conf

編集内容は次の5点

  • ServerName
  • DocumentRoot
  • DirectoryIndex
  • SSLCertificateFile
  • SSLCertificateKeyFile
<VirtualHost *:443>
	ServerAdmin webmaster@localhost

	ServerName サイトのドメインを指定

	DocumentRoot /var/www/html/public
    DirectoryIndex index.php

    SSLCertificateFile      /etc/ssl/certs/apache.crt
	SSLCertificateKeyFile   /etc/ssl/private/private.key

docker-compose.yml

version: "3"
services:
  php-apache:
    build: ./PHP-Apache
    container_name: PHP-Apache
    ports:
      - 80:80
      - 443:443        ← httpsのポート番号追加しただけ。
    volumes:
#      - \\wsl.localhost\Ubuntu\Development\TestSite:/var/www/html
      - /Users/user_name/Development/aiueo/Code:/var/www/html
  postgresql:
    build: ./PostgreSQL
    container_name: PostgreSQL
    ports:
      - 5432:5432
    volumes:
#      - \\wsl.localhost\Ubuntu\Development\DevEnvironment\postgresql.conf:/etc/postgresql/postgresql.conf
      - /Users/user_name/Development/aiueo/DevEnvironment/aiueo_DevelopEnvironment/docker/Dockerfile/PostgreSQL/postgresql.conf:/etc/postgresql/postgresql.conf
    command: '-c config_file=/etc/postgresql/postgresql.conf'

php-apacheコンテナのDockerfile

次の内容になった。

FROM php:8.2.11-apache
LABEL authors="shinke1987"

# Composerのインストール。
# Composerの公式ページから引用。
COPY --from=composer/composer:latest-bin /composer /usr/bin/composer

# アップデート。
RUN apt-get update
RUN apt-get -y install less git unzip

# タイムゾーンの設定
RUN cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# pdo_pgsqlで必要。
RUN apt-get -y install libpq-dev

# apachectl status コマンドの実行に必要。
RUN apt-get -y install lynx

# Apacheのファイルをコピー。
COPY ./apache2.conf /etc/apache2/
COPY ./000-default.conf /etc/apache2/sites-available/
COPY ./default-ssl.conf /etc/apache2/sites-available/    ← この行追加。

# PHPのファイルをコピー。
COPY ./php.ini /usr/local/etc/php/

# TLS/SSL接続に必要な証明書をコピー。
COPY ./cert/apache.crt /etc/ssl/certs/    ← この行追加。

# TLS/SSL接続に必要な秘密鍵をコピー。
COPY ./cert/private.key /etc/ssl/private    ← この行追加。

## SSLのモジュールを有効化。
## mod_rewriteを有効化。(https://URL/aiueoへアクセスして404としないため)(20240314追記)
RUN a2enmod ssl rewrite           ← この行追加。

## httpsでのバーチャルホスト有効化。
RUN a2ensite default-ssl    ← この行追加。

# Laravelで必要なPHPの拡張ライブラリを設定。
RUN docker-php-ext-install pdo_pgsql

# xDebugインストール。
RUN pecl install xdebug-3.2.2
# xDebug設定。
RUN docker-php-ext-enable xdebug
# xDebug用設定ファイルをコピー。
COPY ./docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/


# Apache起動。
CMD ["apache2-foreground"]

ディレクトリ構成

docker
└ Dockerfile
  ├ PHP-Apache
  │ ├ cert
  │ │ ├ apache.crt
  │ │ ├ apache.csr
  │ │ └ private.key
  │ ├ 000-default.conf
  │ ├ apache2.conf
  │ ├ default-ssl.conf
  │ ├ docker-php-ext-xdebug.ini
  │ ├ Dockerfile
  │ └ php.ini
  ├ PostgreSQL
  │ ├ Dockerfile
  │ ├ pg_hba.conf
  │ └ postgresql.conf
  ├ .env
  └ docker-compose.yml

コンテナ作成

上記ディレクトリ構成のDockerfile直下にて次のコマンドを実行。
注意点として、dockerのイメージまで削除して実行する必要がある。

# docker compose up -d

その後、Webブラウザでhttps://URLにアクセスし、証明書について注意されても続行すれば、
httpでアクセスした時と同じ場所へアクセスできる。

その他

a2enmodコマンドやa2ensiteコマンドは、デフォルトのapache2.confファイル内にその存在が書かれているが、
manページはコンテナ内に存在しなかった。

コメント

タイトルとURLをコピーしました