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

More than 5 years have passed since last update.

PHP Slim 環境構築(2) SSLとXdebug

Last updated at Posted at 2019-09-17

PHP Slim 環境構築(2) SSLとXdebug

Introduction

前回は、ひとまず"Hello, World"を表示してみました。

今回は、デバグに必要なXDebugの設定と、ついでにSSL化を行います。

Xdebug

docker-compose

docker-compose.ymlファイルにXdebugと、IntelliJ IDEAのソースとのマッピングを行うための環境変数(XDEBUG_CONFIG, PHP_IDE_CONFIG)を追加します。
なお、172.28.0.1は、dockerコンテナ内部から見たホスト(=ローカルマシン)のIPです。

/compose/docker-compose.yml
#...(略)...
  web_hoge:
    build:
      context: ./web_hoge
      args:
        - environment=local
    environment:
      XDEBUG_CONFIG: "remote_host=172.28.0.1"
      PHP_IDE_CONFIG: "serverName=hoge.localhost"
    domainname: localhost
#...(略)...

IntelliJ IDEA

PHP設定のServersに、web_hoge dockerコンテナ用の設定を追加します。

Settings
> Languages & Frameworks
  > PHP
    Servers

Name: hoge.localhost        (docker-compose.ymlに設定したPHP_IDE_CONFIGのserverName)
Host: hoge.localhost        (web_hogeコンテナにアクセスするホスト名)
Port: 9000                  (XDEBUG用ポート。php-fpmのポート番号と被っているので変更する場合は間違ってそちらを触らないように注意)
Debugger: Xdebug
Use path mappings: [X]
  $(PROJECTROOT)/src → /var/www     ($(PROJECTROOT)は今回作成したプロジェクトのルート)

これでブレークポイントの設定などが行えます。docker-componentの再起動と、IDEのPHP Debug Connectionsの開始をお忘れなく。

SSL化

オレオレ証明書

まず、開発時にのみ使用するローカル用のオレオレ証明書を作成します。コマンドの説明は省略します。

$ cd $(PROJECTROOT)
$ mkdir certs
$ cd certs
$ openssl genrsa 2048 > server.key
$ openssl req -new -key server.key > server.csr

...(いろいろ入力)...

$ openssl x509 -in server.csr -days 2048 -req -signkey server.key > server.crt
$ ls
server.crt  server.csr  server.key

証明書のマウントとポートオープン

docker-composeのFrontEndコンポーネントに、上記で作成した証明書パスのマウントと、SSLポートを追加します。

/compose/docker-compose.yml

services:
  web_front:
    build:
      context: ./web_front
    links:
      - web_action
      - web_manager
    ports:                               # "443:8443"を追加
      - "80:3128"
      - "443:8443"
    volumes:                             # volumes:../certsを追加(read onlyに特に意味は無い) 
      - ../certs:/etc/certs:ro
    container_name: web_front
    networks:
      - local_net
#..(以下略)..

FrontEndコンポーネントのDockerfileにSSL用のEXPOSEを追加します。

/compose/web_front/Dockerfile
# EXPOSE 8443を追加
EXPOSE 3128
EXPOSE 8443

続いて、nginxの設定にssl設定を追加します。

/compose/web_front/default.conf
# 証明書の指定等
ssl_certificate /etc/certs/server.crt;
ssl_certificate_key /etc/certs/server.key;

ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

server {
  listen 3128;
  listen 8443 ssl;                                       # SSLポートの追加
  server_name hoge.localhost;
  access_log /dev/stdout;
  error_log /dev/stderr;
  root /var/www/hoge/public;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
    index     index.php;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass web_hoge:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS $https;                              # http(s)の固定 
  }
}

server {
  listen 3128 default_server;
  listen 8443 ssl default_server;                        # SSLポートの追加 
  server_name _;

  location / {
    return 404;
  }
}

docker-composeをrebuildして再起動します。
https://hoge.localhost/にアクセスできればOKです。
なお、オレオレ証明書なので、ブラウザによっていろいろ警告がでるはずですので、ご注意ください。

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