12
24

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.

PHP7+Laravel5.4+docker環境構築

Last updated at Posted at 2017-01-28

インストール

laravel の開発環境作りやったので、そのメモです
なんで mysql5.5 なのかと言うと、とあるレンタルサーバに合わせているからです

  • install docker
brew install docker
  • install composer
brew install composer
# すでに composer インストール済なら
brew upgrate composer
  • install Laravel
composer global require "laravel/installer"
export PATH="$HOME/.composer/vendor/bin:$PATH"
  • Laravelプロジェクトを生成
laravel new appname
  • diff確認しやすいように、とりあえず初回コミット
cd appname
git init
git add -A
git commit -m "First commit"
  • 起動してみる
php artisan serve
  • localhost:8000 にアクセスして画面表示されたらOK

docker環境構築

  • docker-compose.yml 書く
docker-compose.yml
version: '2'

services:
  datastore:
    image: busybox
    volumes:
     - /var/lib/mysql
  db:
    image: mysql:5.5
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
    ports:
      - "3306:3306"
    volumes_from:
      - datastore
  cache:
    image: redis:3.2.3-alpine
    ports:
      - "6379:6379"
  app:
    image: php:7.1.1-apache
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
      - cache
  • docker動かす
docker-compose up
  • しばらく待って  http://localhost へアクセス
  • あれ、見れない?
  • http://localhost/public へアクセス
  • 見れた。どうやら DocumentRoot がずれている
000-default.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/public
    <Directory "/var/www/html/public">
            AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • docker-compose.yml でapacheの設定ファイルを上書き
docker-compose.yml
     volumes:
       - .:/var/www/html
+      - ./000-default.conf:/etc/apache2/sites-available/000-default.conf
     depends_on:
       - db
       - cache
  • docker-compose up を実行して、再度 http://localhost へアクセス
  • 見れた!!

認証機能入れてみる

php artisan make:auth
  • http://localhost/ へアクセスして右上の register をクリック
  • 見れない?
  • なんか rewrite されてなさげ。。。
  • 仕方ないので Dockerfile で有効にするようにしてみた 
Dockerfile
FROM php:7.1.1-apache
RUN a2enmod rewrite
  • Dockerfileからbuildするように修正
docker-compose.yml
   app:
-    image: php:7.1.1-apache
+    # image: php:7.1.1-apache
+    build: .
     ports:
       - "80:80"
     volumes:
  • 一回止めて docker-compose builddocker-compose up を実行
  • http://localhost/register へアクセス
  • 見れた!
  • フォーム入力してみた
  • 落ちた
  • データベース設定見直し
.env
- DB_HOST=127.0.0.1
+ DB_HOST=db
  DB_PORT=3306
- DB_DATABASE=homestead
- DB_USERNAME=homestead
- DB_PASSWORD=secret
+ DB_DATABASE=dbname
+ DB_USERNAME=root
+ DB_PASSWORD=

  BROADCAST_DRIVER=log
  CACHE_DRIVER=file
  SESSION_DRIVER=file
  QUEUE_DRIVER=sync

- REDIS_HOST=127.0.0.1
+ REDIS_HOST=cache
  REDIS_PASSWORD=null
  REDIS_PORT=6379
  • pdoもなさげだったので
Dockerfile
  FROM php:7.1.1-apache
+ RUN docker-php-ext-install pdo pdo_mysql
  RUN a2enmod rewrite
  • 1回止めて docker-compose builddocker-compose up を実行
  • db作成
mysql -uroot -h127.0.0.1

> CREATE DATABASE dbname DEFAULT CHARSET utf8;
> exit
  • migration
docker-compose exec app php artisan migrate

Migration table created successfully.

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`
  (`email`))

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
  • ここで mysql5.5 で泣く。
  • ググった結果、以下のように回避
config/database.php
             'database' => env('DB_DATABASE', 'forge'),
             'username' => env('DB_USERNAME', 'forge'),
             'password' => env('DB_PASSWORD', ''),
-            'charset' => 'utf8mb4',
-            'collation' => 'utf8mb4_unicode_ci',
+            'charset' => 'utf8',
+            'collation' => 'utf8_unicode_ci',
             'prefix' => '',
             'strict' => true,
             'engine' => null,
  • 一度db作り直し
> database drop dbname
> CREATE DATABASE dbname DEFAULT CHARSET utf8;
  • 再度、migrate!
docker-compose exec app php artisan migrate

Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
  • http://localhost/register へアクセスして入力してみた
  • 無事ユーザ登録成功!
12
24
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
12
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?