1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

localhost で https://リクエストを受け取れる環境を作る

Posted at

環境

  • wsl
  • docker
  • php
  • apache

ディレクトリ構成

/project
├── docker
│   ├── apache
│   │   └── custom.conf
│   └── php
│       ├── php.ini
│       └── Dockerfile
├── src
│   ├── index.php
│   └── .htaccess
└── docker-compose.yml

前準備(httpでのサーバー立ち上げまで)

1. docker-compose.ymlの作成

docker-compose.yml
services:
  app:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    ports:
      - "8266:80"
    volumes:
      - ./src:/var/www/html
      - ./docker/apache/custom.conf:/etc/apache2/sites-available/000-default.conf
      - ./docker/php/php.ini:/usr/local/etc/php/php.ini
    working_dir: /var/www/html

2. php.iniの作成

php.ini
display_errors = On
memory_limit = 512M
upload_max_filesize = 20M
post_max_size = 30M

3. Dockerfile の作成

FROM php:8.4.7-apache

RUN a2enmod rewrite

COPY docker/php/php.ini /usr/local/etc/php/
COPY docker/apache/custom.conf /etc/apache2/sites-available/000-default.conf

WORKDIR /var/www/html

COPY ./ /var/www/html

4. custom.conf の作成

custom.conf
<VirtualHost *:80>
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

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

index.php(確認用)の作成

index.php
<?php
phpinfo();

上記ファイルが用意できたら
docker compose up -d をターミナルで実行してコンテナが立ちあげる。
localhost:8266にアクセスしてphpinfoが表示されていれば完了。

SSL化

今回はopenSSLを使用して取得していく。

1. まず/apacheディレクトリ配下に sslディレクトリを作成する

mkdir ./docker/apache/ssl

2. ssl証明証を発行する

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
 -keyout docker/apache/ssl/server.key \
 -out docker/apache/ssl/server.crt \
 -subj "/C=JP/ST=Tokyo/L=Shibuya/O=Dev/OU=Dev/CN=localhost"

docker/apache/ssl/ディレクトリ内に server.key 及び server.crt が生成されているか確認する。

3. custom.conf を編集、更新する

diff_custom.conf
<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/server.key

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

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

4. docker-compose.yml を編集、更新する

docker-compose.yml
services:
  app:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    ports:
      - "8266:80"
      - "8443:443"  # ← 追加
    volumes:
      - ./src:/var/www/html
      - ./docker/apache/custom.conf:/etc/apache2/sites-available/000-default.conf
      - ./docker/php/php.ini:/usr/local/etc/php/php.ini
      - ./docker/apache/ssl:/etc/apache2/ssl  # ← 追加
    working_dir: /var/www/html

5. Dockerfile を編集、更新する

FROM php:8.4.7-apache

RUN a2enmod rewrite ssl

COPY docker/php/php.ini /usr/local/etc/php/

COPY docker/apache/custom.conf /etc/apache2/sites-available/000-default.conf

WORKDIR /var/www/html
COPY ./ /var/www/html

6. 起動

docker compose down
docker compose build --no-cache
docker compose up -d

ttps://localhost:8443 にアクセスして phpindoが表示されれば完了

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?