LoginSignup
9
4

More than 5 years have passed since last update.

[メモ] Dockerにて、PHP 5.6 + Apache + MySQL 5.6 を XDebugする環境作成

Last updated at Posted at 2018-02-08

概要

  • Dockerにて、PHP 5.6 + Apache + MySQL 5.6 を XDebugする環境作成
  • Eclipseにて
    image.png

環境

コンテナの設定・起動

  1. Docker実行環境作成

  2. 以下コピペ

    コピペすると、ごにょごにょしたあと、コンテナが立ち上がります。
    # ワークフォルダ 
    cd 
    mkdir php
    cd php
    
    mkdir www
    cat <<EOF >www/index.php
    <html>
     <head>
      <title>PHP Test</title>
     </head>
     <body>
     <?php echo '<p>Hello World</p>';
     phpinfo();
     ?> 
     </body>
    </html>
    EOF
    
    #
    # Docker関係.
    #
    cat <<EOF >.dockerignore
    mysql-cnf
    mysql-data
    EOF
    
    cat <<'EOF' >Dockerfile
    FROM php:5.6-apache
    RUN yes | pecl install xdebug-2.5.5 \
        && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
        && echo \
    'xdebug.remote_enable=1\n'\
    'xdebug.remote_autostart=1\n'\
    'xdebug.remote_connect_back=1\n'\
    'xdebug.remote_port=9000\n'\
    >> /usr/local/etc/php/conf.d/xdebug.ini
    RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
    RUN docker-php-ext-install gettext && docker-php-ext-enable gettext
    RUN a2enmod rewrite
    RUN a2enmod ssl
    ADD ssl.conf /etc/apache2/sites-available/ssl.conf
    RUN a2ensite ssl
    EXPOSE 443
    EOF
    
    #
    # docker-compose.yml
    #
    cat <<'EOF' >docker-compose.yml
    php:
      build: .
      #image: php:5.6-apache
      container_name: php
      ports:
       - "80:80"
       - "443:443"
      volumes:
        - ./www:/var/www/html
        - ./keys/server.key:/etc/apache2/server.key
        - ./keys/server.crt:/etc/apache2/server.crt
        - ./ssl.conf:/etc/apache2/sites-available/ssl.conf
      links:
        - mysql
    mysql:
      image: mysql:5.6
      container_name: mysql
      volumes:
        - ./mysql-data:/var/lib/mysql
        - ./mysql-cnf:/etc/mysql/conf.d
      command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
      environment:
        MYSQL_ROOT_PASSWORD: password
      ports:
        - "3306:3306"
    pma:
      image: phpmyadmin/phpmyadmin
      container_name: phpmyadmin
      ports:
       - "8080:80"
      volumes:
        - ./mysql-data:/var/lib/mysql
        - ./mysql-cnf:/etc/mysql/conf.d
      environment:
        PMA_ARBITARY: 1
        PMA_HOST: mysql
        PMA_USER: root
        PMA_PASSWORD: password
      links:
        - mysql
    EOF
    
    #
    # HTTPS関係
    #
    # キー作成
    mkdir ./keys
    cd ./keys
    #秘密鍵の作成
    openssl genrsa 2048 > server.key
    
    #証明書署名要求の作成
    openssl req -new -key server.key <<EOF > server.csr
    JP
    Tokyo
    Minato-ku
    Company
    Section
    vagrant.local
    hoge@example.com
    
    Optional Company Name
    EOF
    #サーバ証明書の作成 - 100日くらい?
    CERT_EXPIRE_DAYS=100
    openssl x509 -in server.csr -days ${CERT_EXPIRE_DAYS} -req -signkey server.key > server.crt
    cd ..
    
    # apacheのssl設定
    cat <<'EOF' > ssl.conf
    <VirtualHost _default_:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/apache2/server.crt
        SSLCertificateKeyFile /etc/apache2/server.key
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
        </Directory>
        BrowserMatch "MSIE [2-6]" \
                      nokeepalive ssl-unclean-shutdown \
                      downgrade-1.0 force-response-1.0
        # MSIE 7 and newer should be able to use keepalive
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
    EOF
    
    # 起動
    docker-compose up -d
    
    # 終わったら
    # docker-compose stop # 使用したコンテナ残す
    # docker-compose down # 使用したコンテナ消す
    
  3. ブラウザを起動して、index.phpが表示されるか、確認。

    • http://<dockerの起動しているマシンのIP> とか、
    • VMでポートフォワードしているなら、http://localhost
    • https://でもいけるハズ。おれおれ証明なので、ブラウザでワーニング(Not secure)が出る
      ADVANCEDというのをクリックして、Proceed to localhost (unsafe)をクリック

image.png

説明

後ほど..

デバッガの設定(XDebug使用)

後ほど..

Eclipse (PHP)
VisualStudioCode
PHPStorm

その他

9
4
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
9
4